Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid PathExpression while trying to do a query

I don't know what more I can try.

I'm getting this error:

[Semantical Error] line 0, col 10 near 'idEntrada FROM': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

This is my query:

$query=$em->createQuery("SELECT mp.idEntrada FROM PAVPrimerAvisoBundle:ModeradoPor mp WHERE mp.idUsuario = $userId");

And this is my Entity:

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

/**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumn(name="user", referencedColumnName="id")
 */
private $idUsuario;

/**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="Entrada")
 * @ORM\JoinColumn(name="idEntrada", referencedColumnName="id")
 */
private $idEntrada;

/**
 * @var integer
 *
 * @ORM\Column(name="votacio", type="integer")
 */
private $votacio;

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set idUsuario
 *
 * @param integer $idUsuario
 * @return ModeradoPor
 */
public function setIdUsuario($idUsuario)
{
    $this->idUsuario = $idUsuario;

    return $this;
}

/**
 * Get idUsuario
 *
 * @return integer 
 */
public function getIdUsuario()
{
    return $this->idUsuario;
}

/**
 * Set idEntrada
 *
 * @param integer $idEntrada
 */
public function setIdEntrada($idEntrada)
{
    $this->idEntrada = $idEntrada;
}

/**
 * Get idEntrada
 *
 * @return integer 
 */
public function getIdEntrada()
{
    return $this->idEntrada;
}

/**
 * Set votacio
 *
 * @param integer $votacio
 */
public function setVotacio($votacio)
{
    $this->votacio = $votacio;
}

/**
 * Get votacio
 *
 * @return integer 
 */
public function getVotacio()
{
    return $this->votacio;
}

If I do this query:

$query=$em->createQuery("SELECT mp FROM PAVPrimerAvisoBundle:ModeradoPor mp WHERE mp.idUsuario = $userId");

Or

$query=$em->createQuery("SELECT mp.id FROM PAVPrimerAvisoBundle:ModeradoPor mp WHERE mp.idUsuario = $userId");

works perfectly. It's just with mp.idEntrada.

Is there any typo in my Entity?

Edit: Happens too with mp.idUsuario.

EDIT: For example, I dump mysql query, and it's shown like this (when SELECT mp)

SELECT m0_.id AS id0, m0_.votacio AS votacio1, m0_.user AS user2, m0_.entrada_id AS entrada_id3 FROM ModeradoPor m0_ WHERE m0_.user = 5

I can also do SELECT mp.id

But never mp.idEntrada / mp.idUser

like image 977
Reinherd Avatar asked Dec 26 '22 08:12

Reinherd


2 Answers

SELECT IDENTITY (mp.entrada)FROM PAVPrimerAvisoBundle:ModeradoPor mp WHERE mp.idUsuario = $userId

This solved the issue. Notice the "IDENTITY"

like image 120
Reinherd Avatar answered Jan 13 '23 15:01

Reinherd


You need to use your entity FK definitions, to get an PK field (property).

Example:

I have a entities:

  • documentoDocumento ( entity with relations between documents )

  • documento ( document info with a document_Type_Id FK)

  • documentTipo ( entity of reference with a document_type_id -> (my target !!) )

In documentDocument entity:

/**
* @var Documento
*
* @ManyToOne(targetEntity="Documento")
* @JoinColumns({
*   @JoinColumn(name="documento2_id", referencedColumnName="id")
* })
*/
private $documento2;

In document entity:

/**
* @var DocumentoTipo
*
* @ManyToOne(targetEntity="DocumentoTipo")
* @JoinColumns({
*   @JoinColumn(name="documento_tipo_id", referencedColumnName="id")
* })
*/
private $documentoTipo;

IN TABLES


[DocumentoDocumento]  -- table with some type of relations between documents
* id              pk
- documento1_id   fk  
- documento2_id   fk

[Documento]
*id               pk
-documento_tipo   fk  -- type of document
- blablabla

[DocumentoTipo]
*id               pk
-tipo                 -- type description
************************

I want a select data from DocumentoDocumento and show tipo from DocumentoTipo.

This return an error:

 $qb3 = $this->_em->createQueryBuilder();
$qb3->select('dd.id, dd.documento, d2.contenidoTipo')
  ->from('Mnc\Entity\DocumentoDocumento', 'dd')    // select from DocumentoDocumento
  ->innerJoin('dd.documento2','d2' );               // join to 'd2' 

but with:

$qb3 = $this->_em->createQueryBuilder();
$qb3->select('dd.id, dd.documento, dt2.tipo documentoTipo, dt2.id documentoTipoId')
  ->from('Mnc\Entity\DocumentoDocumento', 'dd') // select from DocumentoDocumento
  ->innerJoin('dd.documento2','d2' )            // join to 'd2'
  ->innerJoin('d2.documentoTipo', 'dt2');       // join FK in d2 to dt2

I get a dt2.id with an alias 'documentoTipoId'.

like image 21
wagagt Avatar answered Jan 13 '23 15:01

wagagt