【问题标题】:Symfony2 and Doctrine entity undefined methodSymfony2 和 Doctrine 实体未定义方法
【发布时间】:2014-04-30 20:34:16
【问题描述】:

我和The method name must start with either findBy or findOneBy. Undefined method Symfony? 有同样的问题,但答案没有帮助。 当我运行我的代码时

<?php

namespace Map\ViewBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Map\ViewBundle\Entity\Markers;

class DefaultController extends Controller
{
    public function getMarkersAction()
    {
        $em = $this->getDoctrine()->getManager();
        $em->getRepository('MapViewBundle:Markers')
            ->findAllMarkers();
    }
    //...   
}

我得到异常

未定义的方法“findAllMarkers”。方法名必须以 findBy 或 findOneBy!

这是我的 MarkersRepository 文件(位于实体目录中)

<?php

namespace Map\ViewBundle\Entity;
use Doctrine\ORM\EntityRepository;

class MarkersRepository extends EntityRepository
{
    public function  findAllMarkers() {
        //this query will be different
        return $this->getEntityManager()
            ->createQuery(
                'SELECT m FROM MapViewBundle:Markers m'
            )
            ->getResult();
    }
}

这是 Markers 实体类(也位于实体目录中)

<?php

namespace Map\ViewBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="markers")
 * @ORM\Entity(repositoryClass="Map\ViewBundle\Entity\MarkersRepository")
 */
class Markers
{
    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100, nullable=false)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=500, nullable=false)
     */
    private $description;

    /**
     * @var integer
     *
     * @ORM\Column(name="icon", type="integer", nullable=false)
     */
    private $icon;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="post_date", type="datetime", nullable=false)
     */
    private $postDate;

    /**
     * @var float
     *
     * @ORM\Column(name="lat", type="float", precision=10, scale=0, nullable=false)
     */
    private $lat;

    /**
     * @var float
     *
     * @ORM\Column(name="lng", type="float", precision=10, scale=0, nullable=false)
     */
    private $lng;

    /**
     * @var integer
     *
     * @ORM\Column(name="relevance_degree", type="integer", nullable=false)
     */
    private $relevanceDegree;

    /**
     * @var string
     *
     * @ORM\Column(name="geohash", type="string", length=12, nullable=false)
     */
    private $geohash;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Map\ViewBundle\Entity\Tags", inversedBy="idMarkers")
     * @ORM\JoinTable(name="markers_tags",
     *   joinColumns={
     *     @ORM\JoinColumn(name="id_markers", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="id_tags", referencedColumnName="id")
     *   }
     * )
     */
    private $idTags;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->idTags = new \Doctrine\Common\Collections\ArrayCollection();
    }


    /**
     * Set name
     *
     * @param string $name
     * @return Markers
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Markers
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set icon
     *
     * @param integer $icon
     * @return Markers
     */
    public function setIcon($icon)
    {
        $this->icon = $icon;

        return $this;
    }

    /**
     * Get icon
     *
     * @return integer 
     */
    public function getIcon()
    {
        return $this->icon;
    }

    /**
     * Set postDate
     *
     * @param \DateTime $postDate
     * @return Markers
     */
    public function setPostDate($postDate)
    {
        $this->postDate = $postDate;

        return $this;
    }

    /**
     * Get postDate
     *
     * @return \DateTime 
     */
    public function getPostDate()
    {
        return $this->postDate;
    }

    /**
     * Set lat
     *
     * @param float $lat
     * @return Markers
     */
    public function setLat($lat)
    {
        $this->lat = $lat;

        return $this;
    }

    /**
     * Get lat
     *
     * @return float 
     */
    public function getLat()
    {
        return $this->lat;
    }

    /**
     * Set lng
     *
     * @param float $lng
     * @return Markers
     */
    public function setLng($lng)
    {
        $this->lng = $lng;

        return $this;
    }

    /**
     * Get lng
     *
     * @return float 
     */
    public function getLng()
    {
        return $this->lng;
    }

    /**
     * Set relevanceDegree
     *
     * @param integer $relevanceDegree
     * @return Markers
     */
    public function setRelevanceDegree($relevanceDegree)
    {
        $this->relevanceDegree = $relevanceDegree;

        return $this;
    }

    /**
     * Get relevanceDegree
     *
     * @return integer 
     */
    public function getRelevanceDegree()
    {
        return $this->relevanceDegree;
    }

    /**
     * Set geohash
     *
     * @param string $geohash
     * @return Markers
     */
    public function setGeohash($geohash)
    {
        $this->geohash = $geohash;

        return $this;
    }

    /**
     * Get geohash
     *
     * @return string 
     */
    public function getGeohash()
    {
        return $this->geohash;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add idTags
     *
     * @param \Map\ViewBundle\Entity\Tags $idTags
     * @return Markers
     */
    public function addIdTag(\Map\ViewBundle\Entity\Tags $idTags)
    {
        $this->idTags[] = $idTags;

        return $this;
    }

    /**
     * Remove idTags
     *
     * @param \Map\ViewBundle\Entity\Tags $idTags
     */
    public function removeIdTag(\Map\ViewBundle\Entity\Tags $idTags)
    {
        $this->idTags->removeElement($idTags);
    }

    /**
     * Get idTags
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getIdTags()
    {
        return $this->idTags;
    }
}

我试过清除缓存并使用命令

php app/console doctrine:generate:entities Map

但仍然抛出异常。有人看到我的代码有什么问题吗?

解决方案Map\ViewBundle\Resources\config\doctrine 目录中删除文件:Markers.orm.ymlTags.orm.yml 是解决方案。

【问题讨论】:

  • 您是否尝试过手动从 app/cache 目录中删除缓存?
  • 是的,我仍然遇到这个错误
  • 您的代码似乎完全正确。如果您设法解决了问题,请在此处添加您的答案。
  • 您确定在创建捆绑包时将别名设置为MapViewBundle 或者可能是ViewBundle
  • 我使用了$ php app/console generate:bundle --namespace=Map/ViewBundle 命令。也许原因是根据本教程link 从现有数据库表创建标记实体

标签: php symfony doctrine-orm doctrine


【解决方案1】:

首先验证您没有获得正确的存储库。

$repo = $em->getRepository('MapViewBundle:Markers')
die('Repo Class ' . get_class($repo));

如果你碰巧得到了正确的课程,那么你有一个错字。

并确保您没有任何学说/markers.orm.yml 或 xml 文件。

最后,用 app/config/config.yml 中的教义部分更新您的问题

【讨论】:

  • Doctrine/markers.orm.yml 文件是问题所在。我删除了它们,一切都很好。啊!!谢谢@Cerad,你拯救了我的一天
【解决方案2】:

这听起来可能很奇怪,但我在自定义存储库函数中遇到了类似的问题。 尝试将您的 Repository 函数重命名为 getAllMarkers() 并更新控制器中的调用,然后重试。这在那个场合为我解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 2018-09-21
    • 1970-01-01
    相关资源
    最近更新 更多