Doctrine不生成数据库模式ManyToMany关系

时间:2017-04-01 18:33:15

标签: doctrine-orm console

Doctrine不会为ManyToMany关系生成数据库架构。

c:\ Bitnami \ wampstack-5.6.30-0 \ apache2 \ htdocs \ typejoy.biz> php vendor \ doctrine \ orm \ bin \ doctrine orm:schema-tool:create

为两个实体testChi和testPar创建两个表,但不创建@JoinTable“tParChiMTM”。

ENtity testChi

<?php

namespace LogBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Index;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\JoinTable;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\ManyToMany;
use LogBundle\Entity\testPar;


/** 
 * @Table(name="ttestchi")
 * @Entity()
 */ 
class testChi {

    /**
     * @var integer
     *
     * @Column(name="id", type="integer")
     * @Id
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;
/** @Column ( name="name", type="string", length=50, unique=false, nullable=true) */
     private $name; 

/** @ManyToMany ( targetEntity="testPar", mappedBy="chiMTM") */
     private $parMTM; 


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

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }                     
    /**
     * Get name
     *
     * @return integer
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set name
     * @param integer $name
     * @return name
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }    



  //make you have to serialize, deserialize entity here ??? 
    /**
     * Get parMTM
     *
     * @return LogBundle\Entity\ParMTM
     */
    public function getParMTM()
    {
        return $this->parMTM;
    }

    /**
     * Remove parMTM
     *
     * @param \LogBundle\Entity\testPar $parMTM
     */
    public function removeParMTM (\LogBundle\Entity\testPar $parMTM)
    {
        if ( $this->hasParMTM($parMTM) ) { 
            $this->parMTM->removeElement($parMTM);
        }
    }

    /**
     * Add parMTM
     *
     * @param \LogBundle\Entity\testPar $parMTM
     *
     * @return ParMTM
     */
    public function addParMTM(\LogBundle\Entity\testPar $parMTM)
    {
        if ( !$this->hasParMTM($parMTM) ) { 
            $this->parMTM[] = $parMTM;
        }

        return $this;
    }

    /**
     * @param \LogBundle\Entity\testPar $parMTM
     * @return bool
     */
    public function hasParMTM($parMTM)
    {
        if( $this->getParMTM() ) {
            return $this->getParMTM()->contains($parMTM);
        }
    }   



    /**
     * Return Entity as string  
     * 
     * @return string String representation of this class
     */
    public function __toString()
    {
        return strval($this->id);
    }

 } 

实体testPar

<?php

namespace LogBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Index;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\JoinTable;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\ManyToMany;
use LogBundle\Entity\testChi;


/** 
 * @Table(name="ttestpar")
 * @Entity()
 */ 
class testPar {

    /**
     * @var integer
     *
     * @Column(name="id", type="integer")
     * @Id
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;
/** @Column ( name="name", type="string", length=50, unique=false, nullable=true) */
     private $name; 

/** @ManyToMany ( targetEntity="testChi", inversedBy="parMTM") */
/** @JoinTable ( name="tParChiMTM") */
     private $chiMTM; 


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

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }                     
    /**
     * Get name
     *
     * @return integer
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set name
     * @param integer $name
     * @return name
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }    



  //make you have to serialize, deserialize entity here ??? 
    /**
     * Get chiMTM
     *
     * @return LogBundle\Entity\ChiMTM
     */
    public function getChiMTM()
    {
        return $this->chiMTM;
    }

    /**
     * Remove chiMTM
     *
     * @param \LogBundle\Entity\testChi $chiMTM
     */
    public function removeChiMTM (\LogBundle\Entity\testChi $chiMTM)
    {
        if ( $this->hasChiMTM($chiMTM) ) { 
            $this->chiMTM->removeElement($chiMTM);
        }
    }

    /**
     * Add chiMTM
     *
     * @param \LogBundle\Entity\testChi $chiMTM
     *
     * @return ChiMTM
     */
    public function addChiMTM(\LogBundle\Entity\testChi $chiMTM)
    {
        if ( !$this->hasChiMTM($chiMTM) ) { 
            $this->chiMTM[] = $chiMTM;
        }

        return $this;
    }

    /**
     * @param \LogBundle\Entity\testChi $chiMTM
     * @return bool
     */
    public function hasChiMTM($chiMTM)
    {
        if( $this->getChiMTM() ) {
            return $this->getChiMTM()->contains($chiMTM);
        }
    }   



    /**
     * Return Entity as string  
     * 
     * @return string String representation of this class
     */
    public function __toString()
    {
        return strval($this->id);
    }

 } 

c:\ Bitnami \ wampstack-5.6.30-0 \ apache2 \ htdocs \ typejoy.biz&gt; php vendor \ doctrine \ orm \ bin \ doctrine orm:schema-tool:create

创建两个表,但不创建@JoinTable“tParChiMTM”。

1 个答案:

答案 0 :(得分:0)

原因在于注释:

CORRECT

/** 
 * @ManyToMany ( targetEntity="testPar", mappedBy="chiMTM") */

/**
 *  @ManyToMany ( targetEntity="testChi", inversedBy="parMTM") 
 * ( name="tParChiMTM") */
     private $chiMTM; 

您无法使用/**开始每一行,只有第一行应包含此行,后续行只能以单*开头。

WRONG:

/** @ManyToMany ( targetEntity="testChi", inversedBy="parMTM") */
/** @JoinTable ( name="tParChiMTM") */