Doctrine2 findBy 关系对象触发字符串转换错误

2022-01-03 00:00:00 php doctrine-orm

假设我在 Doctrine2 中有两个相互关联的实体,ModelsUser 和 ModelsComment.如果我在 Doctrine 2.0.0 中这样做...

Say I have two entities in Doctrine2 that are related to each other, ModelsUser and ModelsComment. If I do this in Doctrine 2.0.0...

<?php
// $em instanceof EntityManager, $user instanceof ModelsUser
$comments = $em->getRepository('ModelsComment')
    ->findBy(array('user' => $user, 'public' => true));

...我收到一个 PHP 错误:

...I get a PHP error:

严重性:注意

消息:ModelsUser 类对象到字符串的转换

Message: Object of class ModelsUser to string conversion

文件名:DBAL/Connection.php

Filename: DBAL/Connection.php

行号:574

这不应该发生,对吧?如果我使用 QueryBuilder 和 setParameter('user', $user) 它会按预期工作.

This shouldn't happen, right? If I use the QueryBuilder and setParameter('user', $user) it works as expected.

推荐答案

允许按关系查询,但必须在那里传递标识符.尚不支持按对象查询,只会在 2.1 中实现.

Query by relationship is allowed, but you have to pass the Identifier in there. Query by object is not yet supported and will only make it into 2.1.

<?php
// $em instanceof EntityManager, $user instanceof ModelsUser
$comments = $em->getRepository('ModelsComment')
->findBy(array('user' => $user->getId(), 'public' => true));

相关文章