简体   繁体   中英

Symfony3 repository method doesn't work

I'm trying to call a method in the repository ArticleRepository from my controller ArticleController. However it says :

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

my Entity Article : (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
{

my ArticleController (Controller\\ArticleController)

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

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

my ArticleRepository

    <?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;
    }
}

if someone can help me please ! I'm pretty sure it's a stupid error :/

The value of repositoryClass in your mapping annotation needs to be the namespace + class name of your repository, it looks like you've maybe added some of the directory structure as well. - actually isn't a valid character in a PHP namespace, so the value you've got now definitely isn't right.

Try

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

For reference: when this classname is not valid, Doctrine falls back to the default repository - this is what's throwing the error you're seeing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM