0
votes

I am having issues running through the doctrine tutorial and attempting to setup entity repos.

// _src/entities/User.php
/**
 * @ORM\Entity(repositoryClass="UserRepository")
 * @Entity @Table(name="users")
 */
class User
{
    /**
 * @Id @GeneratedValue @Column(type="integer")
 * @var int
 */
protected $id;
/** @Column(type="string") */
protected $fname;
/** @Column(type="string") */
protected $lname;

public function getId()
{
    return $this->id;
}

public function getFname()
{
    return $this->fname;
}

public function setFname($fname)
{
    $this->fname = $fname;
}
public function getLname()
{
    return $this->lname;
}

public function setLname($lname)
{
    $this->lname = $lname;
}
}

I am then calling:

$users = $em->getRepository('User')->getUsersByLastName('user');
var_dump($users);

My Repo:

// _src/entities/UserRepository.php

use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository
{
    public function getUsersByLastName($lname)
    {
        $dql = "SELECT u FROM User u WHERE u.lname = ?1 ORDER BY b.created DESC";

        return $this->getEntityManager()->createQuery($dql)
        ->setParameter(1, $lname)
        ->getResult();
    }
}

I am following the tutorial and don't understand what I am missing. Any assistance would be amazing. I am trying to learn this rather quickly and can't believe im running into these issues already.

Here is the error I am getting:

PHP Fatal error: Uncaught BadMethodCallException: Undefined method 'getUsersByLastName'. The method name must start with either findBy or findOneBy! in /Users/ks/Documents/PHPStormProjects/dan/_vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:226

Stack trace:

#0 /Users/ks/Documents/PHPStormProjects/dan/test.php(27): Doctrine\ORM\EntityRepository->__call('getUsersByLastN...', Array)

#1 {main} thrown in /Users/ks/Documents/PHPStormProjects/dan/_vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php on line 226

1
Please add any errors you are getting. It helps narrow down the problem, experienced users might know what's wrong by seeing the error message. It's either way much easier than reading through all your code trying to try to find an unknown error - JimL
Yes @JimL, not sure why I forgot that. Edited original post to include error and stack. - user3170367
Try clearing metadata cache - malarzm
I tried clearing all cache this morning. $cacheDriver = new \Doctrine\Common\Cache\MemcacheCache(); $deleted = $cacheDriver->deleteAll(); I received this error:PHP Fatal error: Uncaught Error: Call to a member function get() on null in /Users/ks/Documents/PHPStormProjects/dan/_vendor/doctrine/cache/lib/Doctrine/Common/Cache/MemcacheCache.php:69 I dont understand why so many issues with these. I am doing exactly what they are saying in the documentation. - user3170367

1 Answers

0
votes

I found the answer. It was literally in the error. Once I changed the method to public function findByLname($lname) it worked. I need to review the documentation more carefully. Seems weird, you can't name methods whatever you want.

I still am unsure on the clear cache error. Followed that documentation correctly I thought too.