Doctrine 2 ORM Zend 2 Form Many to Many Example

房东的猫 提交于 2019-12-11 03:42:30

问题


Anyone have a good and complete example for many to many relation using ZF2 and Doctrine 2, especially when using ObjectMultiCheckBox ? I found this tutorial - https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md but it don't explain how to do a many to many relation.


回答1:


K so I figured out how todo this eventually after realizing the hydrator wasn't binding associations and I had to create the link. I'm going to put a full blog post up explaining it but in case you're still stuck. If you want to look at all the code together I've got it up on github (link) mind you I'm actively learning/developing/cleaning it up so its a bit messy

Basically you need to collect the selected models, create the association link by adding them to the entity, then persist the entity WITH the cascade add/del active on Doctrine (or manually do it by persisting the link's before the entity).

Below example is a Many-to-Many between my Posts and Categories

You need to have cascade active on your entity's property

/**
 * @ORM\OneToMany(targetEntity="CategoryPostAssociation", mappedBy="category",  cascade={"persist", "remove"})
 */
protected $category_post_associations;

You need to push the object manager from your Form to your fieldsets

PostFieldSet

$categoryFieldset = new CategoryFieldset($objectManager);
    $this->add(array(
        'type'    => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
        'name' => 'categories',
        'options' => array(
            'label' => 'Select Categories',
            'object_manager' => $objectManager,
            'should_create_template' => true,
            'target_class'   => 'OmniBlog\Entity\Category',
            'property'       => 'title',
            'target_element' => $categoryFieldset,
          ),
    ));

and categoryfieldset just has a textbox of the Title.

In my PostController's AddAction

public function addAction() {
    // Get your ObjectManager
    $objectManager = $this->getEntityManager();

    //Create the form and inject the ObjectManager
    //Bind the entity to the form
    $form = new PostForm($objectManager);
    $post = new Post();
    $form->bind($post);

    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setData($request->getPost());

        if ($form->isValid()) {
            /*
            * Get IDs from form element
            * Get categories from the IDs
            * add entities to $post's categories list
            */
            $element = $form->getBaseFieldset()->get('categories'); //Object of: DoctrineModule\\Form\\Element\\ObjectMultiCheckbox
            $values = $element->getValue();

            foreach($values as $catID){
                $results = $objectManager->getRepository('OmniBlog\Entity\Category')->findBy(array('id' => $catID));
                $catEntity = array_pop($results);                   
                $link = $post->addCategory($catEntity);
                //Entity/Post 's association table cascades persists and removes so don't need to persist($link), but would be done here
            }

            $objectManager->persist($post);
            $objectManager->flush();

            return $this->redirect()->toRoute(
                static::ROUTE_CHILD,
                array('controller' => static::CONTROLLER_NAME
            ));
        }
    }
    return array('form' => $form);
}

Above if you see $post->addCategory($catEntity); this leads to my Entity or Model managing the linking (I was passing back the link encase I want to handle the cascading manually)

Post

    /**
 * Returns the created $link
 */
public function addCategory(Category $category){
    $link = new CategoryPostAssociation();
    $link->setCategory($category);
    $link->setPost($this);
    $this->addCategoryPostAssociations($link);
    return $link;
}



回答2:


Found an good blog where it is explained step by step what is need to be done - http://laundry.unixslayer.pl/2013/zf2-quest-zendform-many-to-many/



来源:https://stackoverflow.com/questions/18723340/doctrine-2-orm-zend-2-form-many-to-many-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!