【发布时间】:2016-02-11 14:12:22
【问题描述】:
我是框架和 Symfony 的新手,我正在尝试学习一些基础知识。
我有一个来自名为 Product 的实体的 OneToMany 关联。它的反面是来自名为 Description 的实体的 ManyToOne 关联。我正在尝试从我的产品控制器获取描述以显示在我的树枝文件中。
在产品实体中我有:
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Product
*
* @ORM\Table(name="products")
* @ORM\Entity(repositoryClass="Pas\ShopTestBundle\Entity\ProductRepository")
*/
class Product
{
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Description", mappedBy="product")
*/
private $descriptions;
// MORE CODE BELOW....
/**
* Creates Constructor for ArrayCollection
*/
public function __construct()
{
$this->descriptions = new ArrayCollection();
}
MORE CODE...
/**
* Add descriptions
*
* @param \Pas\ShopTestBundle\Entity\Description $descriptions
* @return Product
*/
public function addDescription(\Pas\ShopTestBundle\Entity\Description $descriptions)
{
$this->descriptions[] = $descriptions;
return $this;
}
/**
* Remove descriptions
*
* @param \Pas\ShopTestBundle\Entity\Description $descriptions
*/
public function removeDescription(\Pas\ShopTestBundle\Entity\Description $descriptions)
{
$this->descriptions->removeElement($descriptions);
}
/**
* Get descriptions
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getDescriptions()
{
return $this->descriptions;
}
/**
* Converts Product Name to a Viewable String
* @return String
*/
public function __toString()
{
return $this->getName();
return $this->getDescriptions();
}
}
我试图让描述出现在我的“showAction”中,该路径指向 show.html.twig。在那个函数中我有:
/**
* Finds and displays a Product entity.
*
* @Route("/{id}", name="product_show")
* @Method("GET")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('PasShopTestBundle:Product')->find($id);
// $descriptionInfo = $em->getRepository('PasShopTestBundle:Description')
// ->find($id)
// ->getProductDesciption();
//get description to appear on show page
if (!$entity) {
throw $this->createNotFoundException('Unable to find Product entity.');
} else {
$productInfo = $entity->getDescriptions();
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
如您所见,我已经尝试了一些方法,但是我不确定它是否正确。
在 show.html.twig 我有:
<tr>
<th>Description</th>
{% for description in descriptions %}
<td>{{ entity.description }}</td>
{% endfor %}
{# --------- Need Description Show.html.twig to go to above ------------------ #}
</tr>
就目前而言,如果我去我的“showAction”路线,我会得到错误:
第 22 行的 src/Pas/ShopTestBundle/Resources/views/Product/show.html.twig 中不存在变量“描述” (你看到的第一个描述是第 22 行...)
在描述实体及其控制器中,一切正常。我可以输入与产品实体/控制器的 ID 对应的 ID 的描述。总之,我希望我在那里输入的描述出现在产品中。 (我希望这是有道理的)
描述实体:
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collection\ArrayCollection;
/**
* Description
*
* @ORM\Table(name="descriptions")
* @ORM\Entity(repositoryClass="Pas\ShopTestBundle\Entity\DescriptionRepository")
*/
class Description
{
/**
* @var Product
*
* @ORM\ManyToOne(targetEntity="Product", inversedBy="descriptions")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
我确定我很接近,但我无法完全弄清楚。任何帮助表示赞赏,并提前感谢!
【问题讨论】:
标签: php entity-framework symfony orm