Return property in a Manytomany relationship

Asked

Viewed 69 times

2

I have a relationship ManyToMany between 2 classes. Campannha and Empresa. A Annotation of the class Campanha is like this:

/**
 * @ORM\ManyToMany( targetEntity="JN\Entity\Empresas\Empresa")
 * @ORM\JoinTable(
 *      name="campanhas_empresas",
 *      joinColumns={
 *              @ORM\JoinColumn(
 *              name="campanhaId",
 *              referencedColumnName="id")},
 *      inverseJoinColumns={
 *              @ORM\JoinColumn(
 *              name="empresaId",
 *              referencedColumnName="id")}
 * )
 **/
private $empresas;
/**
 * @return mixed
 */
public function getEmpresas()
{
    return $this->empresas;
}

/**
 * @param mixed $empresas
 */
public function addEmpresas($empresas)
{
    $this->empresas->add($empresas); //ArrayCollection
}

The point is that when I access the method GetEmpresas, comes back as a result of this:

object(Doctrine\ORM\PersistentCollection)[609]
  private 'snapshot' => 
array (size=3)
  0 => 
object(Proxy\__CG__\JN\Entity\Empresas\Empresa)[644]
  public '__initializer__' => 
object(Closure)[392]
  public '__cloner__' => 
object(Closure)[393]
  public '__isInitialized__' => boolean false
  private 'id' (JN\Entity\Empresas\Empresa) => string '3' (length=1)
  private 'tipo_cadastro' (JN\Entity\Empresas\Empresa) => null
  private 'idMatriz' (JN\Entity\Empresas\Empresa) => null
  private 'razao_nome' (JN\Entity\Empresas\Empresa) => null
  private 'fantasia_contato' (JN\Entity\Empresas\Empresa) => null
  private 'categoria' (JN\Entity\Empresas\Empresa) => null
  private 'campanhas' (JN\Entity\Empresas\Empresa) => null

Which is completely correct. I wanted to know how I access, for example, the id or any other property above. I tried making a for-each thus:

foreach($this->getEmpresas()->toArray() as $item){
        $emp .= $item->getId.' - '.$item->getRazaoNome.'; ';            
    }

But the following mistake:

Notice: Undefined Property: Proxy__cg__ JN Entity Companies Company::$getId

Thank you

1 answer

0


The mistake itself has already given you the answer.

Notice: Undefined Property: Proxy__cg__ JN Entity Companies Company::$getId

In the following block, you call the property getId, that doesn’t exist.

foreach ($this->getEmpresas()->toArray() as $item) {
    $emp .= $item->getId.' - '.$item->getRazaoNome.'; ';            
}

I believe it’s just a typo, missing the parentheses of method getId():

foreach ($this->getEmpresas()->toArray() as $item) {
    $emp .= $item->getId() . ' - ' . $item->getRazaoNome . '; ';            
}
  • It was sleep. Really after a few good hours of programming I no longer analyzed correctly. As soon as I woke up I could see the error. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.