V
V
Viktor Krasnov2016-07-26 13:34:43
Doctrine ORM
Viktor Krasnov, 2016-07-26 13:34:43

How to create linked posts with many-to-many relationship in zend framework using doctrine?

Hello!
I am using ZF2, doctrine, many-to-many.
There is a Portfolio entity associated with a Tag entity.
When creating/editing a portfolio, you need to not only select from existing tags, but also be able to create new ones (using Select2).
Please advise if there is an adequate non-ugly way to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
novrm, 2016-07-26
@novrm

If it helps, if it doesn't help, call...
Try these tools:
https://www.mysql.com/products/workbench/ https
://github.com/mysql-workbench-schema-exporter...
The automaton has created the following entities for the test module TestModule:
b52390696a37424bb2aafa429bc25bf1.jpg

<?php

/**
 * Auto generated by MySQL Workbench Schema Exporter.
 * Version 3.0.2 (doctrine2-annotation) on 2016-07-26 19:31:50.
 * Goto https://github.com/johmue/mysql-workbench-schema-exporter for more
 * information.
 */

namespace TestModule\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * TestModule\Entity\Portfolio
 *
 * @ORM\Entity()
 * @ORM\Table(name="Portfolio")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"base":"BasePortfolio", "extended":"Portfolio"})
 */
class BasePortfolio
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     */
    protected $idPortfolio;

    /**
     * @ORM\ManyToMany(targetEntity="Tag", mappedBy="portfolios")
     */
    protected $tags;

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

    /**
     * Set the value of idPortfolio.
     *
     * @param integer $idPortfolio
     * @return \TestModule\Entity\Portfolio
     */
    public function setIdPortfolio($idPortfolio)
    {
        $this->idPortfolio = $idPortfolio;

        return $this;
    }

    /**
     * Get the value of idPortfolio.
     *
     * @return integer
     */
    public function getIdPortfolio()
    {
        return $this->idPortfolio;
    }

    /**
     * Add Tag entity to collection.
     *
     * @param \TestModule\Entity\Tag $tag
     * @return \TestModule\Entity\Portfolio
     */
    public function addTag(Tag $tag)
    {
        $this->tags[] = $tag;

        return $this;
    }

    /**
     * Remove Tag entity from collection.
     *
     * @param \TestModule\Entity\Tag $tag
     * @return \TestModule\Entity\Portfolio
     */
    public function removeTag(Tag $tag)
    {
        $this->tags->removeElement($tag);

        return $this;
    }

    /**
     * Get Tag entity collection.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTags()
    {
        return $this->tags;
    }

}

<?php

/**
 * Auto generated by MySQL Workbench Schema Exporter.
 * Version 3.0.2 (doctrine2-annotation) on 2016-07-26 19:31:50.
 * Goto https://github.com/johmue/mysql-workbench-schema-exporter for more
 * information.
 */

namespace TestModule\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * TestModule\Entity\Tag
 *
 * @ORM\Entity()
 * @ORM\Table(name="Tag")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"base":"BaseTag", "extended":"Tag"})
 */
class BaseTag
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     */
    protected $idTag;

    /**
     * @ORM\ManyToMany(targetEntity="Portfolio", inversedBy="tags")
     * @ORM\JoinTable(name="Portfolio_has_Tag",
     *     joinColumns={@ORM\JoinColumn(name="Tag_idTag", referencedColumnName="idTag", nullable=false)},
     *     inverseJoinColumns={@ORM\JoinColumn(name="Portfolio_idPortfolio", referencedColumnName="idPortfolio", nullable=false)}
     * )
     */
    protected $portfolios;

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

    /**
     * Set the value of idTag.
     *
     * @param integer $idTag
     * @return \TestModule\Entity\Tag
     */
    public function setIdTag($idTag)
    {
        $this->idTag = $idTag;

        return $this;
    }

    /**
     * Get the value of idTag.
     *
     * @return integer
     */
    public function getIdTag()
    {
        return $this->idTag;
    }

    /**
     * Add Portfolio entity to collection.
     *
     * @param \TestModule\Entity\Portfolio $portfolio
     * @return \TestModule\Entity\Tag
     */
    public function addPortfolio(Portfolio $portfolio)
    {
        $portfolio->addTag($this);
        $this->portfolios[] = $portfolio;

        return $this;
    }

    /**
     * Remove Portfolio entity from collection.
     *
     * @param \TestModule\Entity\Portfolio $portfolio
     * @return \TestModule\Entity\Tag
     */
    public function removePortfolio(Portfolio $portfolio)
    {
        $portfolio->removeTag($this);
        $this->portfolios->removeElement($portfolio);

        return $this;
    }

    /**
     * Get Portfolio entity collection.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getPortfolios()
    {
        return $this->portfolios;
    }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question