【发布时间】:2015-08-05 14:06:18
【问题描述】:
首先让我说,我知道这个问题之前已经出现过,但似乎没有一个解决方案有效。而且,我不是新会员,我只是不记得我的登录详细信息,所以我不得不重新注册....
基本上,我的工作是重新整理一个现有的 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.twig 在第 30 行。
我正在使用我认为是 2.7 的最新版本的 Symfony。我确定它以前可以工作,但是在清除缓存并重新生成实体之后,它似乎把事情搞砸了。我已经尝试了大多数其他建议,但现在不知所措。
如果有人能对此有所了解,我将不胜感激,谢谢。
迈克尔
编辑:
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;
}
}
【问题讨论】:
-
在 Resources/config/doctrine 下检查。如果您有 yml 或 xml 文件,请删除它们,以便您的注释映射工作。意外创建它们然后忘记它们是很常见的。
标签: php symfony repository entities