我有2个实体,ActivityType和ActivityNature,具有多对多单向关系,其中ActivityType是拥有实体:
<?php
namespace Evo\AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* ActivityType
*
* @ORM\Table(name="sf_activity_types")
* @ORM\Entity(repositoryClass="Evo\AppBundle\Entity\ActivityTypeRepository")
*/
class ActivityType
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="Evo\AppBundle\Entity\ActivityNature")
* @ORM\JoinTable(name="sf_activity_types_natures")
*/
protected $activityNatures;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
namespace Evo\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* ActivityNature
*
* @ORM\Table(name="sf_activity_natures")
* @ORM\Entity(repositoryClass="Evo\AppBundle\Entity\ActivityNatureRepository")
*/
class ActivityNature implements \JsonSerializable
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
我可以使用$ activityType-&gt; getActivityNatures()轻松获取与给定activityType相关的activityNatures; 但我无法找到有关如何从给定的activityNature获取activityTypes的任何线索。由于性能问题,我真的不想设置双向关系。任何帮助将不胜感激。
答案 0 :(得分:1)
您可以使用以下dql:
"SELECT at FROM AcmeBundle:ActivityType at WHERE :natureId MEMEBR OF at.activityNatures"
编辑:回答您的评论,获取ActivityNature:
"SELECT an FROM AcmeBundle:ActivityType at JOIN at.activityNatures an WHERE at.id = :activitiTypeId"