【问题标题】:Symfony3 repository method doesn't workSymfony3 存储库方法不起作用
【发布时间】:2018-06-07 13:43:02
【问题描述】:

我正在尝试从我的控制器 ArticleController 调用存储库 ArticleRepository 中的方法。但是它说:

Undefined method 'afficheArticle'. The method name must start with either findBy or findOneBy!

我的实体文章:(Entity\Article.php)

/**
 * Article
 *
 * @ORM\Table(name="Article", indexes={@ORM\Index(name="I_FK_Article_TypeArticle", columns={"idTypeArticle"})})
 * @ORM\Entity(repositoryClass="erp-gkeep\new_erp\gkeepBundle\Repository\ArticleRepository")
 */
class Article
{

我的文章控制器(控制器\文章控制器)

 /**
     * @Route("viewArticle2", name="viewArticle2")
     */
    public function listAction2()
    {

        $data = $this->getDoctrine()->getRepository('gkeepBundle:Article')->afficheArticle();

我的文章库

    <?php
/**
 * Created by PhpStorm.
 */

namespace gkeepBundle\Repository;


use Doctrine\ORM\EntityRepository;

class ArticleRepository extends EntityRepository
{

    public function afficheArticle(){
        $em=$this->getEntityManager();

        $query = $em->createQuery(
            'SELECT a.reference, a.designationfr, a.designationen, a.plan, a.url, a.datecreation, a.idtypearticle
     FROM gkeepBundle:Article a
     '
        );

        $articles = $query->getArrayResult();

        return $articles;
    }
}

如果有人可以帮助我,请!我很确定这是一个愚蠢的错误:/

【问题讨论】:

    标签: php repository symfony-3.4


    【解决方案1】:

    映射注释中repositoryClass 的值需要是存储库的命名空间+ 类名,看起来你可能还添加了一些目录结构。 - 实际上不是 PHP 命名空间中的有效字符,所以你现在得到的值肯定是不对的。

    试试

    @ORM\Entity(repositoryClass="gkeepBundle\Repository\ArticleRepository")
    

    供参考:当这个类名无效时,Doctrine 会回退到默认存储库 - 这就是引发您所看到的错误的原因。

    【讨论】:

    • 嗨@iainn,感谢您的帮助!我已经尝试过你的答案,但没有积极的结果,我仍然有同样的错误信息:/
    • 顺便说一句,我自己创建了目录“Repository”和“ArticleRepository.php”类,也许它改变了一些东西,这就是为什么 logiciel 无法识别存储库?
    • 尝试检查拼写并清理缓存
    猜你喜欢
    • 2018-07-26
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    相关资源
    最近更新 更多