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 LogBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMappingEntity;
use DoctrineORMMappingTable;
use DoctrineORMMappingIndex;
use DoctrineORMMappingId;
use DoctrineORMMappingGeneratedValue;
use DoctrineORMMappingColumn;
use DoctrineORMMappingJoinTable;
use DoctrineORMMappingOneToOne;
use DoctrineORMMappingOneToMany;
use DoctrineORMMappingManyToOne;
use DoctrineORMMappingManyToMany;
use LogBundleEntitytestPar;
/**
* @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 LogBundleEntityParMTM
*/
public function getParMTM()
{
return $this->parMTM;
}
/**
* Remove parMTM
*
* @param LogBundleEntitytestPar $parMTM
*/
public function removeParMTM (LogBundleEntitytestPar $parMTM)
{
if ( $this->hasParMTM($parMTM) ) {
$this->parMTM->removeElement($parMTM);
}
}
/**
* Add parMTM
*
* @param LogBundleEntitytestPar $parMTM
*
* @return ParMTM
*/
public function addParMTM(LogBundleEntitytestPar $parMTM)
{
if ( !$this->hasParMTM($parMTM) ) {
$this->parMTM[] = $parMTM;
}
return $this;
}
/**
* @param LogBundleEntitytestPar $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);
}
}
实体测试Par
<?php
namespace LogBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMappingEntity;
use DoctrineORMMappingTable;
use DoctrineORMMappingIndex;
use DoctrineORMMappingId;
use DoctrineORMMappingGeneratedValue;
use DoctrineORMMappingColumn;
use DoctrineORMMappingJoinTable;
use DoctrineORMMappingOneToOne;
use DoctrineORMMappingOneToMany;
use DoctrineORMMappingManyToOne;
use DoctrineORMMappingManyToMany;
use LogBundleEntitytestChi;
/**
* @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 LogBundleEntityChiMTM
*/
public function getChiMTM()
{
return $this->chiMTM;
}
/**
* Remove chiMTM
*
* @param LogBundleEntitytestChi $chiMTM
*/
public function removeChiMTM (LogBundleEntitytestChi $chiMTM)
{
if ( $this->hasChiMTM($chiMTM) ) {
$this->chiMTM->removeElement($chiMTM);
}
}
/**
* Add chiMTM
*
* @param LogBundleEntitytestChi $chiMTM
*
* @return ChiMTM
*/
public function addChiMTM(LogBundleEntitytestChi $chiMTM)
{
if ( !$this->hasChiMTM($chiMTM) ) {
$this->chiMTM[] = $chiMTM;
}
return $this;
}
/**
* @param LogBundleEntitytestChi $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>php vendor\doctrine\orm\bin\doctrine orm:schema-tool:create
创建两个表,但不创建@JoinTable"tParChiMTM"。
注释中:
正确
/**
* @ManyToMany ( targetEntity="testPar", mappedBy="chiMTM") */
/**
* @ManyToMany ( targetEntity="testChi", inversedBy="parMTM")
* ( name="tParChiMTM") */
private $chiMTM;
不能每行都以/**
开头,只有第一行才能包含这个,后续的行只能以单行*
开头。
错:
/** @ManyToMany ( targetEntity="testChi", inversedBy="parMTM") */
/** @JoinTable ( name="tParChiMTM") */