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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测