0

I have an entity which looks like

    .
.
.
    /**
     * @ORM\ManyToMany(targetEntity="PrLeadBundle\Entity\Stock")
     * @ORM\JoinColumn(name="stock_id", referencedColumnName="id", nullable=true)
     */
    private $stock;
public function __construct()
{
    $this->stock = new \Doctrine\Common\Collections\ArrayCollection();
}



/**
 * Add stock
 *
 * @param \PrLeadBundle\Entity\Stock $stock
 * @return ValueList
 */
public function addStock(\PrLeadBundle\Entity\Stock $stock)
{
    $this->stock[] = $stock;

    return $this;
}

/**
 * Remove stock
 *
 * @param \PrLeadBundle\Entity\Stock $stock
 */
public function removeStock(\PrLeadBundle\Entity\Stock $stock)
{
    $this->stock->removeElement($stock);
}

/**
 * Get stock
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getStock()
{
    return $this->stock;
}

Now, If I try to access theese values I get an Join Column error.

I'm using :

  $Values = $entityManager->getRepository('PrLeadBundle:ValueList')->findBy(array('disabled' => '0','exportable'=>'1'), array('title' => 'ASC'));
foreach ($Values as $item){
    var_dump($item->getStock()->getId());
}

This is a bit confusing for me cause i ran into an :

Attempted to call method "getId" on class "Doctrine\ORM\PersistentCollection". 500 Internal Server Error - UndefinedMethodException

I did thsi for certain times in past, and I can also access the stock values in twig by using {{ item.stock.id }} ...

What am I doing wrong??

TheTom
  • 934
  • 2
  • 14
  • 40

1 Answers1

1

Because $stock variable is a Doctrine\Common\Collections\ArrayCollection, so instead of $item->getStock()->getId() you should try something like $item->getStock()->first()->getId()); or var_dump($item->getStock() to see the structure before extracting the element.

JC Sama
  • 2,214
  • 1
  • 13
  • 13