• Как создать связанные записи со связью many-to-many в zend framework, используя doctrine?

    @novrm
    Если поможет, если не поможет - звиняйте...
    Попробуйте вот эти инструменты:
    https://www.mysql.com/products/workbench/
    https://github.com/mysql-workbench-schema-exporter...

    На скорую руку автомат наваял следующие сущности для пробного модуля 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;
        }
    
    }
    Ответ написан
    Комментировать