"Error: Class FrontEndBundle \ Entity \ BrandAuto has no association named id" in Symfony 3, when specifying an association in two entities

0

I have two entities MarcaAuto and ModeloAuto with the following definition:

MarcaAuto

<?php

namespace FrontEndBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * MarcaAuto
 *
 * @ORM\Table(name="marcas_auto")
 * @ORM\Entity(repositoryClass="FrontEndBundle\Repository\MarcaAutoRepository")
 */
class MarcaAuto
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\OneToMany(targetEntity="ModeloAuto", mappedBy="idMarca")
     */
    private $id;


    /**
     * @ORM\Column(type="string", length=100)
     */
    private $marca;

ModeloAuto

<?php

namespace FrontEndBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * ModeloAuto
 *
 * @ORM\Table(name="modelos_auto")
 * @ORM\Entity(repositoryClass="FrontEndBundle\Repository\ModeloAutoRepository")
 */
class ModeloAuto {

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var int
     * @ORM\Column(name="id_marca", type="integer")
     * @ORM\ManyToOne(targetEntity="MarcaAuto", inversedBy="id")
     * @ORM\JoinColumn(name="id_marca", referencedColumnName="id")
     */
    private $idMarca;

By making a statement about the entity in my controller in this way:

AcmeController

$em = $this->getDoctrine()->getManager();
        $query = $em->createQuery(
                'SELECT a
                FROM FrontEndBundle:MarcaAuto a
                LEFT JOIN a.id
                ORDER BY a.marca ASC'
        );

        $marcasVehiculos = $query->getResult();

I get the following error:

  

[Semantical Error] line 0, col 68 near 'ORDER BY a.brand': Error:   Class FrontEndBundle \ Entity \ MarcaAuto has no association named id 500   Internal Server Error - QueryException 1 linked Exception:   QueryException »

I have reviewed the documentation again and again and I can not find the error.

    
asked by je134 20.02.2017 в 14:18
source

3 answers

0

I solved my problem in the following way, although I do not know what is the reason why it does not work with the initial example.

I removed the following line from the ModeloAuto entity:

* @ORM\Column(name="id_marca", type="integer")

With the above, a foreign key was generated in the "models_autos" table that was not previously generated when calling the command: "php bin / console doctrine: schema: update --force"

I changed the select to:

 'SELECT a
                FROM FrontEndBundle:ModeloAuto a
                LEFT JOIN a.idMarca b
                '

Apparently there is some kind of problem when trying to generate an association to another table when the attribute is a primary key.

Edition 1

I was able to identify the problem of my original question and this is the solution:

MarcaAuto:

/**
 * MarcaAuto
 *
 * @ORM\Table(name="marcas_auto")
 * @ORM\Entity(repositoryClass="FrontEndBundle\Repository\MarcaAutoRepository")
 */
class MarcaAuto {

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="ModeloAuto", mappedBy="idMarca")
     */
    private $modelos;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $marca;

ModeloAuto:

/**
 * ModeloAuto
 *
 * @ORM\Table(name="modelos_auto")
 * @ORM\Entity(repositoryClass="FrontEndBundle\Repository\ModeloAutoRepository")
 */
class ModeloAuto {

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var int
     * @ORM\ManyToOne(targetEntity="MarcaAuto", inversedBy="modelos")
     * @ORM\JoinColumn(name="id_marca", referencedColumnName="id")
     */
    private $idMarca;

AcmeController:

$em = $this->getDoctrine()->getManager();

        $query = $em->createQuery(
                'SELECT a, b
                FROM FrontEndBundle:MarcaAuto a
                LEFT JOIN a.modelos b
                '
        );
    
answered by 20.02.2017 / 17:00
source
0

Your LEFT JOIN has an error:

SELECT a
                FROM FrontEndBundle:MarcaAuto a
                LEFT JOIN a.id
                ORDER BY a.marca ASC

You do not tell the SQL what you want to do the JOIN with:

LEFT JOIN a.id ON  #... La otra tabla y la columna donde quieres hacer JOIN
    
answered by 20.02.2017 в 14:34
0

Doctrine is not equal to PDO unless it is a custom Query you could access the object check this link to me helped me solve that problem, link the database is accessed as if they were objects, if it were correct, you could simply access it as if it were an object without having to make a Query with DQL. you could from the console update the schema of your database to match your entities.

    
answered by 21.02.2017 в 02:15