doctrine - Symfony findOneBy/findBy

标签 doctrine symfony

有人在 Symfony 3(最后一个版本)中遇到过这个奇怪的问题吗?

我有以下简单的代码:

$repository = $this->getDoctrine()
                   ->getManager()
                   ->getRepository('GeneralRegistrationBundle:Service');

$service = $repository->findOneBy(array('name' => 'Registration'),array('name' => 'ASC'));

$comment = $service->getComment();
$name = $service->getName();

return new Response('le service is '. $name . ', content is ' . $comment);

此代码有效。

我清除缓存并将 findOneBy 更改为 findBy:

$service = $repository->findBy(array('name' => 'Registration'),array('name' => 'ASC'),1 ,0);

然后我有以下错误:

Error: Call to a member function getComment() on array

有人有想法或线索吗?

提前致谢。

最佳答案

findBy() 返回具有给定条件的对象数组。

如果没有找到,则返回一个空数组。如果只有一行满足您的条件,那么您可以在 $service 的最后添加 [0],如下所示:

$service = $repository->findBy(array('name' => 'Registration'),array('name' => 'ASC'),1 ,0)[0];

如果没有,您应该使用 foreach 或类似的东西循环找到的数组。

关于doctrine - Symfony findOneBy/findBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38815175/

相关文章:

php - 具有多对多关系的 Doctrine Two Entities 中的问题,cli 工具在创建模式时报告错误(SchemaException)

php - 带有 stof 可上传扩展名的 Symfony2 上传文件

symfony - FOSUserBundle 中组和角色的干净使用

php - Doctrine 控制台缺失 : "dest-path"

symfony - Composer 更新和 KnpPaginatorBundle 的运行时异常

php - Doctrine 集合 : difference between toArray() and getData()

mysql - 使用 Doctrine 保存行时 Doctrine_Connection_Mysql_Exception

php - Doctrine:如何根据与 Criteria 的关联按属性排序

php - Doctrine ORM : use interface as relation for different entities?

css - 为什么 symfony 表单的复选框看起来很奇怪?