Symfony备用新动作 [英] Symfony alternate new action

查看:80
本文介绍了Symfony备用新动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台服务器,我需要在其中存储一些图像.现在可以上传图片或即时创建图片,让我们说一下,只需在一些默认图片中添加一些文字即可(因此,我为此创建了一个文件MakenewForm.php).我数据库中的表将文件名存储在本地文件系统上.现在上传很简单,我可以使用默认的_new操作.为了创建图片,我做了一个新动作,说makenew. newmakenew都在列表视图上显示.我将newSuccess.php复制到了makenewSuccess.php.现在,我要为它们设置一组不同的提交按钮.我似乎无法弄清楚该怎么做.我所看到的就是这个:

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>

我不知道什么是$ configuration和什么$ helper.有人可以告诉我他们吗?我需要更改哪一个以及如何更改?

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?

此外,您可以推断,new动作的Submit动作只需要执行$form->save(),而makenew的Submit动作则需要接受所有文本输入并写入图像文件(比如说使用 imagejpeg )

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.

推荐答案

我希望很好地关注您要寻找的东西.

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

关于$configuration$helper,正如您在cache\mypplication\dev\modules\autoMymodule\actions\actions.class.php中可以找到的那样,功能preExecute()也可用于创建它们:

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();

这些类在apps\docdesk\modules\mymodule\lib中.您可以在cache\mypplication\dev\modules\autoMymodule\lib中找到基类.看一下这些基类,以了解如何使用它们,以及管理生成器提供哪些配置功能和帮助程序功能.

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.

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

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)) ?>

当然,您必须将模板_makeform.php作为每个新模板或替代模板放置在apps\docdesk\modules\mymodule\template中.

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

我不太了解您在保存图像方面的麻烦...我想您是在要求图像的保存链以及如何以及在何处可以管理所需的内容. 要添加小部件以在PosterForm类中上传图像,您可以使用如下代码段,其中假设photo作为小部件名称,并根据架构字段photo: { type: varchar(255), required: true }进行自定义: >

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();
    }

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

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

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

Symfony will upload and save the image for you!

然后,您可以根据需要覆盖doSave函数,将相同的函数放在PosterForm类中:

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);
}

最后要删除图像(即文件),当删除对象(即图像名称为字段的db记录)时,必须将此代码放入模型类Poster:

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.

这篇关于Symfony备用新动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆