Reputation: 23
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
Upvotes: 0
Views: 1110
Reputation: 23
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.
Upvotes: 0