Symfony2 - 方法名称必须以findBy或findOneBy开头

问题描述 投票:1回答:1

首先让我说,我知道之前出现了这个问题,但没有一个解决方案似乎有效。而且,我也不是新成员,我记不起我的登录信息,所以我不得不重新注册....

基本上,我的工作是重新整理现有的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 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;
    }

}
php symfony repository entities
1个回答
0
投票

仔细检查您的实体定义:似乎Doctrine正在使用默认存储库而不是您的自定义存储库。

你必须在repositoryClass注释中设置@ORM\Entity选项:

/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\GroupRepository")
 * [...]
 */
class Group
{
    // [...]
}
© www.soinside.com 2019 - 2024. All rights reserved.