Question

So, we have two entities. One with repository and another is not. When we trying to get the data from another table we will get the ArrayCollection data. Question is how to call this entity repository methods? Is it real?

Example:

    $system = $this
            ->getDoctrine()
            ->getEntityManager()
            ->getRepository('SomeBundle:FirstEntity')
            ->findOneByColumnID($id);

    $den = $system->getDataFromSecondTable(); // ArrayCollection of SecondEntity

And then i want to use some kind of:

    $den[0]->functionFromSecondEntityRepository();

So, method "functionFromSecondEntityRepository" is in Repository of class SecondEntity and i can't call it - error on undefined method call "functionFromSecondEntityRepository".

So how can i do it in right way?

Was it helpful?

Solution

You didnt provide too many details so I will make some example up here.

Let's say you have an Entity FriendsList and a One-to-Many relationship with Entity Friend.

$List = $this->getDoctrine()
                ->getEntityManager()
                ->getRepository('SomeBundle:FriendsList')
                ->find($id);

// The list you pulled in by ID can now be used
$List->getId();

foreach($List->getFriends() as $Friend)
{
    // Each friend will be output here, you have access
    // to the Friend methods now for each.
    $Friend->getId();
    $Friend->getFirstName();
    $Friend->getLastName();
    $Friend->getDOB();
    $Friend->getFavoriteColor();
}

By default when you create relationships a method to acquire the collection is created, in this example getFriends which returns an array of Entities. After you generate the entities look at your Entity Model to see which methods are available. By default one is created for each property in your entity and additional ones for Collections.

SomeCool/Bundle/Entity/FriendsList
Somecool/Bundle/Entity/Friend

The following is what a one-to-many relationship would look like if you use YAML configuration.

SomeCool\Bundle\Entity\FriendsList:
  type: entity
  table: null
  oneToMany:
    friend:
      targetEntity: Friend
      mappedBy: friendslist
      cascade:  ["persist"]

SomeCool/Bundle/Entity/Friend
  manytoOne:
    friends:
      targetEntity: FriendsList
      mappedBy: friend
      cascade:  ["persist"]

Accessing a Repository

YAML Configuration (services.yml)

somebundle.bundle.model.friends:
    class: SomeBundle/Bundle/Model/Friends
    arguments: [@doctrine.orm.entity_manager]

On the Controller

$friendsModel = $this->get('somebundle.bundle.model.friends');
$Friends = $friendsModel->findByFirstName('Bobby');

foreach($Friends as $Friend)
{
    $Friend->getLastName();
}

OTHER TIPS

Repository methods are not available in Entities. You would need a function in your AnotherEntity to grab the ArrayCollection. IE:

class FirstEntity {

   public function getAnotherEntity()
   {
       return $this->anotherEntity;
   }

}

class AnotherEntity 
{
   public function getArrayCollection()
   {
       return $this->myArrayCollection;
   }
}

$firstEntity->getAnotherEntity()->getArrayCollection();

Another option would be to get the AnotherEntity's repository based on results from first:

$system = $this
        ->getDoctrine()
        ->getEntityManager()
        ->getRepository('SomeBundle:SomeEntity')
        ->findOneByColumnID($id);

$anotherEntity = $system->getAnotherEntity();

$anotherEntityResult = $this->getDoctrine()
                            ->getRepository(get_class($anotherEntity))
                            ->functionFromAnotherEntityRepository($anotherEntity->getId());

If using the second solution, I'd make sure that $anotherEntity is not null before attempting to retrieve the repository.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top