Need help getting OneToMany from ManyToOne

Let me start by saying that I asked a similar question earlier and got an answer. I tried to use these principles here, but I was stuck again, I donโ€™t know where to go from here.

I have a page where I list all the "products" along with their respected identifiers, price and name. On the same page I want to get a description that I created for each of them. Description is its own essence and has its own controller.

In mine ProductController, in mine indexAction, I'm trying to get a description to appear here.

The problem is that I do not reference the id inside indexAction(I use findAll). I tried to browse all the products and links using $key, but I will either get the most recent description entered in the description, or currently:

Error: calling the getDescriptions () member function for a non-object.

EDIT: I should mention that $ prodEnt is null ...

I didnโ€™t want to come here for help, but I no longer have thoughts on how to do what I want.

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

Here is the indexActionroute tweet file:

{% 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 %}

Product:

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;

Description Object:

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;

Any help really appreciated, thanks!

+4
source share
3 answers

. , , . , . , , ... , , , , - , . 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 %}
+2

, .

, , .

foreach ($entities as $entity) {
   // To get the descriptions...
   $entity->getDescriptions();
}

, , . , , , . :

:

$entities .

$entity ,

$descriptions

.

:

entities entity, entity, . $entity $descriptions , entities, .

+1

Perhaps you are working with a unified object, try setting the sampling level.

In your product object, you must set the level of sampling from the relationship.

Relationship fixing:

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", fetch="EAGER")
     */
    private $descriptions;
0
source

Source: https://habr.com/ru/post/1628546/


All Articles