首先让我说我之前问过类似的问题并得到了回答。我尝试在这里使用这些原则,但我再次陷入困境,我不确定从这里去哪里。
我有一个页面,其中列出了所有“产品”及其相关 ID、价格和名称。在同一页面上,我想获得我为每个人创建的描述。描述是它自己的实体并且有它自己的 Controller 。
在我的 ProductController
中,在我的 indexAction
中,我试图让描述出现在这里。
问题是,我没有在 indexAction
中引用 id(我使用 findAll)。我尝试使用 $key
遍历所有产品和引用,但我要么获得在描述中输入的最新描述,要么当前:
错误:调用非对象上的成员函数 getDescriptions()。
编辑:我应该提到 $prodEnt 是空的...
我不想来这里寻求帮助,但我对如何获得我想要的东西没有更多的想法。
这是带有 indexAction
的 ProductController
:
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
的 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 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;
非常感谢任何帮助,谢谢!
最佳答案
你把情况复杂化了。当您检索所有产品实体时,除了在响应中返回这些实体外,您无需执行任何其他操作。每个实体在您的类定义中都已具有与其相关联的描述。此外,在典型的索引操作中,如果不存在任何产品,您不一定需要抛出异常......您可能仍想向用户显示索引页面,然后如果没有任何产品,他们只会看到一个空的 table 。你的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 %}
该示例给出了一个以逗号分隔的描述列表,但您可以根据需要更改该循环。另外,您可以添加 __toString()
Description
的方法实体以避免必须调用那个 productDescription
直接字段:
// class Description
public function __toString()
{
return $this->getProductDescription();
}
这将允许您为 Twig 执行以下操作:
{% for description in entity.descriptions %}
{{ description }}{% if not loop.last %}, {% endif %}
{% endfor %}
关于php - 需要帮助从 ManyToOne 获得 OneToMany 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35368492/