UFO ET IT

메소드 이름은 findBy 또는 findOneBy로 시작해야합니다.

ufoet 2020. 12. 14. 20:26
반응형

메소드 이름은 findBy 또는 findOneBy로 시작해야합니다. 정의되지 않은 방법 Symfony?


Symfony2의 part4를 통해 작업 중이며 컨트롤러 및 도우미 클래스 코드를 업데이트하는 동안 다음 오류 메시지가 나타납니다.

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

컨트롤러에 코드를 삽입하기 전에 튜토리얼에서 가르친대로 도우미 클래스로 이동하여 위의 오류 메시지가 표시됩니다.

<?php
// src/Blogger/BlogBundle/Repository/BlogRepository.php
namespace Blogger\BlogBundle\Repository;
use Doctrine\ORM\EntityRepository;

/**
 * BlogRepository
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
*/
class BlogRepository extends EntityRepository
{
 public function getLatestBlogs($limit = null)
 {
    $qb = $this->createQueryBuilder('b')
               ->select('b')
               ->addOrderBy('b.created', 'DESC');

    if (false === is_null($limit))
        $qb->setMaxResults($limit);

    return $qb->getQuery()
              ->getResult();
  } 
}

그리고 여기에 내 컨트롤러 파일 인덱스 작업 코드가 있습니다.

// src/Blogger/BlogBundle/Controller/PageController.php
class PageController extends Controller
{
  public function indexAction()
  {
    $em = $this->getDoctrine()
               ->getEntityManager();

    $blogs = $em->getRepository('BloggerBlogBundle:Blog')
                ->getLatestBlogs();

    return $this->render('BloggerBlogBundle:Page:index.html.twig', array(
        'blogs' => $blogs
    ));
    }

    // ..
}

/Entity/Blog.php 파일에서 몇 줄을 첨부하고 있습니다. 귀하의 답변에 따라 올바른지 확인하십시오.

<?php
// src/Blogger/BlogBundle/Entity/Blog.php

namespace Blogger\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")
 * @ORM\Table(name="blog")
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Entity
 */

class Blog
 {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   * @ORM\HasLifecycleCallbacks()
  */
  protected $id;
  --
  --
 }

내가 어디에서 잘못하고 있습니까?


엔티티 클래스를 수정했는지 확인하십시오.

// src/Blogger/BlogBundle/Entity/Blog.php
/**
 * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")
 * @ORM\Table(name="blog")
 * @ORM\HasLifecycleCallbacks()
 */
class Blog
{
    // ..
}

주석 @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")이 필요합니다.

엔터티를 다시 생성하는 것을 잊지 마십시오.

php app/console doctrine:generate:entities Blogger

최신 정보

주석을 제거 @ORM\Entity합니다. 올바른 주석을 재정의합니다.@ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")


In my case adding proper annotation was insufficient.
Deleting Doctrine Cache by php app/console doctrine:cache:clear-metadata also not worked.

I generate my entities from database by commands

php app/console doctrine:mapping:import --force AcmeBlogBundle xml
php app/console doctrine:mapping:convert annotation ./src
php app/console doctrine:generate:entities AcmeBlogBundle

First command generate orm.xml file for each DB Table in my project. After DELETING all orm.xml files Annotations started work properly.


If youre using yml as config files for your entities try adding this:

Blogger\BlogBundle\Entity\Blog:
    type: entity
    table: Blog
    repositoryClass: Blogger\BlogBundle\Repository\BlogRepository
    ...

and then as mentioned above:

php app/console doctrine:generate:entities Blogger

The other solution is to delete all the orm.xml files added by generated entities. If you move the folder or delete, your mapping with repository will be operationnal.


In case you are using PHP-FPM then this issue might persist even after all the above solutions you have tried then use sudo service php5-fpm restart which did the trick for me.


 * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Repository\BlogRepository")

Try putting the repository class at the same directory next to the Entity class:

     * @ORM\Entity(repositoryClass="Blogger\BlogBundle\BlogRepository")

For me it helped to restart my vm (Vagrant Box)


In Symfony 3 you are probably missing the repository class in your orm.xml file.

repository-class="Bundle\Repository\MyRepository"

Example:

<doctrine-mapping>
    <entity name="Bundle\Entity\MyEntity"
            table="tablename"
            repository-class="Bundle\Repository\MyRepository">
        <id name="id" type="integer" column="id">
            <generator strategy="AUTO"/>
        </id>
    </entity>
</doctrine-mapping>

참고URL : https://stackoverflow.com/questions/9172586/the-method-name-must-start-with-either-findby-or-findoneby-undefined-method-sym

반응형