【问题标题】:Symfony not finding a custom repositorySymfony 找不到自定义存储库
【发布时间】:2014-06-30 10:35:48
【问题描述】:

我使用 Symfony 的实体生成工具来创建实体(例如,Person 和 Destination)以及自定义实体存储库(例如,PersonRepository 和 DestinationRepository)。实体是针对 orm.yml 文件生成的,例如 Person.orm.yml 和 Destination.orm.yml。 DestinationRepository 类工作得很好,但我的 PersonController 似乎永远无法在 PersonRepository 类中找到任何方法。我得到的错误是:

UndefinedMethodException:尝试在 C:\xampp55\htdocs\symtran2\src\Me\MyBundle\Controller\PersonController.php 第 124 行中的类“Me\MyBundle\Entity\Person”上调用方法“findMe”。

我在 Person 实体中放了一个小“findMe()”方法,它可以工作,但是当我将它移到 PersonRepository.php 时,我得到了上述错误。这让我发疯了,Google 告诉我,我不是唯一遇到这个问题的人。

这是我的 Destination.org.yml 文件:

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

这是我的 Person.orm.yml 文件:

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 ]

这是我的 DestinationRepository 文件:

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

这是我的 PersonRepository.php 文件:

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

我将 YAML 用于 Doctrine,但在我的控制器中使用注释。

这是试图调用 findMe() 方法的控制器方法:

/** 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(),
    );
}

如果能帮我解决这个问题,我将不胜感激。

【问题讨论】:

  • 您在实体上使用 findMe,而不是存储库。您应该先从 Manager 获取存储库: $repository = $em->getRepository('GinsbergTransportationBundle:Person'); $repository->findMe();
  • 非常感谢!那确实是我做错了。

标签: php symfony doctrine-orm doctrine


【解决方案1】:

您正在对 id 找到的实体调用 findMe,您必须在存储库中调用它

$repository = $em->getRepository('GinsbergTransportationBundle:Person');

$found = $repository->findMe();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    相关资源
    最近更新 更多