0
votes

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
You are using findMe on the entity, not the repository. You should get the repository from the Manager before: $repository = $em->getRepository('GinsbergTransportationBundle:Person'); $repository->findMe();AlixB
Thanks a lot! That is indeed what I was doing wrong.Erica Ackerman

1 Answers

1
votes

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();