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

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

问题描述

说我在Doctrine2中有两个相互关联的实体,即Models\User和Models\Comment。如果我在Doctrine 2.0.0中这样做...

 <?php 
// $ em instanceof EntityManager ,$ user instanceof Models\User
$ comments = $ em-> getRepository('Models\Comment')
- > findBy(array('user'=> $ user,' public'=> true));

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


严重性:通知



消息:类的对象\用于字符串转换



文件名:DBAL / Connection.php



行号:574



$ b $这不应该发生,对吗?如果我使用QueryBuilder和setParameter('user',$ user),它的工作原理如下。

解决方案

,但你必须通过标识符在那里。按对象查询尚未得到支持,只会使其成为2.1。

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


Say I have two entities in Doctrine2 that are related to each other, Models\User and Models\Comment. If I do this in Doctrine 2.0.0...

<?php
// $em instanceof EntityManager, $user instanceof Models\User
$comments = $em->getRepository('Models\Comment')
    ->findBy(array('user' => $user, 'public' => true));

...I get a PHP error:

Severity: Notice

Message: Object of class Models\User to string conversion

Filename: DBAL/Connection.php

Line Number: 574

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

解决方案

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 Models\User
$comments = $em->getRepository('Models\Comment')
->findBy(array('user' => $user->getId(), 'public' => true));

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

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