douliang1891 2018-06-11 21:47
浏览 20

学说中的实体存储库

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条回答 默认 最新

  • dongni3854 2018-06-12 15:46
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度