douqingzhi0980 2014-06-30 10:35
浏览 44
已采纳

Symfony找不到自定义存储库

I have used Symfony's entity generation facilities to create Entities (e.g., Person and Destination) as well as custom Entity Repositories (e.g., PersonRepository and DestinationRepository). The Entities were generated against orm.yml files such as Person.orm.yml and Destination.orm.yml. The DestinationRepository class works great, but my PersonController can never seem to find any methods in the PersonRepository class. The error I'm getting is:

UndefinedMethodException: Attempted to call method "findMe" on class "Me\MyBundle\Entity\Person" in C:\xampp55\htdocs\symtran2\src\Me\MyBundle\Controller\PersonController.php line 124.

I put a little "findMe()" method in the Person entity, and it works, but when I move it to PersonRepository.php, I get the above error. This is driving me crazy, and Google informs me that I'm not the only person to have this problem.

Here is my Destination.org.yml file:

Ginsberg\TransportationBundle\Entity\Destination:
type: entity
repositoryClass: Ginsberg\TransportationBundle\Entity\DestinationRepository
table: destination
id:
    id:
        type: integer
        generator:
          strategy: AUTO
fields:
    name:
        type: string
        length: 255
        unique: true
        nullable: false
    is_active:
        type: boolean
        nullable: true
manyToOne:
    program:
        targetEntity: Program
        inversedBy: destinations
        joinColumn:
            name: program_id
            referencedColumnName: id
oneToMany:
    reservations:
        targetEntity: Reservation
        mappedBy: destination

Here is my Person.orm.yml file:

Ginsberg\TransportationBundle\Entity\Person:
type: entity
repositoryClass: Ginsberg\TransportationBundle\Entity\PersonRepository
table: person
id:
    id:
        type: integer
        generator:
          strategy: AUTO
fields:
    firstName:
        type: string
        length: 100
    lastName:
        type: string
        length: 100
    uniqname:
        type: string
        length: 25
        unique: true
    phone:
      type: string
      length: 20
      nullable: true
    status:
      type: string
      length: 100
      nullable: true
    dateApproved:
      type: datetime
      nullable: true
    isTermsAgreed:
      type: boolean
      nullable: true
    hasUnpaidTicket:
      type: smallint
      nullable: true
    created:
      type: datetime
    modified:
      type: datetime
      nullable: true
oneToMany:
  reservations:
        targetEntity: Reservation
        mappedBy: person
manyToOne:
  program:
        targetEntity: Program
        inversedBy: persons
        joinColumn:
            name: program_id
            referencedColumnName: id
            nullable: false
lifecycleCallbacks: 
  prePersist: [ setCreatedValue ]
  preUpdate: [ setModifiedValue ]

Here is my DestinationRepository file:

<?php

namespace Ginsberg\TransportationBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * DestinationRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class DestinationRepository extends EntityRepository
{
  public function findByProgramsSortedByProgram($param) 
    {
      $dql = 'SELECT d, p FROM GinsbergTransportationBundle:Destination d JOIN         d.program p ORDER BY p.name ASC, d.name ASC';
      $query = $this->getEntityManager()->createQuery($dql);

      try {
        return $query->getResult();
      } catch (\Doctrine\ORM\NoResultException $ex) {
        return null;
      }
    }
}

And here is my PersonRepository.php file:

<?php

namespace Ginsberg\TransportationBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * PersonRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class PersonRepository extends EntityRepository
{
  public function findMe() {
    print_r("Here");
  }

  public function findByPendingSortedByCreated($fakeParam) 
    {
      $dql = "SELECT p, prog FROM GinsbergTransportationBundle:Person p JOIN d.program prog WHERE p.status = 'pending' ORDER BY p.created ASC";
        $query = getEntityManager()->createQuery($dql);

        try {
          return $query->getResult();
        } catch (\Doctrine\ORM\NoResultException $ex) {
          return null;
        }
    }
}

I'm using YAML for Doctrine but annotations in my controllers.

Here's the controller method that tries to call the findMe() method:

/** Finds and displays a Person entity.
 *
 * @Route("/{id}", name="person_show")
 * @Method("GET")
 * @Template()
 */
public function showAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('GinsbergTransportationBundle:Person')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Person entity.');
    }

    $entity->findMe();
    //$entity->testMe();
    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity'      => $entity,
        'delete_form' => $deleteForm->createView(),
    );
}

I would be beyond grateful for any assistance in sorting this out.

  • 写回答

1条回答 默认 最新

  • dragon87836215 2014-06-30 11:06
    关注

    you are calling findMe on the entity found by id, you have to call it on the repository

    $repository = $em->getRepository('GinsbergTransportationBundle:Person');
    
    $found = $repository->findMe();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决