讓我先說我之前問過類似的問題,並得到了答案。我嘗試在這裏使用這些原則,但我再次被卡住了,我不知道該從哪裏出發。需要幫助從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;
任何幫助真的很感激,謝謝!
所以有可以是單個產品的多個描述,是的?這就是你想要展示的東西? –
是的,可能會有。目前的情況是,現在每個產品只有一個說明,以便只說明會顯示 –