php – 需要帮助从ManyToOne获取OneToMany关联
让我先说我之前问了一个类似的问题然后得到答案.我试着在这里使用这些原则,但我再次陷入困境,我不知道从哪里开始.
我有一个页面,列出所有’产品’以及他们尊重的ID,价格和名称.在同一页面上,我想获得我为每个人创建的描述.描述是它自己的实体,并拥有自己的控制器. 在我的ProductController中,在我的indexAction中,我试图让描述出现在这里. 问题是,我没有在indexAction中引用id(我使用findAll).我试图使用$key循环遍历所有产品和引用,但我要么在描述中输入最新描述,要么: 错误:在非对象上调用成员函数getDescriptions(). 编辑:我应该提到$prodEnt为null … 我不想来这里寻求帮助,但我对如何获得我想要的东西没有更多的想法. 这是带有indexAction的ProductController: namespace PasShopTestBundleController; use SymfonyComponentHttpFoundationRequest; use SymfonyBundleFrameworkBundleControllerController; use SensioBundleFrameworkExtraBundleConfigurationMethod; use SensioBundleFrameworkExtraBundleConfigurationRoute; use SensioBundleFrameworkExtraBundleConfigurationTemplate; use PasShopTestBundleEntityProduct; use PasShopTestBundleFormProductType; /** * 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,); } 这是indexActions Route twig文件: {% 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 PasShopTestBundleEntity; use DoctrineORMMapping as ORM; use DoctrineCommonCollectionsArrayCollection; /** * Product * * @ORMTable(name="products") * @ORMEntity(repositoryClass="PasShopTestBundleEntityProductRepository") */ class Product { /** * @var ArrayCollection * * @ORMOneToMany(targetEntity="Description",mappedBy="product") */ private $descriptions; 描述实体: namespace PasShopTestBundleEntity; use DoctrineORMMapping as ORM; use DoctrineCommonCollectionArrayCollection; /** * Description * * @ORMTable(name="descriptions") * @ORMEntity(repositoryClass="PasShopTestBundleEntityDescriptionRepository") */ class Description { /** * @var string * * @ORMColumn(name="name",type="string") */ private $productDescription; /** * @var Product * * @ORMManyToOne(targetEntity="Product",inversedBy="descriptions") * @ORMJoinColumn(name="product_id",referencedColumnName="id") */ private $product; 非常感谢任何帮助,谢谢! 解决方法
你的情况过于复杂.检索所有产品实体时,除了在响应中返回这些实体之外,您无需执行任何其他操作.每个实体在类定义中都已将其描述与其关联.此外,在典型的索引操作中,如果不存在产品,则不一定需要抛出异常…您可能仍希望向用户显示索引页,然后如果没有任何产品,他们只会看到一个空表.你的indexAction()可以简单地是:
public function indexAction() { $em = $this->getDoctrine()->getManager(); $entities = $em->getRepository('PasShopTestBundle:Product')->findAll(); return array( 'entities' => $entities,); } 如果单个产品有多个描述,您的Twig也会引起问题,因为< td>的数量.单元格每行都是可变的.最好做一些像显示逗号分隔的描述列表,或者可能用< br>分隔.或者< p>,或者您甚至可以通过< ul>将它们作为无序列表和< li>: {% 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 description in entity.descriptions %} {{ description.productDescription }}{% if not loop.last %},{% endif %} {% endfor %} {# ... #} </tr> {% endfor %} 该示例给出了逗号分隔的描述列表,但您可以根据需要更改该循环.此外,您可以向Description实体添加__toString()方法,以避免必须直接调用该productDescription字段: // class Description public function __toString() { return $this->getProductDescription(); } 然后,这将允许您为您的Twig执行以下操作: {% for description in entity.descriptions %} {{ description }}{% if not loop.last %},{% endif %} {% endfor %} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- php – jQuery $(this).serialize()不会发送未更改的输入
- 简单了解将WordPress中的工具栏移到底部的小技巧
- php使用unset()删除数组中某个单元(键)的方法
- 利用PHP获取访客IP、地区位置、浏览器及来源页面等信息
- php – Codeigniter 2 index和__construct之间的区别以及__
- php – HTML表单Button值不会在Safari和Chrome中发布
- php – 函数eregi()已弃用
- PHP编程:php中使用in_array() foreach array_search() 查找
- php – jQuery Mobile:如何正确提交表单数据
- php批量缩放图片的代码[ini参数控制]