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;
class ProductController extends Controller
{
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('PasShopTestBundle:Product')->findAll();
if (!$entities) {
throw $this->createNotFoundException('Error Nothing in Entities.');
}
else {
foreach ($entities as $key => $entity) {
$prodEnt = $em->getRepository('PasShopTestBundle:Product')->find($key);
$descriptions = $prodEnt->getDescriptions();
}
}
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;
class Product
{
private $descriptions;
Description Object:
namespace Pas\ShopTestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collection\ArrayCollection;
class Description
{
private $productDescription;
private $product;
Any help really appreciated, thanks!