4

我有这个模型:

/** @Entity @Table(name="articles") */
class Article {
    /** @Id @GeneratedValue @Column(type="integer") */
    protected $id;

    /** @Column(type="string", length=100, nullable=true) */
    protected $title;

    /** @ManyToOne(targetEntity="User", inversedBy="articles") */   
    protected $author;

    /** @Column(type="datetime") */
    protected $datetime;

    /**
     * @ManyToMany(targetEntity="Game", inversedBy="articles")
     * @JoinTable(name="articles_games",
     *      joinColumns={@JoinColumn(name="article_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="game_id", referencedColumnName="id")}
     *      )
     */
    protected $games;

    # Constructor
    public function __construct() {
        $this->datetime = new DateTime();
        $this->games = new \Doctrine\Common\Collections\ArrayCollection();
    }

    # ID
    public function getId() { return $this->id; }

    # TITLE
    public function setTitle($v) { $this->title = $v; }
    public function getTitle() {
        if(empty($this->title)) {
            $game = $this->getFirstGame();
            return ($game instanceof Game) ? $game->getTitle() : NULL;
        } else 
            return $this->title;
    }

    # AUTHOR
    public function setAuthor($v) { $this->author = $v; }
    public function getAuthor() { return $this->author; }

    # DATE & TIME
    public function getDateTime() { return $this->datetime; }
    public function setDateTime($v) { $this->datetime = $v; }

    # GAMES
    public function setGames($value) {
        $except_txt = 'Jedna z przesłanych wartości nie jest instancją klasy Game!';

        if(is_array($value)) {
            foreach($value as $v) {
                if($v instanceof Game) $this->games->add($v);
                else throw new Exception($except_txt);
            }
        } else {
            if($value instanceof Game) $this->games->add($value);
            else throw new Exception($except_txt);
        }
    }
    public function getGames() { return $this->games; }
}

如何使查询看起来像这样

SELECT a FROM Article a WHERE :game_id IN a.games

我有这个($game->getId()是一个整数)

$articles = $db->createQuery("SELECT a.type FROM Article a WHERE :game_id IN a.games GROUP BY a.type")->setParameter('game_id', $game->getId())->getResult();

但它返回给我一个语法错误

[Syntax Error] line 0, col 47: Error: Expected Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS, got 'a'
4

3 回答 3

14

这个问题与我刚刚回答的一个更新的问题相关联,我觉得把它放在这里也是有益的,因为它是一个更正确的解决方案:

Doctrine函数需要语句后的IN格式。不幸的是,它并不意味着列条件来证明成员资格。(1, 2, 3, 4, ...)IN

但是,我相信您正在寻找MEMBER OFDoctrine 功能:

SELECT a FROM Article a WHERE :game_id MEMBER OF a.games

你可以传递一个有效的 Doctrine 对象或标识符来game_id使用这个功能。

这个例子隐藏在Doctrine 文档的深处:

$query = $em->createQuery('SELECT u.id FROM CmsUser u WHERE :groupId MEMBER OF u.groups');
$query->setParameter('groupId', $group);
$ids = $query->getResult();
于 2014-11-17T15:43:00.033 回答
7

如果您正在寻找与某款游戏相关的文章:

$articles = $db->createQuery("SELECT a FROM Article a JOIN a.games game WHERE game.id = :game_id")
    ->setParameter('game_id', $game->getId())
    ->getResult();

或多个:

$articles = $db->createQuery("SELECT a FROM Article a JOIN a.games game WHERE game.id IN (?,?, ... ?)")
    ->setParameters(array($game1->getId(), $game2->getId() ... $gameN->getId()))
    ->getResult();
于 2012-04-23T18:21:58.567 回答
1

我想您需要为此创建一个自定义存储库。我刚刚解决了这样的问题。

使用 Doctrine\ORM\EntityRepository;

class Company extends EntityRepository
{
    /**
     * Load by product.
     * @param int $productId
     * @return array
     */
    public function getByProduct($productId)
    {
        $dql = "SELECT i FROM Domain\Model\Company i JOIN i.products p WHERE p.id = :id";
        return $this->_em->createQuery($dql)->setParameter(':id', $productId)->getResult();
    }
}
于 2012-04-23T12:24:47.673 回答