0
votes

I'm modifying CRUD generated by doctrine in symfony 2.8. I have 2 tables (proveedores(providers) and productos(products)) with a relation 1:n. I want show through index action the complete list of products with their providers that it owns. What I try is create a DQL sentence that joins the 2 tables in the result and then manipulate it with twig. When I try to access to index, I get the error mentioned.

[Semantical Error] line 0, col 75 near 'x ': Error: Class Cocina\ComprasBundle\Entity\Productos has no association named proveedores

[2/2] QueryException: [Semantical Error] line 0, col 75 near 'x ': Error: Class Cocina\ComprasBundle\Entity\Productos has no association named proveedores

[1/2] QueryException: SELECT p,x FROM ComprasBundle:Productos p JOIN p.proveedores x

I have revised another similar threads over here but I have no found solution. I have checked too clearing all the doctrine caches and symfony cache but nothing. Thanks for the answers.

ENTITY PROVEEDORES

namespace Cocina\ComprasBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Proveedores
 *
 * @ORM\Table(name="proveedores")
 * @ORM\Entity(repositoryClass="Cocina\ComprasBundle\Repository\ProveedoresRepository")
 */
class Proveedores
{
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="nombre", type="string", length=255, unique=true)
...
}

ENTITY PRODUCTOS

namespace Cocina\ComprasBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Productos
 *
 * @ORM\Table(name="productos")
 * @ORM\Entity(repositoryClass="Cocina\ComprasBundle\Repository\ProductosRepository")
 */
class Productos
{
/**
 * @var integer $idProveedor
 * @ORM\ManyToOne(targetEntity="Proveedores")
 * @ORM\JoinColumn(name="id_proveedor_id", referencedColumnName="id")
 */
private $idProveedor;

...
}

PRODUCTOS REPOSITORY

namespace Cocina\ComprasBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * ProductosRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ProductosRepository extends EntityRepository
{
public function findListaProductos()
{
    $em=$this->getEntityManager();
    $consulta=$em->createQuery('
            SELECT p,x
            FROM ComprasBundle:Productos p 
            JOIN p.proveedores x            
            ');
    return $consulta->getResult();
}
}

CONTROLLER

namespace Cocina\ComprasBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Cocina\ComprasBundle\Entity\Productos;
use Cocina\ComprasBundle\Form\ProductosType;

/**
 * Productos controller.
 *
 */
class ProductosController extends Controller
{
/**
 * Lists all Productos entities.
 *
 */
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    //$productos = $em->getRepository('ComprasBundle:Productos')->findAll();
    $productos=$em->getRepository('ComprasBundle:Productos')->findListaProductos()->getResult();

    return $this->render('ComprasBundle:productos:index.html.twig', array(
        'productos' => $productos,
    ));
}

RELATIONSHIP SCHEMA

2
In the class Productos rename the attribute $idProveedor with $proveedores - Matteo

2 Answers

0
votes

It seems you are not joining correctly. It should be something like: FROM tablename INNER JOIN othertablename ON tablename.someid = othertablename.someid.

However, with doctrine you don't need to do that. You can just call a function from your entity.

See this. You should generate your getters and setters. (something like php bin/console doctrine:generate:entities AppBundle).

Then you can just query your productos (pseudocode not tested).

     $productos = $this->getDoctrine()
    ->getRepository('ComprasBundle:Productos')
    ->findAll() or fetchAll();

And then you have your proveedores in your objects. It probably returns an array of objects. Pseudocode:

      $firstproductprovidername  = $productos[0]->getProveedores()->getName();
      $firstproductproviderid  = $productos[0]->getProveedores()->getId();
0
votes

In the Entity Productos the relation is named idProveedor instead of proveedores. So try this query:

public function findListaProductos()
{
    $em=$this->getEntityManager();
    $consulta=$em->createQuery('
            SELECT p,x
            FROM ComprasBundle:Productos p 
            JOIN p.idProveedor x            
            ');
    return $consulta->getResult();
}

Hope this helps

NB: As previous comment probably is better renaming the relation as expected in your query as proveedores