Symfony2 1:M/1:1 关系和奏鸣曲管理表 [英] Symfony2 1:M / 1:1 Relationship and Sonata Admin Form

查看:22
本文介绍了Symfony2 1:M/1:1 关系和奏鸣曲管理表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用头撞墙了无数个小时,希望 SO 能帮上忙!

I've hitting my head against the wall for countless hours now and I hope SO can be of help!

我有 Retailer、Branch 和 RetailerBranches 实体,它们运行良好,零售商可以拥有多个分支机构,而一个分支机构只能拥有一个零售商.当试图让 Sonata Admin (SonataAdminBundle) 与这种关系很好地相处时,困难的部分发生了.在最简单的形式中,它们如下所示:

I have Retailer, Branch and RetailerBranches entities which work just fine, retailers can have many branches and a branch can only have one retailer. The hard part happens when trying to make Sonata Admin (SonataAdminBundle) play nice with that relationship. In their simplest form, they look like this:

零售商实体

    /**
     * @ORMColumn(name="ID", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * Relation
     * 
     * @ORMOneToMany(targetEntity="RetailerBranches", mappedBy="Retailer", cascade={"persist"})
     */
    protected $branches;

    public function __construct() {
        $this->branches = new ArrayCollection();
    }

RetailerBranches 连接表

    /**
     * @ORMColumn(name="ID", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORMJoinColumn(name="Retailer_ID", referencedColumnName="ID", nullable=false)
     * @ORMManyToOne(targetEntity="Retailer", inversedBy="branches")
     */
    private $retailer;

    /**
     * @ORMJoinColumn(name="Branch_ID", referencedColumnName="ID", nullable=false, unique=true)
     * @ORMOneToOne(targetEntity="Branch", inversedBy="retailer")
     */
    private $branch;

分支实体

    /**
     * @ORMColumn(name="ID", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * Relation
     * 
     * @ORMOneToOne(targetEntity="RetailerBranches", mappedBy="branch", cascade={"persist"})
     */
    private $retailer;

更难的部分发生在尝试生成表单以允许这种关系形成时:

The harder part happens when trying generate the form to allow that relationship to take shape:

零售商管理员

protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('Branches')
                ->add('branches', 'sonata_type_collection', array(
                    'required' => false,
                    'by_reference' => false
                ), array(
                    'edit' => 'inline',
                    'inline' => 'table',
                ))
                ->end()
        ;
    }

RetailerBranchesAdmin

protected function configureFormFields(FormMapper $formMapper)
    {
        if ($this->hasRequest()) {
            $link_parameters = array('context' => $this->getRequest()->get('context'));
        } else {
            $link_parameters = array();
        }

        $formMapper
            ->add('succursale', 'sonata_type_model_list', array(
                'class' => 'VeloRetailerBundle:Branch',
                'required' => false,
            ), array(
                'edit' => 'inline',
                'inline' => 'table',
            ))
        ;
    }

问题:

所有这些工作,这里是一个屏幕截图:

All this sort of works, here's a screenshot:

有一家零售商及其分支机构.耶.

There's a Retailer and its Branches. Yay.

问题 1:底部的添加新"按钮试图添加一个 RetailerBranches 对象,而不是一个显然不起作用的简单 Branch 对象.

Problem 1: The "Add new" button at the bottom attempts to add a RetailerBranches object instead of a simple Branch object which obviously doesn't work.

问题 2:此方法也不允许用户内联修改分支.

Problem 2: This method also doesn't allow the user to modify a Branch inline.

我觉得我已经接近解决方案了,但我无法完全到达那里.任何帮助将不胜感激!

I feel like I'm close to the solution, but I just cannot quite get there. Any help would be greatly appreciated!

推荐答案

对于那些遇到同样问题的人,我将解决方案发布在 GitHub

For those running into the same problem, I posted the solution on GitHub

.

这篇关于Symfony2 1:M/1:1 关系和奏鸣曲管理表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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