首先让我说,我知道这个问题以前出现过,但没有一个解决方案似乎奏效。而且,我不是新会员,我只是不记得我的登录信息,所以我不得不重新注册....
基本上,我的工作是重组一个现有的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'})) }}
我在运行应用程序时收到的错误是:
在呈现模板期间抛出异常未定义的方法'count'。方法名必须以两者之一开头findBy或findOneBy!")AcmePagesBundle:: main.html。第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
{
// [...]
}