zavalit
zavalit

Reputation: 307

Get a value from associated Doctrine Entity

I have ManyToMany Association between Entities "Consultant" and "Status" which defined as follows:

@ORM\ManyToMany(targetEntity="Status", inversedBy="consultant")
  @ORM\JoinTable(name="consultant_status",
  joinColumns={
  @ORM\JoinColumn(name="consultant_id", referencedColumnName="id")
  },
  inverseJoinColumns={
  @ORM\JoinColumn(name="status_id", referencedColumnName="id")
 }
)

when I try(on Doctrine postUpdate event) to get an id from Status, with the following way:

...
$entity = $args->getEntity();
if($entity instanceof Consultant){
 $status_id= $entity->getStatu()->getId();
}
...

I get:

Call to undefined method Doctrine\ORM\PersistentCollection::::getId()

Does anybody know what I do wrong?

Upvotes: 1

Views: 3408

Answers (1)

Pierrickouw
Pierrickouw

Reputation: 4704

As Consultant and Status are in a ManyToMany relation, getStatus() will return a Collection object with all the status related to this Consultant.

To loop throught all your statut just use foreach

foreach($entity->getStatu() as $statut) {
                        ^^^
                        //you might have a typo here
    $statut->getId();
    //other stuff
}

Upvotes: 2

Related Questions