5

我的项目中有以下实体结构。

class MyEntity
{
 [... some more fields...]
 /**
 * @Type("array<string, string>")
 * @ORM\ManyToMany(targetEntity="Me\MyBundle\Entity\Example")
 * @ORM\JoinTable(name="example_myentity",
 *      joinColumns={@ORM\JoinColumn(name="plan_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="example", referencedColumnName="label")}
 * )
 */
private $example;
}



class Example
{

/**
 * @ORM\Id
 * @ORM\Column(name="label", type="string", length=50, unique=true, nullable=false)
 */
private $label;
}

当我尝试使用 Doctrine 中的 findby() 函数获取“$example”时,我收到以下通知:

未定义索引:joinColumns

我尝试调试了一下,问题似乎出在函数中的学说文件BasicEntityPersister.php中

 _getSelectEntitiesSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null),

我在堆栈跟踪中观察到第二个参数 "$assoc" 始终为 null,我认为这就是 Doctrine 不进行 JOIN 语句的原因。

任何想法?

谢谢

4

1 回答 1

4

我相信这是一个错误,可能是这个

我有同样的问题,目前唯一的解决方案似乎是等待 BasicEntityPersister 的更新或编写非常简单的查询。

首先将存储库添加到您的类

/**
 * MyClass
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\MyClassRepository")
 */
class MyClass
...

然后在您的存储库类中:

class MyClassRepository extends EntityRepository
{
    public function findByExample( Example $example )
    {
         return $this->getEntityManager()
                ->createQueryBuilder()
                ->select('m')
                ->from('AcmeDemoBundle:MyClass', 'm')
                ->innerJoin('m.examples', 'e')
                ->where('e.id = :exampleid' )
                ->setParameter('exampleid', $example->getId() )
                ->getQuery()
                ->getResult();
    }
}

最后,您可以通过以下方式获取与示例相关的所有 MyClass 实例:

$this->getDoctrine()->getManager()->getRepository('AcmeDemoBundle:MyClass')->findByExample( $example );
于 2014-02-09T13:32:09.793 回答