0

我有一个看起来像的实体

    .
.
.
    /**
     * @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;
}

现在,如果我尝试访问这些值,我会收到一个 Join Column 错误。

我在用着 :

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

这对我来说有点令人困惑,因为我遇到了:

试图在类“Doctrine\ORM\PersistentCollection”上调用方法“getId”。500 内部服务器错误 - UndefinedMethodException

我过去曾多次这样做,我还可以使用 {{ item.stock.id }} ... 访问 twig 中的股票值

我究竟做错了什么??

4

1 回答 1

1

因为$stockvariable 是 a Doctrine\Common\Collections\ArrayCollection,所以$item->getStock()->getId()你应该尝试类似$item->getStock()->first()->getId());orvar_dump($item->getStock()在提取元素之前查看结构。

于 2015-12-28T15:10:03.177 回答