Symfony 替代新动作

发布于 2024-12-20 04:37:56 字数 885 浏览 1 评论 0 原文

我有一个服务器,我需要在其中存储一些图像。现在图像可以上传或动态创建,让我们说只需向某些默认图片添加一些文本(所以我为此创建了一个文件 MakenewForm.php )。我的数据库中的表将文件名存储在本地文件系统上。现在上传很简单,我只需使用默认的 _new 操作即可。为了创建图片,我做了一个新动作,比如makenewnewmakenew 都显示在列表视图上。我将 newSuccess.php 复制到 makenewSuccess.php。现在我想要为他们提供一组不同的提交按钮。我似乎不知道该怎么做。我看到的只是这样:

<div id="sf_admin_content">
<?php include_partial('poster/form', array('poster' => $poster, 'form' => $form,       'configuration' => $configuration, 'helper' => $helper)) ?>
</div>

我不知道什么是$configuration,什么是$helper。有人可以告诉我关于他们的事吗?我需要更改哪一项以及如何更改?

另外,正如您可以推断的,new操作的提交操作只需要执行$form->save(),但makenew的提交操作需要执行$form->save() 需要获取所有文本输入并写入图像文件(假设使用imagejpeg

我需要一些实现这一目标的指示。

I have a server where I need to store some images. Now the images can be either uploaded or created on the fly let us say just by adding some text to some default picture(So I made a file MakenewForm.php for that) . The table in my database stores filename on the local filesystem. Now upload is simple, I can just use the default _new action for that. For creating picture, I made a new action, say makenew. new and makenew both are diplayed on the list view. I copied newSuccess.php to makenewSuccess.php. Now I want a different set of submit buttons for them. I can't seem to figure out how to do that. All I see is this:

<div id="sf_admin_content">
<?php include_partial('poster/form', array('poster' => $poster, 'form' => $form,       'configuration' => $configuration, 'helper' => $helper)) ?>
</div>

I don't know what is $configuration and what is $helper. Can someone tell me about them? Which one do I need to change and How?

Also, as you can infer, the submit action of the new action only needs to do a $form->save() but the submit action of makenew needs to take all the text input and write an image file(let's say using imagejpeg)

I need some pointers towards achieving this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄断弥空 2024-12-27 04:37:56

我希望能够很好地关注您正在寻找的东西。

关于 $configuration$helper,您可以在 cache\mypplication\dev\modules\autoMymodule\actions\actions.class.php 中找到函数 preExecute() 也用于创建它们:

$this->configuration = new mymoduleGeneratorConfiguration();
$this->helper = new mymoduleGeneratorHelper();

这些类位于 apps\docdesk\modules\mymodule\lib 中。您可以在 cache\mypplication\dev\modules\autoMymodule\lib 中找到基类。查看这些基类以了解如何使用以及管理生成器提供哪些配置函数和辅助函数。

为了处理您的新操作,我不知道您正在开发的功能,因此我将尝试想象两种可能的场景。您只能使用一个表单,通过自定义小部件切换上传/创建图片的功能,覆盖模板 _form.php(在您的代码片段中调用)以及新操作等等,或者,这就是按照您所遵循的方式,创建一个完全独立的操作、表单和所有需要的模板。因此,在 makenewSucces.php 中,您可以包含一个使用

$poster, 'form' => $form) 调用的模板 _makeform.php , 'configuration' => $configuration, 'helper' => $helper)) ?>

当然你必须把模板apps\docdesk\modules\mymodule\template 中的 _makeform.php 作为每个新的或覆盖的。

我不太明白你对图像保存的麻烦...我想你是在询问图像的保存链以及如何以及在哪里管理你所需要的内容。
要添加小部件以在 PosterForm 类中上传图像,您可以使用这样的代码片段,其中我们假设 photo 作为小部件名称,根据架构字段 photo : { type: varchar(255), required: true },那就当然是自定义了:

    public function configure()
    {
        $photo = $this->getObject()->getPhoto(); // get the photo name
        $photo = sfConfig::get('sf_upload_image_dir').$photo;

        $this->widgetSchema['photo'] = new sfWidgetFormInputFileEditable(array(
            'label'     => 'Photo',
            'file_src'  => $photo,
            'is_image'  => true,
            'edit_mode' => !$this->isNew(),
            'delete_label' => 'check to remove',
            'template'  => '<div>%input%<br/><br/>%file%<br/><br/>%delete%<p>%delete_label%<br/><p></div>'
        ));

        $this->validatorSchema['photo'] = new sfValidatorFile(array(
            'required'   => true,
            'path'            => sfConfig::get('sf_upload_image_dir'),
            'mime_categories' => array('web_images' => array(
                'image/jpeg',
                'image/pjpeg',
                'image/png',
                'image/x-png'
            )),
            'mime_types' => 'web_images',
        ));

        $this->validatorSchema['photo_delete'] = new sfValidatorPass();
    }

注意我的upload_image_dir设置是%SF_UPLOAD_DIR%/images/

Symfony 将为您上传并保存图像!

然后,您可以根据您的需要重写doSave函数,将相同的函数放入您的PosterForm类中:

protected function doSave($con = null)
{
    $delete = $this->getValue('photo_delete');
    if ( $delete )
    {
        // ...
    }

    $upload = $this->getValue('photo');
    if ( $upload )
    {
        // ...
    }
    return parent::doSave($con);
}

最后删除您的图像,即文件,当您删除对象,即图像名称为字段的数据库记录,您必须将此代码放入您的模型类Poster中:

public function delete(PropelPDO $con = null) // using Propel
{
    $photo = sfConfig::get('sf_upload_image_dir').$this->getPhoto();
    if ( file_exists($photo) )
        unlink($photo);

    return parent::delete($con);
}

我希望这可以帮助您。

I hope to well focus about what you are looking for.

About $configuration and $helper, as you can find in cache\mypplication\dev\modules\autoMymodule\actions\actions.class.php the function preExecute() is used also to create them:

$this->configuration = new mymoduleGeneratorConfiguration();
$this->helper = new mymoduleGeneratorHelper();

These classes are in apps\docdesk\modules\mymodule\lib. You can find base classes in cache\mypplication\dev\modules\autoMymodule\lib. Have a look to these base classes to understand how are used and so which configuration functions and helper functions the admin generator makes available.

To deal with your new action, I don't know the feature you're developing so I'll try to imagine two possible scenarios. You can use only a form switching your functions of upload/create picture trough a custom widget, overriding the template _form.php, called in your snippet, and the new action and so on or, this is the way you're following, create a completely separate action, form and all needed templates. So in your makenewSucces.php you can include a template _makeform.php called with

<?php include_partial('poster/makeform', array('poster' => $poster, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper)) ?>

Of course you have to put the template _makeform.php in apps\docdesk\modules\mymodule\template as each new or overrided one.

I don't well understand your trouble about the image saving... I suppose you're asking for the saving chain of an image and how and where you can manage what you need.
To add the widget to upload an image inside your PosterForm class you can use a snippet like this where we suppose photo as widget name, according with a schema field photo: { type: varchar(255), required: true }, that is of course to customize:

    public function configure()
    {
        $photo = $this->getObject()->getPhoto(); // get the photo name
        $photo = sfConfig::get('sf_upload_image_dir').$photo;

        $this->widgetSchema['photo'] = new sfWidgetFormInputFileEditable(array(
            'label'     => 'Photo',
            'file_src'  => $photo,
            'is_image'  => true,
            'edit_mode' => !$this->isNew(),
            'delete_label' => 'check to remove',
            'template'  => '<div>%input%<br/><br/>%file%<br/><br/>%delete%<p>%delete_label%<br/><p></div>'
        ));

        $this->validatorSchema['photo'] = new sfValidatorFile(array(
            'required'   => true,
            'path'            => sfConfig::get('sf_upload_image_dir'),
            'mime_categories' => array('web_images' => array(
                'image/jpeg',
                'image/pjpeg',
                'image/png',
                'image/x-png'
            )),
            'mime_types' => 'web_images',
        ));

        $this->validatorSchema['photo_delete'] = new sfValidatorPass();
    }

Note that my setting for upload_image_dir is %SF_UPLOAD_DIR%/images/

Symfony will upload and save the image for you!

Then you can override the doSave function, according to your needs, putting the same one yet in your PosterForm class:

protected function doSave($con = null)
{
    $delete = $this->getValue('photo_delete');
    if ( $delete )
    {
        // ...
    }

    $upload = $this->getValue('photo');
    if ( $upload )
    {
        // ...
    }
    return parent::doSave($con);
}

Finally to remove your image, that is the file, when you delete the object, that is the db record where the image name is a field, you have to put this code in your model class Poster:

public function delete(PropelPDO $con = null) // using Propel
{
    $photo = sfConfig::get('sf_upload_image_dir').$this->getPhoto();
    if ( file_exists($photo) )
        unlink($photo);

    return parent::delete($con);
}

I hope this can help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文