Question

I have this same issue as here The method name must start with either findBy or findOneBy. Undefined method 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'. The method name must start with either findBy or findOneBy!

this is my MarkersRepository file (located in Entity directory)

<?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)

<?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.

Was it helpful?

Solution

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.

Finally, update your question with the doctrine section in app/config/config.yml

OTHER TIPS

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. This solved the issue for me on that occasion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top