我先说我以前问过一个类似的问题,然后得到了答案。我试着在这里运用这些原则,但我又陷入了困境,不知道从这里走到哪里。
我有一个页面,我列出了所有的'产品'连同他们尊敬的身份证,价格和名称。在同一页上,我想得到我为他们每个人创建的描述。描述是它自己的实体,有自己的控制器。
在我的ProductController中,在我的indexAction中,我试图将描述显示在此处。
问题是,我没有引用indexAction中的id(我使用findall)。我试图使用$key循环浏览所有产品和引用,但我要么在“说明”中输入最新的说明,要么当前:
错误:对非对象调用成员函数getDescriptions()。
编辑:我应该提到$prodent是空的…
我不想来这里寻求帮助,但我对如何得到我想要的东西没有更多的想法。
这里有ProductControllerindexAction

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,
        );
    }

以下是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 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;

任何帮助都非常感谢,谢谢!

最佳答案

你把情况搞得太复杂了。当您检索所有产品实体时,除了在响应中返回这些实体外,不需要执行任何其他操作。在类定义中,每个实体都有与其关联的描述。另外,在典型的索引操作中,如果不存在任何产品,则不一定要抛出异常……您可能仍然希望向用户显示索引页,然后如果没有任何产品,则只会看到一个空表。您的indexAction()可以是:

public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('PasShopTestBundle:Product')->findAll();

    return array(
        'entities' => $entities,
    );
}

当一个产品有多个描述时,你的小树枝也会引起问题,因为每行的<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();
}

这将允许您对您的树枝执行以下操作:
{% for description in entity.descriptions %}
    {{ description }}{% if not loop.last %}, {% endif %}
{% endfor %}

10-02 20:26