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

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

问题描述

我已经把我的头撞在墙上无数小时了,希望这样可以帮忙!



我有零售商,分公司和零售商分行实体好的,零售商可以有很多分行,一个分行只能有一个零售商。当尝试使Sonata Admin(SonataAdminBundle)与该关系发挥出色时,很难发生。最简单的形式就是这样:



零售商实体

  / ** 
* @ ORM\Column(name =ID,type =integer,nullable = false)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =IDENTITY)
* /
private $ id;

/ **
*关系
*
* @ ORM\OneToMany(targetEntity =RetailerBranches,mappedBy =Retailer,cascade = {persist })
* /
protected $ branches;

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

零售商分支连接表

  / ** 
* @ ORM\Column(name =ID,type =integer,nullable = false)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =IDENTITY)
* /
private $ id;

/ **
* @ ORM\JoinColumn(name =Retailer_ID,referencedColumnName =ID,nullable = false)
* @ ORM\ManyToOne(targetEntity =零售商,inversedBy =branches)
* /
private $ retailer;

/ **
* @ ORM\JoinColumn(name =Branch_ID,referencedColumnName =ID,nullable = false,unique = true)
* @ ORM\\ \\OneToOne(targetEntity =Branch,inversedBy =retailer)
* /
private $ branch;

分支机构

  / ** 
* @ ORM\Column(name =ID,type =integer,nullable = false)
* @ ORM\\ \\ id
* @ ORM\GeneratedValue(strategy =IDENTITY)
* /
private $ id;

/ **
*关系
*
* @ ORM\OneToOne(targetEntity =RetailerBranches,mappedBy =branch,cascade = {persist })
* /
私人零售商;

当尝试生成表单以使该关系形成时,更难的部分发生:



RetailerAdmin

  protected function configureFormFields(FormMapper $ formMapper )
{
$ formMapper
- > with('Branches')
- > add('branches','sonata_type_collection',array(
'required '=> false,
'by_reference'=> false
),array(
'edit'=>'inline',
'inline'=&表',
))
- > 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',
))
;
}

问题:



所有这些作品,这里是一个截图:



有零售商及其分支机构。



问题1:底部的添加新按钮会尝试添加一个RetailerBranches对象,而不是一个明显不起作用的简单Branch对象。 p>

问题2:此方法也不允许用户在线修改分支。



我觉得我接近解决方案,但我根本不能到达那里。任何帮助将不胜感激!

解决方案

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




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

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:

Retailer entity

    /**
     * @ORM\Column(name="ID", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

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

RetailerBranches join table

    /**
     * @ORM\Column(name="ID", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORM\JoinColumn(name="Retailer_ID", referencedColumnName="ID", nullable=false)
     * @ORM\ManyToOne(targetEntity="Retailer", inversedBy="branches")
     */
    private $retailer;

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

Branch entity

    /**
     * @ORM\Column(name="ID", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

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

RetailerAdmin

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

The problem:

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

There's a Retailer and its Branches. Yay.

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.

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!

解决方案

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

.

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

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