使用具有多对多关系的EntityRepository :: findBy()将导致Doctrine中的E_NOTICE

dev*_*eep 32 php doctrine many-to-many database-relations symfony

对于Symfony2项目,我必须在博客文章和所谓的平台之间建立关系.平台根据您用于查看站点的域定义特定过滤器.例如:如果您通过url first-example.com加入该网站,该网站将仅提供与此特定平台相关联的博客帖子.

为此,我创建了两个实体Post和Platform.之后我将它们与多对多关系映射在一起.我正试图通过findBy()Doctrines中内置函数的多对多关系来检索数据EntityRepository.

// every one of these methods will throw the same error
$posts = $postRepo->findBy(array('platforms' => array($platform)));
$posts = $postRepo->findByPlatforms($platform);
$posts = $postRepo->findByPlatforms(array($platform));
Run Code Online (Sandbox Code Playgroud)

实体和现有对象$postRepo的正确存储库在哪里. 无论哪种方式:我最终得到以下错误:Post$platformPlatform

ErrorException: Notice: Undefined index: joinColumns in [...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1495

[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1495
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1452
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1525
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1018
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:842
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:157
[...]/src/Foobar/BlogBundle/Tests/ORM/PostTest.php:102
Run Code Online (Sandbox Code Playgroud)

是否有可能以这种方式以多对多关系检索相关的entites,或者我被迫自己编写这些函数?奇怪的是:Doctrine不会抛出任何错误:"这不可能.",但是内部E_NOTICE.这就是为什么我认为它应该是可能的,但我在这里错过了一些观点.

剥离到有趣的部分,这两个实体看起来像这样.

<?php

namespace Foobar\CommunityBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

// [...] other namespace stuff

/**
 * @ORM\Entity(repositoryClass="Foobar\CommunityBundle\Entity\Repository\PlatformRepository")
 * @ORM\Table(name="platforms")
 */
class Platform
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    // [...] other field stuff
}
Run Code Online (Sandbox Code Playgroud)
<?php

namespace Foobar\BlogBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

// [...] other namespace stuff

/**
 * @ORM\Entity(repositoryClass="Foobar\BlogBundle\Entity\Repository\PostRepository")
 * @ORM\Table(name="posts")
 */
class Post implements Likeable, Commentable, Taggable, PlatformAware
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="Foobar\CommunityBundle\Entity\Platform", cascade={"persist"})
     * @ORM\JoinTable(name="map_post_platform",
     *      joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="platform_id", referencedColumnName="id")}
     *      )
     */
    protected $platforms;

    // [...] other fields

    /**
     * Constructor
     */
    public function __construct()
    {
        // [...]
        $this->platforms  = new ArrayCollection();
    }
}
Run Code Online (Sandbox Code Playgroud)

当然还有composer.json文件(以及剥离到相关行)

{
    [...]
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.1.*",
        "doctrine/orm": ">=2.2.3,<2.4-dev",
        "doctrine/doctrine-bundle": "1.0.*",
        "doctrine/doctrine-fixtures-bundle": "dev-master",
        [...]

    },
    [...]
}
Run Code Online (Sandbox Code Playgroud)

jhv*_*ras 35

另一种方式,可能有点OO /清洁而不使用ID:

public function getPosts(Platform $platform)
{
    $qb = $this->createQueryBuilder("p")
        ->where(':platform MEMBER OF p.platforms')
        ->setParameters(array('platform' => $platform))
    ;
    return $qb->getQuery()->getResult();
}
Run Code Online (Sandbox Code Playgroud)

一个更好的方法名称 findPostsByPlatform

  • 如果您关心性能,那么不要使用QueryBuilder,也不要使用ORM. (2认同)

Lig*_*art 27

这很有可能,但Stock Doctrine Repository不能以这种方式工作.

根据您的具体情况,您有两种选择:

在存储库中编写自定义方法.

class PostRepository extends EntityRepository
{
  public function getPosts($id)
  {
    $qb = $this->createQueryBuilder('p');
    $qb->join('p.platform', 'f')
       ->where($qb->expr()->eq('f.id', $id));
    return $qb;
  }
}
Run Code Online (Sandbox Code Playgroud)

或者在平台对象中使用默认的getter方法.

$posts = $platform->getPosts();
Run Code Online (Sandbox Code Playgroud)

你"去掉了有趣的部分",所以如果你有这种方法并不明显,但它通常是在制作的

app/console doctrine:generate:entities
Run Code Online (Sandbox Code Playgroud)