[英]Symfony2 and Doctrine entity undefined method
I have this same issue as here The method name must start with either findBy or findOneBy. 我的问题与此处相同。方法名称必须以findBy或findOneBy开头。 Undefined method Symfony?
未定义的方法Symfony? but the answers don't help.
但答案无济于事。 When I run my code
当我运行代码时
<?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();
}
//...
}
I get exception 我有例外
Undefined method 'findAllMarkers'.
未定义的方法'findAllMarkers'。 The method name must start with either findBy or findOneBy!
方法名称必须以findBy或findOneBy开头!
this is my MarkersRepository file (located in Entity directory) 这是我的MarkersRepository文件(位于Entity目录中)
<?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();
}
}
and this is Markers entity class (located in Entity directory as well) 这是Markers实体类(也位于Entity目录中)
<?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;
}
}
I tried clear cache and use command 我尝试清除缓存并使用命令
php app/console doctrine:generate:entities Map
but still exception is throw. 但是仍然抛出异常。 Anybody see whats wrong with my code?
有人看到我的代码有什么问题吗?
Solution Removing files: Markers.orm.yml and Tags.orm.yml from Map\\ViewBundle\\Resources\\config\\doctrine directory is the solution. 解决方案删除文件: 解决方案是从Map \\ ViewBundle \\ Resources \\ config \\ doctrine目录中的Markers.orm.yml和Tags.orm.yml 。
Start by verifying that you are not getting the correct repository. 首先,验证您没有获得正确的存储库。
$repo = $em->getRepository('MapViewBundle:Markers')
die('Repo Class ' . get_class($repo));
If you do happen to get the correct class then you have a typo. 如果您碰巧获得了正确的课程,那么您就有错别字。
And make sure you don't have any doctrine/markers.orm.yml or xml files hanging around. 并确保您没有任何doctrine / markers.orm.yml或xml文件。
Finally, update your question with the doctrine section in app/config/config.yml 最后,使用app / config / config.yml中的“学说”部分更新您的问题
This may sound weird but I encountered a similar issue with a custom repository function. 这听起来可能很奇怪,但是我在自定义存储库功能中遇到了类似的问题。 Try renaming your Repository function to getAllMarkers() and update the call in the controller and try again.
尝试将您的Repository函数重命名为getAllMarkers()并更新控制器中的调用,然后重试。 This solved the issue for me on that occasion.
那时,这为我解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.