gpt4 book ai didi

Symfony2 拒绝我的模型类中的自定义 findBy 函数

转载 作者:行者123 更新时间:2023-12-02 11:19:08 27 4
gpt4 key购买 nike

我遵循在 Doctrine 模型类中设置自定义 findOneByJoinedToCategory($id) 函数的示例,如此处文档中所述:

http://symfony.com/doc/current/book/doctrine.html

就我而言,我有一个名为 TestVendorCategory 的模型类,其中包含一堆属性和此函数:

public function findOneByNameJoinedToVendorCategoryMappings($vendorCategoryName)
{
$query = $this->getEntityManager()
->createQuery('
SELECT vc, vcm FROM TestCoreBundle:VendorCategory vc
JOIN vcm.vendorCategoryMapping vcm
WHERE vc.name = :name'
)->setParameter('name', $vendorCategoryName);

try
{
return $query->getSingleResult();
}
catch (\Doctrine\ORM\NoResultException $e)
{
return null;
}
}

在我的 Controller 中,我这样调用它:

$vendorCategoryMapping = $this->em->getRepository("TestCoreBundle:VendorCategory")->findOneByNameJoinedToVendorCategoryMappings($vendorCategoryName);

当我转到浏览器并使用此调用执行此操作时,我收到以下错误消息:

实体“Test\CoreBundle\Entity\VendorCategory”没有字段“nameJoinedToVendorCategoryMappings”。因此,您无法在实体存储库上调用“findOneByNameJoinedToVendorCategoryMappings”

看起来 Symfony 2.1 希望 findOneBy...() 方法仅反射(reflect)现有字段的名称,而不是自定义的“JoinedTo...”类型的方法。请问我是不是错过了什么?该文档显示了一个这样的示例,它应该可以工作。我正在使用注释,但是这个方法没有任何注释。谢谢!

最佳答案

您必须将 findOneByNameJoinedToVendorCategoryMappings 函数放入 VendorCategoryRepository 类中:

<?php
namespace Test\CoreBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;

class VendorCategoryRepository extends EntityRepository
{
public function findOneByNameJoinedToVendorCategoryMappings($vendorCategoryName)
{
$query = $this->getEntityManager()
->createQuery('
SELECT vc, vcm FROM TestCoreBundle:VendorCategory vc
JOIN vcm.vendorCategoryMapping vcm
WHERE vc.name = :name'
)->setParameter('name', $vendorCategoryName);

try
{
return $query->getSingleResult();
}
catch (NoResultException $e)
{
return null;
}
}
}

并在实体中链接此存储库类:

/**
* @ORM\Entity(repositoryClass="Test\CoreBundle\Entity\VendorCategoryRepository")
*/
class VendorCategory

关于Symfony2 拒绝我的模型类中的自定义 findBy 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15180321/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com