Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

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

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

    /**
     * @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 join table

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

Branch entity

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

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: enter image description here

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...