让我先说我之前问过类似的问题,并得到了答案。我尝试在这里使用这些原则,但我再次被卡住了,我不知道该从哪里出发。需要帮助从ManyToOne获取OneToMany关联
我有一个页面,列出所有'产品'以及他们尊敬的编号,价格和名称。在这个相同的页面上,我想获得我为他们创建的描述。说明是它自己的实体,并有它自己的控制器。
在我的ProductController
中,在我的indexAction
中,我试图让描述出现在这里。
问题是,我没有在indexAction
(我使用findAll)中引用一个id。我试图通过使用$key
来遍历所有产品和参考,但我可以获取描述中或当前输入的最近描述:
错误:在非对象上调用成员函数getDescriptions()。
编辑:我应该提到$ prodEnt为空...
我不希望到这里来寻求帮助,但我对如何去获得我想要的东西没有更多的想法。
这里是ProductController
与indexAction
:
namespace Pas\ShopTestBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Pas\ShopTestBundle\Entity\Product;
use Pas\ShopTestBundle\Form\ProductType;
/**
* Product controller.
*
* @Route("/product")
*/
class ProductController extends Controller
{
/**
* Lists all Product entities.
*
* @Route("/", name="product")
* @Method("GET")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('PasShopTestBundle:Product')->findAll();
//$entities = $em->getRepository('PasShopTestBundle:Product')->find($id);
//var_dump($entities);
//dump($entities); die;
if (!$entities) {
throw $this->createNotFoundException('Error Nothing in Entities.');
}
else {
//dump($entities); die;
foreach ($entities as $key => $entity) {
//dump($entities); die;
//dump($entity); die;
//dump($key); die; //needs to be 1
//$entity = $em->getRepository('PasShopTestBundle:Product')->findAll($key);
$prodEnt = $em->getRepository('PasShopTestBundle:Product')->find($key);
//dump($entity); die;
//dump($prodEnt); die;
$descriptions = $prodEnt->getDescriptions();
//dump($entity); die;
}
//dump($entities); die;
}
return array(
'descriptions' => $descriptions,
'entities' => $entities,
'entity' => $entity,
);
}
这里是indexAction
思想路线树枝文件:
{% extends '::base.html.twig' %}
{% block body -%}
<h1>Product List</h1>
<table class="records_list">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<td><a href="{{ path('product_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>
<td>{{ entity.name }}</td>
<td>{{ entity.price }}</td>
<td>{{ entity.quantity }}</td>
{% for key, entity in descriptions %}
<pre>{{ dump(entity) }}</pre>
{# <pre>{{ dump(key) }}</pre> #}
<td>{{ entity.productDesciption }}</td>
<pre>{{ dump(entity.productDesciption) }}</pre>
{% endfor %}
<td>
<ul>
<li>
<a href="{{ path('product_cart', { 'id': entity.id }) }}">Add Product To Cart</a>
</li>
<li>
<a href="{{ path('product_show', { 'id': entity.id }) }}">show</a>
</li>
<li>
<a href="{{ path('product_edit', { 'id': entity.id }) }}">edit</a>
</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<ul>
<li>
<a href="{{ path('product_new') }}">
Create a new entry
</a>
</li>
</ul>
{% endblock %}
产品实体:
namespace Pas\ShopTestBundle\Entity;
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;
描述实体:
namespace Pas\ShopTestBundle\Entity;
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 string
*
* @ORM\Column(name="name", type="string")
*/
private $productDescription;
/**
* @var Product
*
* @ORM\ManyToOne(targetEntity="Product", inversedBy="descriptions")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
任何帮助真的很感激,谢谢!
所以有可以是单个产品的多个描述,是的?这就是你想要展示的东西? –
是的,可能会有。目前的情况是,现在每个产品只有一个说明,以便只说明会显示 –