简体   繁体   中英

Get OneToMany association from ManyToOne

I'm new to frameworks and Symfony and I'm trying to learn some basics.

I have a OneToMany association from an entity called Product. It's inverse is ManyToOne association from an entity called Description. I'm trying to get the description to show in my twig file from my product controller.

In Product entity I have:

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;

    // MORE CODE BELOW....

    /**
     * Creates Constructor for ArrayCollection
     */
    public function __construct() 
    {
        $this->descriptions = new ArrayCollection();
    }

    MORE CODE...
    /**
     * Add descriptions
     *
     * @param \Pas\ShopTestBundle\Entity\Description $descriptions
     * @return Product
     */
    public function addDescription(\Pas\ShopTestBundle\Entity\Description $descriptions)
    {
        $this->descriptions[] = $descriptions;

        return $this;
    }

    /**
     * Remove descriptions
     *
     * @param \Pas\ShopTestBundle\Entity\Description $descriptions
     */
    public function removeDescription(\Pas\ShopTestBundle\Entity\Description $descriptions)
    {
        $this->descriptions->removeElement($descriptions);
    }

    /**
     * Get descriptions
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getDescriptions()
    {
        return $this->descriptions;
    }

    /**
     * Converts Product Name to a Viewable String
     * @return String
     */
    public function __toString() 
    {
        return $this->getName();

        return $this->getDescriptions();
    }
}

I'm trying to get the description to appear in my 'showAction', which routes to show.html.twig. In that function I have:

 /**
 * Finds and displays a Product entity.
 *
 * @Route("/{id}", name="product_show")
 * @Method("GET")
 * @Template()
 */
public function showAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('PasShopTestBundle:Product')->find($id);

    // $descriptionInfo = $em->getRepository('PasShopTestBundle:Description')
    //     ->find($id)
    //     ->getProductDesciption();

    //get description to appear on show page

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Product entity.');
    } else {
        $productInfo = $entity->getDescriptions();
    }

    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity'      => $entity,
        'delete_form' => $deleteForm->createView(),
    );
}

As you can see, I've tried a few things, however, I'm not sure if it's right.

In show.html.twig I have:

<tr>
    <th>Description</th>
    {% for description in descriptions %}
        <td>{{ entity.description }}</td>
    {% endfor %}
{# --------- Need Description Show.html.twig to go to above ------------------ #}
</tr>

As it stands now, if I go to my 'showAction' route I get error:

Variable "descriptions" does not exist in src/Pas/ShopTestBundle/Resources/views/Product/show.html.twig at line 22 (where you see the first descriptions is line 22...)

In descriptions entity and its controller, everything works. I can enter a description with an ID corresponding to the ID from product entity/controller. In all I want the description I enter there to appear in product. (I hope this makes sense)

Description 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 Product
     *
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="descriptions")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     */
    private $product;

I'm sure I'm close but I can't quite figure it out. Any help is appreciated and thanks in advanced!

You are not passing descriptions in your controller to the view.

Also, your view is wrong. you loop over descriptions and then ask for entity.description

either pass the descriptions variable to your view or change the view to

<tr>
    <th>Description</th>
    {% for description in entity.descriptions %}
        <td>{{ description }}</td>
    {% endfor %}
</tr>

And maybe show your Description class also.

In your controller, you are passing the following variables to the view:

return array(
    'entity'      => $entity,
    'delete_form' => $deleteForm->createView(),
);

But then in twig you are referencing a descriptions var in this line:

{% for description in descriptions %}

That's the error. In your view you only have the entity var and that entity has many descriptions, so what you try to do is:

{% for description in entity.descriptions %}
    <td>{{ description.text }}</td>
{% endfor %}

Hope you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM