Symfony2 - 方法名称必须以 findBy 或 findOneBy 开头
Symfony2 - Method name must begin with findBy or findOneBy
首先让我说,我知道这个问题以前出现过,但 none 的解决方案似乎有效。而且,我也不是新会员,我只是不记得我的登录详细信息,所以我不得不重新注册....
基本上,我的工作是重新散列一个现有的 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'})) }}
我在 运行 应用程序时收到的错误是:
An exception has been thrown during the rendering of a template
("Undefined method 'count'. The method name must start with either
findBy or findOneBy!") in
AcmePagesBundle::main.html.twig at line 30.
我正在使用最新版本的 Symfony,我认为它是 2.7。我确定它之前可以正常工作,但是在清除缓存并重新生成实体之后,它似乎搞砸了。我已经尝试了大多数其他建议,但现在不知所措。
如果有人能对此有所了解,我将不胜感激,谢谢。
迈克尔
编辑:
CustomerRepository.php 文件:
namespace Acme\PagesBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
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 Acme\PagesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints\DateTime;
/**
* Customer
*
* @ORM\Table(name="customer")
* @ORM\Entity(repositoryClass="Acme\PagesBundle\Entity\Repository\CustomerRepository")
*/
class Customer
{
/**
* @var string
*
* @ORM\Column(name="firstname", type="string", length=32, nullable=true)
*/
private $firstname;
/**
* @var string
*
* @ORM\Column(name="lastname", type="string", length=32, nullable=true)
*/
private $lastname;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=96, nullable=true)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="telephone", type="string", length=32, nullable=true)
*/
private $telephone;
/**
* @var string
*
* @ORM\Column(name="mobile", type="string", length=32, nullable=true)
*/
private $mobile;
/**
* @var \DateTime
*
* @ORM\Column(name="date_added", type="datetime", nullable=true)
*/
private $dateAdded;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \Acme\PagesBundle\Address
*
* @ORM\ManyToOne(targetEntity="Acme\PagesBundle\Entity\Address")
* @ORM\JoinColumn(name="defaultaddress_id", referencedColumnName="id")
* @ORM\OrderBy({"city" = "ASC"})
*/
private $defaultaddress;
/**
* @var Pet
*
* @ORM\ManyToMany(targetEntity="Acme\PagesBundle\Entity\Pet", inversedBy="customer", cascade={"persist"})
* @ORM\JoinTable(name="customer_pet",
* joinColumns={
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="pet_id", referencedColumnName="id")
* }
* )
*/
private $pet;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Acme\PagesBundle\Entity\Address", inversedBy="customer")
* @ORM\JoinTable(name="customer_address",
* joinColumns={
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="address_id", referencedColumnName="id")
* }
* )
*/
private $address;
/**
* @var string
*
* @ORM\Column(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 \Acme\PagesBundle\Entity\Address $defaultaddress
* @return Customer
*/
public function setDefaultaddress(\Acme\PagesBundle\Entity\Address $defaultaddress = null)
{
$this->defaultaddress = $defaultaddress;
return $this;
}
/**
* Get defaultaddress
*
* @return \Acme\PagesBundle\Address
*/
public function getDefaultaddress()
{
return $this->defaultaddress;
}
/**
* Set pet
*
* @param \Acme\PagesBundle\Pet $pet
* @return Customer
*/
public function setPet(\Acme\PagesBundle\Entity\Pet $pet = null)
{
$this->pet = $pet;
return $this;
}
/**
* Add pet
*
* @param \Acme\PagesBundle\Entity\Pet $pet
* @return Customer
*/
public function addPet(\Acme\PagesBundle\Entity\Pet $pet)
{
$this->pet[] = $pet;
return $this;
}
/**
* Remove pet
*
* @param \Acme\PagesBundle\Entity\Pet $pet
*/
public function removePet(\Acme\PagesBundle\Entity\Pet $pet)
{
$this->pet->removeElement($pet);
}
/**
* Get pet
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPet()
{
return $this->pet;
}
/**
* Set address
*
* @param \Acme\PagesBundle\Entity\Address $address
* @return Customer
*/
public function setAddress(\Acme\PagesBundle\Entity\Address $address)
{
$this->address[] = $address;
return $this;
}
/**
* Add address
*
* @param \Acme\PagesBundle\Entity\Address $address
* @return Customer
*/
public function addAddress(\Acme\PagesBundle\Entity\Address $address)
{
$this->address[] = $address;
return $this;
}
/**
* Remove address
*
* @param \Acme\PagesBundle\Entity\Address $address
*/
public function removeAddress(\Acme\PagesBundle\Entity\Address $address)
{
$this->address->removeElement($address);
}
/**
* Get address
*
* @return \Doctrine\Common\Collections\Collection
*/
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 使用的是默认存储库而不是您的自定义存储库。
您必须在 @ORM\Entity
注释中设置 repositoryClass
选项:
/**
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\GroupRepository")
* [...]
*/
class Group
{
// [...]
}
首先让我说,我知道这个问题以前出现过,但 none 的解决方案似乎有效。而且,我也不是新会员,我只是不记得我的登录详细信息,所以我不得不重新注册....
基本上,我的工作是重新散列一个现有的 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'})) }}
我在 运行 应用程序时收到的错误是:
An exception has been thrown during the rendering of a template ("Undefined method 'count'. The method name must start with either findBy or findOneBy!") in AcmePagesBundle::main.html.twig at line 30.
我正在使用最新版本的 Symfony,我认为它是 2.7。我确定它之前可以正常工作,但是在清除缓存并重新生成实体之后,它似乎搞砸了。我已经尝试了大多数其他建议,但现在不知所措。
如果有人能对此有所了解,我将不胜感激,谢谢。
迈克尔
编辑:
CustomerRepository.php 文件:
namespace Acme\PagesBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
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 Acme\PagesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints\DateTime;
/**
* Customer
*
* @ORM\Table(name="customer")
* @ORM\Entity(repositoryClass="Acme\PagesBundle\Entity\Repository\CustomerRepository")
*/
class Customer
{
/**
* @var string
*
* @ORM\Column(name="firstname", type="string", length=32, nullable=true)
*/
private $firstname;
/**
* @var string
*
* @ORM\Column(name="lastname", type="string", length=32, nullable=true)
*/
private $lastname;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=96, nullable=true)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="telephone", type="string", length=32, nullable=true)
*/
private $telephone;
/**
* @var string
*
* @ORM\Column(name="mobile", type="string", length=32, nullable=true)
*/
private $mobile;
/**
* @var \DateTime
*
* @ORM\Column(name="date_added", type="datetime", nullable=true)
*/
private $dateAdded;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \Acme\PagesBundle\Address
*
* @ORM\ManyToOne(targetEntity="Acme\PagesBundle\Entity\Address")
* @ORM\JoinColumn(name="defaultaddress_id", referencedColumnName="id")
* @ORM\OrderBy({"city" = "ASC"})
*/
private $defaultaddress;
/**
* @var Pet
*
* @ORM\ManyToMany(targetEntity="Acme\PagesBundle\Entity\Pet", inversedBy="customer", cascade={"persist"})
* @ORM\JoinTable(name="customer_pet",
* joinColumns={
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="pet_id", referencedColumnName="id")
* }
* )
*/
private $pet;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Acme\PagesBundle\Entity\Address", inversedBy="customer")
* @ORM\JoinTable(name="customer_address",
* joinColumns={
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="address_id", referencedColumnName="id")
* }
* )
*/
private $address;
/**
* @var string
*
* @ORM\Column(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 \Acme\PagesBundle\Entity\Address $defaultaddress
* @return Customer
*/
public function setDefaultaddress(\Acme\PagesBundle\Entity\Address $defaultaddress = null)
{
$this->defaultaddress = $defaultaddress;
return $this;
}
/**
* Get defaultaddress
*
* @return \Acme\PagesBundle\Address
*/
public function getDefaultaddress()
{
return $this->defaultaddress;
}
/**
* Set pet
*
* @param \Acme\PagesBundle\Pet $pet
* @return Customer
*/
public function setPet(\Acme\PagesBundle\Entity\Pet $pet = null)
{
$this->pet = $pet;
return $this;
}
/**
* Add pet
*
* @param \Acme\PagesBundle\Entity\Pet $pet
* @return Customer
*/
public function addPet(\Acme\PagesBundle\Entity\Pet $pet)
{
$this->pet[] = $pet;
return $this;
}
/**
* Remove pet
*
* @param \Acme\PagesBundle\Entity\Pet $pet
*/
public function removePet(\Acme\PagesBundle\Entity\Pet $pet)
{
$this->pet->removeElement($pet);
}
/**
* Get pet
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPet()
{
return $this->pet;
}
/**
* Set address
*
* @param \Acme\PagesBundle\Entity\Address $address
* @return Customer
*/
public function setAddress(\Acme\PagesBundle\Entity\Address $address)
{
$this->address[] = $address;
return $this;
}
/**
* Add address
*
* @param \Acme\PagesBundle\Entity\Address $address
* @return Customer
*/
public function addAddress(\Acme\PagesBundle\Entity\Address $address)
{
$this->address[] = $address;
return $this;
}
/**
* Remove address
*
* @param \Acme\PagesBundle\Entity\Address $address
*/
public function removeAddress(\Acme\PagesBundle\Entity\Address $address)
{
$this->address->removeElement($address);
}
/**
* Get address
*
* @return \Doctrine\Common\Collections\Collection
*/
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 使用的是默认存储库而不是您的自定义存储库。
您必须在 @ORM\Entity
注释中设置 repositoryClass
选项:
/**
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\GroupRepository")
* [...]
*/
class Group
{
// [...]
}