Symfony2 - 方法名称必须以findBy或findOneBy开头
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Symfony2 - 方法名称必须以findBy或findOneBy开头相关的知识,希望对你有一定的参考价值。
首先让我说,我知道之前出现了这个问题,但没有一个解决方案似乎有效。而且,我也不是新成员,我记不起我的登录信息,所以我不得不重新注册....
基本上,我的工作是重新整理现有的Symfony2项目,该项目从未完成,并没有很好地整合在一起。但是,由于某些奇怪的原因,我在尝试在名为Customer的存储库中调用方法“count”时突然遇到上述错误。
这是Repo中的方法(请记住,我的目录不称为Acme,为简单起见,它仅在此处替换):
public function count($type)
{
return $this->getEntityManager()
->createQuery('SELECT COUNT(c) FROM AcmePagesBundle:customer c WHERE c.status = :type')
->setParameter('type', $type)
->getSingleScalarResult();
}
它是从主枝模板调用的,如下所示:
{{ render(controller('AcmeCustomerBundle:Customer:count', {'type':'private'})) }}
运行应用程序时收到的错误是:
在第30行的AcmePagesBundle :: main.html.twig中,在呈现模板期间抛出了异常(“未定义的方法'count'。方法名称必须以findBy或findOneBy!”开头。)。
我正在使用Symfony的最新版本,我认为它是2.7。我确定它之前有效,但在清除缓存并重新生成实体之后,它似乎已经搞砸了。我已经尝试了大多数其他建议,但现在我不知所措。
如果有人能对此有所了解,我会非常感激,谢谢。
迈克尔
编辑:
CustomerRepository.php文件:
namespace AcmePagesBundleEntityRepository;
use DoctrineORMEntityRepository;
class CustomerRepository extends EntityRepository
{
public function countAll()
{
return $this->getEntityManager()
->createQuery('SELECT COUNT(c) FROM AcmePagesBundle:Customer c')
->getSingleScalarResult();
}
public function count($type)
{
return $this->getEntityManager()
->createQuery('SELECT COUNT(c) FROM AcmePagesBundle:Customer c WHERE c.status = :type')
->setParameter('type', $type)
->getSingleScalarResult();
}
public function reverseOrder()
{
return $this->getEntityManager()
->createQuery('SELECT c FROM AcmePagesBundle:Customer c ORDER BY c.id DESC')
->getResult();
}
public function lastTen()
{
return $this->getEntityManager()
->createQuery('SELECT c FROM AcmePagesBundle:Customer c ORDER BY c.id DESC')
->setMaxResults(10)
->getResult();
}
public function getCustomerByDefaultAddress($id)
{
return $this->getEntityManager()
->createQuery('SELECT c FROM AcmePagesBundle:Customer c WHERE c.defaultaddress = :id')
->setParameter('id', $id)
->getResult();
}
public function getCustomerByAddress($id)
{
return $this->getEntityManager()
->createQueryBuilder()
->select('c')
->from('AcmePagesBundle:Customer', 'c')
->innerJoin('c.address', 'a')
->where('a.id = :id OR c.defaultaddress = :id')
->setParameter('id', $id)
->getQuery()
->getResult();
}
}
Customer.php实体文件:
namespace AcmePagesBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use SymfonyComponentValidatorConstraintsDateTime;
/**
* Customer
*
* @ORMTable(name="customer")
* @ORMEntity(repositoryClass="AcmePagesBundleEntityRepositoryCustomerRepository")
*/
class Customer
{
/**
* @var string
*
* @ORMColumn(name="firstname", type="string", length=32, nullable=true)
*/
private $firstname;
/**
* @var string
*
* @ORMColumn(name="lastname", type="string", length=32, nullable=true)
*/
private $lastname;
/**
* @var string
*
* @ORMColumn(name="email", type="string", length=96, nullable=true)
*/
private $email;
/**
* @var string
*
* @ORMColumn(name="telephone", type="string", length=32, nullable=true)
*/
private $telephone;
/**
* @var string
*
* @ORMColumn(name="mobile", type="string", length=32, nullable=true)
*/
private $mobile;
/**
* @var DateTime
*
* @ORMColumn(name="date_added", type="datetime", nullable=true)
*/
private $dateAdded;
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var AcmePagesBundleAddress
*
* @ORMManyToOne(targetEntity="AcmePagesBundleEntityAddress")
* @ORMJoinColumn(name="defaultaddress_id", referencedColumnName="id")
* @ORMOrderBy({"city" = "ASC"})
*/
private $defaultaddress;
/**
* @var Pet
*
* @ORMManyToMany(targetEntity="AcmePagesBundleEntityPet", inversedBy="customer", cascade={"persist"})
* @ORMJoinTable(name="customer_pet",
* joinColumns={
* @ORMJoinColumn(name="customer_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORMJoinColumn(name="pet_id", referencedColumnName="id")
* }
* )
*/
private $pet;
/**
* @var DoctrineCommonCollectionsCollection
*
* @ORMManyToMany(targetEntity="AcmePagesBundleEntityAddress", inversedBy="customer")
* @ORMJoinTable(name="customer_address",
* joinColumns={
* @ORMJoinColumn(name="customer_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORMJoinColumn(name="address_id", referencedColumnName="id")
* }
* )
*/
private $address;
/**
* @var string
*
* @ORMColumn(name="status", type="string", length=32, nullable=false)
*/
private $status = 'private';
/**
* Constructor
*/
public function __construct()
{
$this->pet = new ArrayCollection();
$this->address = new ArrayCollection();
$this->dateAdded = new DateTime();
}
/**
* Set firstname
*
* @param string $firstname
* @return Customer
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* @param string $lastname
* @return Customer
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Get fullname
*
* @return string
*/
public function getFullname()
{
return $this->firstname . ' ' .$this->lastname;
}
/**
* Set email
*
* @param string $email
* @return Customer
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set telephone
*
* @param string $telephone
* @return Customer
*/
public function setTelephone($telephone)
{
$this->telephone = $telephone;
return $this;
}
/**
* Get telephone
*
* @return string
*/
public function getTelephone()
{
return $this->telephone;
}
/**
* Set mobile
*
* @param string $mobile
* @return Customer
*/
public function setMobile($mobile)
{
$this->mobile = $mobile;
return $this;
}
/**
* Get mobile
*
* @return string
*/
public function getMobile()
{
return $this->mobile;
}
/**
* Set dateAdded
*
* @param DateTime $dateAdded
* @return Customer
*/
public function setDateAdded($dateAdded)
{
$this->dateAdded = $dateAdded;
return $this;
}
/**
* Get dateAdded
*
* @return DateTime
*/
public function getDateAdded()
{
return $this->dateAdded;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set defaultaddress
*
* @param AcmePagesBundleEntityAddress $defaultaddress
* @return Customer
*/
public function setDefaultaddress(AcmePagesBundleEntityAddress $defaultaddress = null)
{
$this->defaultaddress = $defaultaddress;
return $this;
}
/**
* Get defaultaddress
*
* @return AcmePagesBundleAddress
*/
public function getDefaultaddress()
{
return $this->defaultaddress;
}
/**
* Set pet
*
* @param AcmePagesBundlePet $pet
* @return Customer
*/
public function setPet(AcmePagesBundleEntityPet $pet = null)
{
$this->pet = $pet;
return $this;
}
/**
* Add pet
*
* @param AcmePagesBundleEntityPet $pet
* @return Customer
*/
public function addPet(AcmePagesBundleEntityPet $pet)
{
$this->pet[] = $pet;
return $this;
}
/**
* Remove pet
*
* @param AcmePagesBundleEntityPet $pet
*/
public function removePet(AcmePagesBundleEntityPet $pet)
{
$this->pet->removeElement($pet);
}
/**
* Get pet
*
* @return DoctrineCommonCollectionsCollection
*/
public function getPet()
{
return $this->pet;
}
/**
* Set address
*
* @param AcmePagesBundleEntityAddress $address
* @return Customer
*/
public function setAddress(AcmePagesBundleEntityAddress $address)
{
$this->address[] = $address;
return $this;
}
/**
* Add address
*
* @param AcmePagesBundleEntityAddress $address
* @return Customer
*/
public function addAddress(AcmePagesBundleEntityAddress $address)
{
$this->address[] = $address;
return $this;
}
/**
* Remove address
*
* @param AcmePagesBundleEntityAddress $address
*/
public function removeAddress(AcmePagesBundleEntityAddress $address)
{
$this->address->removeElement($address);
}
/**
* Get address
*
* @return DoctrineCommonCollectionsCollection
*/
public function getAddress()
{
return $this->address;
}
/**
* Set status
*
* @param string $status
* @return Customer
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return string
*/
public function getStatus()
{
return $this->status;
}
}
仔细检查您的实体定义:似乎Doctrine正在使用默认存储库而不是您的自定义存储库。
你必须在repositoryClass
注释中设置@ORMEntity
选项:
/**
* @ORMEntity(repositoryClass="AppBundleEntityRepositoryGroupRepository")
* [...]
*/
class Group
{
// [...]
}
以上是关于Symfony2 - 方法名称必须以findBy或findOneBy开头的主要内容,如果未能解决你的问题,请参考以下文章
是否真的有必要检查 getBy 或 findBy 方法是否返回真实值?
Symfony2 / 路由 / 使用参数作为控制器或动作名称