gpt4 book ai didi

Symfony2 1 :M/1:1 Relationship and Sonata Admin Form

转载 作者:行者123 更新时间:2023-12-02 13:33:16 25 4
gpt4 key购买 nike

我已经把头撞在墙上无数个小时了,我希望这能有所帮助!

我有 Retailer、Branch 和 RetailerBranches 实体,它们运行得很好,零售商可以有许多分支机构,而一个分支机构只能有一个零售商。当试图让 Sonata Admin (SonataAdminBundle) 处理好这种关系时,困难的部分就发生了。最简单的形式是这样的:

零售商实体

    /**
* @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 连接表

    /**
* @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;

分支机构实体

    /**
* @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;

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

零售商管理员

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

问题:

所有这些工作,这是一个屏幕截图: enter image description here

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

问题 1:底部的“添加新”按钮尝试添加 RetailerBranches 对象而不是简单的 Branch 对象,这显然不起作用。

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

我觉得我已经接近解决方案了,但我只是无法完全实现。任何帮助将不胜感激!

最佳答案

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

.

关于Symfony2 1 :M/1:1 Relationship and Sonata Admin Form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15629225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com