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 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳