Doctrine2 findBy 关系对象触发字符串转换错误 [英] Doctrine2 findBy relationship object triggers string conversion error

查看:20
本文介绍了Doctrine2 findBy 关系对象触发字符串转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 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));

这篇关于Doctrine2 findBy 关系对象触发字符串转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆