php Doctrine Tabel Realtion Annotations(ManyToOne,OneToMany,ManyToMany)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Doctrine Tabel Realtion Annotations(ManyToOne,OneToMany,ManyToMany)相关的知识,希望对你有一定的参考价值。
<?php
namespace AppBundle\Entity;
/**
* @ORM\Entity
* @ORM\Table(name="tasks")
*/
class Task
{
...
/**
* @ORM\ManyToMany(targetEntity="Category", inversedBy="tasks")
* @ORM\JoinTable(name="categories",
* joinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="task_id", referencedColumnName="id")})
*/
protected $category;
...
}
/**
* @ORM\Entity
* @ORM\Table(name="categories")
*/
class Category
{
...
/**
* @ORM\ManyToMany(targetEntity="Task", mappedBy="category")
* @ORM\JoinTable(name="categories",
* joinColumns={@ORM\JoinColumn(name="task_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")})
*/
private $tasks;
...
<?php
namespace AppBundle\Entity;
use AppBundle\Entity\Presentation;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
/**
* Class User
* @package AppBundle\Entity
* @ORM\Entity
* @ORM\Table(name="event")
*/
class Event
{
/**
* @var Collection<Presentation>|null
*
* @ORM\OneToMany(
* targetEntity="AppBundle\Entity\Presentation",
* mappedBy="event",
* cascade={"persist"},
* orphanRemoval=true
* )
* This "ORM\JoinTable(inverseJoinColumns={ORM\JoinColumn(unique=true)})"
* is a fix for set the parent id into the presentation foreign field!
* @ORM\JoinTable(inverseJoinColumns={@ORM\JoinColumn(unique=true)})
*/
private $presentations;
/**
* Event constructor.
*/
public function __construct()
{
$this->presentations = new ArrayCollection();
}
/**
* Returns the Presentation
*
* @return Collection<Presentation>|null
*/
public function getPresentations(): Collection
{
return $this->presentations;
}
/**
* Add a Presentation
*
* @param Presentation $presentation
* @return void
*/
public function addPresentation(Presentation $presentation): void
{
if (!$presentation->getEvent() instanceof Event) {
$presentation->setEvent($this);
}
$this->presentations[] = $presentation;
}
/**
* Remove a Presentation
*
* @param Presentation $presentation
* @return void
*/
public function removePresentation(Presentation $presentation): void
{
$this->presentations->removeElement($presentation);
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Presentation
* @package AppBundle\Entity
* @ORM\Entity
* @ORM\Table(name="presentation")
* @ORM\Entity(repositoryClass="AppBundle\Repository\PresentationRepository")
*/
class Presentation
{
/**
* Location
*
* @ORM\ManyToOne(
* targetEntity="AppBundle\Entity\Location"
* )
* @ORM\JoinColumn(nullable=false)
* @var \AppBundle\Entity\Location
*/
protected $location;
/**
* Returns the Location
*
* @return Location|null
*/
public function getLocation()
{
return $this->location;
}
/**
* Sets the Location
*
* @param Location|null $location
* @return void
*/
public function setLocation($location)
{
$this->location = $location;
}
}
以上是关于php Doctrine Tabel Realtion Annotations(ManyToOne,OneToMany,ManyToMany)的主要内容,如果未能解决你的问题,请参考以下文章
PHP-Doctrine2: Items - Shops - ItemsAtShops - 如何使用Doctrine2方便地实现?
PHP Doctrine 初学者:找不到 Doctrine\ORM\Tools\Setup
PHP 将Zend Framework 1.10与Doctrine 2集成(使用Doctrine的Autoloader)
添加到许多工作表
php Doctrine(和Migrations)DBA
PHP Doctrine 使用鉴别器映射查询继承的类