Doctrine2 findBy relationship object triggers string conversion error(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 关系对象触发字符串转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Doctrine2 findBy 关系对象触发字符串转换错误


基础教程推荐
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01