使用Sonata Admin和Propel添加相关项目

时间:2014-11-07 13:51:24

标签: symfony propel sonata-admin

我有Sonata Admin并运行使用Propel并定义了两个模型/ Admin类; PortfolioImage,其中投资组合项可以包含许多图片。

我有ImageAdmin允许上传图片,需要与投资组合项目相关联 在PortfolioAdmin中,我可以使用模型表单映射器类型将现有图像添加到项目组合项目中。

有没有办法在添加/编辑项目组合项目时添加添加/删除图像的功能,而只是选择现有项目,或者只是添加/删除相关项目的方法而不是删除图像对象我现在的情况。

我知道我可以选择为投资组合类编写自定义管理控制器,但有没有预先构建的方法来实现这种行为?

供参考,一些代码摘录我所做的事情;
schema.xml中

<database name="default" namespace="MyBundle\Model" defaultIdMethod="native">

    <table name="portfolio">
        <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
        <column name="title" type="varchar" primaryString="1" size="100" />
        <column name="description" type="LONGVARCHAR" />
        <behavior name="sluggable" />
        <behavior name="timestampable" />
        <behavior name="archivable" />
    </table>

    <table name="image">
        <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
        <column name="portfolio_id" type="integer" required="true" />
        <column name="title" type="varchar" primaryString="1" size="100" />
        <column name="path" type="varchar" size="255" />
        <column name="description" type="LONGVARCHAR" />
        <foreign-key foreignTable="portfolio">
            <reference local="portfolio_id" foreign="id"/>
        </foreign-key>
        <behavior name="sluggable" />
        <behavior name="timestampable" />
        <behavior name="archivable" />
    </table>

</database>

PortfolioAdmin.php

class PortfolioAdmin extends Admin
{

    protected $baseRouteName = 'portfolio';
    protected $baseRoutePattern = 'portfolio';

    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('title', 'text', array('label' => 'Title'))
            ->add('Images', 'model', array(
                    'class' => 'MyBundle\Model\Image',
                        'multiple' => true,
                        'expanded' => true, 
            ), array())
            ->add('description', 'text', array('label' => 'Description'))
        ;
    }
}

ImageAdmin.php

class ImageAdmin extends Admin
{

    protected $baseRouteName = 'image';
    protected $baseRoutePattern = 'image';

    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('title', 'text', array('label' => 'Title'))
            ->add('portfolio', 'sonata_type_model', array('label' => 'Job'))
            ->add('description', 'text', array('label' => 'Description'))
            ->add('file', 'file', array('required' => false))
        ;
    }
}

admin.yml

services:
    sonata.admin.portfolio:
        class: MyBundle\Admin\PortfolioAdmin
        tags:
            - { name: sonata.admin, manager_type: propel, group: "Content", label: "Portfolio" }
        arguments:
            - ~
            - MyBundle\Model\Portfolio
            - ~
        calls:
            - [ setTranslationDomain, [MyBundle]]
    sonata.admin.image:
        class: MyBundle\Admin\ImageAdmin
        tags:
            - { name: sonata.admin, manager_type: propel, group: "Images", label: "Portfolio Image" }
        arguments:
            - ~
            - MyBundle\Model\Image
            - ~
        calls:
            - [ setTranslationDomain, [MyBundle]]

1 个答案:

答案 0 :(得分:2)

您可以使用普通的旧collection字段类型。

鉴于以下ImageType

class ImageType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title', 'text');
        $builder->add('description', 'textarea');
        $builder->add('file', 'file', array('required' => false));
    }

    public function getName()
    {
        return 'image';
    }
}

PortfolioAdmin类成为:

class PortfolioAdmin extends Admin
{
    protected $baseRouteName = 'portfolio';
    protected $baseRoutePattern = 'portfolio';

    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('title', 'text', array('label' => 'Title'))
            ->add('images', 'collection', array(
                'type'         => new ImageType(),
                'by_reference' => false,
                'allow_add'    => true,
                'allow_delete' => true,
            ))
            ->add('description', 'text', array('label' => 'Description'))
        ;
    }
}