Return property in a ManyToMany relationship

2

I have a ManyToMany relationship between 2 classes. Campannha and Empresa . Annotation of class Campanha looks 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 question is that when I access the GetEmpresas method, this returns as follows:

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

That is completely correct. I was wondering how do I access, for example, id or any other property above. I tried doing for-each like this:

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

But it gave the following error:

  

Notice: Undefined property: Proxy__CG __ \ JN \ Entity \ Enterprise \ Enterprise :: $ getId

Thank you

    
asked by anonymous 16.03.2015 / 03:00

1 answer

0

The error itself has already given you the answer.

  

Notice: Undefined property: Proxy__CG __ \ JN \ Entity \ Enterprise \ Enterprise :: $ getId

In the next block, you call the property getId , which does not exist.

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

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

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