4

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

Reinherd
  • 5,476
  • 7
  • 51
  • 88
  • Your goal here is to get the associated `Entrada` object (or the `entrada_id`) of your `ModeradoPor` entity where `mp.idUsuario = $userId`? – cheesemacfly Feb 25 '13 at 20:09
  • I don't want to get whole object, just the id. But that id is just a foreign key for Entrada Entity. – Reinherd Feb 25 '13 at 20:11
  • Is there a reason why you use `createQuery` over something like `$em->getRepository('MyBundle:ModeradoPor')->find($id)`? – cheesemacfly Feb 25 '13 at 20:17
  • yes, because that query im currently debugging, is just a part of a subquery. – Reinherd Feb 25 '13 at 20:31
  • If you still have the code from my answer, `$query=$em->createQuery("SELECT mp.entrada_id FROM PAVPrimerAvisoBundle:ModeradoPor mp WHERE mp.idUsuario = $userId" );` should work, is it the case? – cheesemacfly Feb 25 '13 at 20:34
  • `Semantical Error] line 0, col 10 near 'entrada_id FROM': Error: Class PAV\PrimerAvisoBundle\Entity\ModeradoPor has no field or association named entrada_id `. Seems like entrada_id isn't the field to search for. Thats why I'm using `entrada` as I've declared it like `private $entrada;`. Maybe I am wrong? – Reinherd Feb 25 '13 at 20:37
  • Have you run `php app/console doctrine:generate:entities MyBundle` after changing your entities file? – cheesemacfly Feb 25 '13 at 20:54
  • Never used that command. I always used doctrine:schema:update... Anyways; i tried to but: `Generating entities for namespace "PrimerAviso" [RuntimeException] Namespace "PrimerAviso" does not contain any mapped entities. ` – Reinherd Feb 25 '13 at 21:19
  • Oh, finally could use that command. Anyways it's not working still. Same error. `: Invalid PathExpression. Must be a StateFieldPathExpression. ` – Reinherd Feb 25 '13 at 21:27
  • found this: http://stackoverflow.com/questions/14216470/symfony2-and-doctrine-error-invalid-pathexpression-must-be-a-statefieldpathe Does it help? – cheesemacfly Feb 25 '13 at 21:38
  • Damn. I've to love you. This is working: `SELECT IDENTITY (mp.entrada)`. Does any1 understand why? Anyways, @cheesemacfly, can you please post this as a Answer so I can tick it as solved? Thanks! – Reinherd Feb 25 '13 at 21:58
  • Did you keep the Entities deifintion as suggested in my answer or did it not help? I will complete my answer but the one with the `SELECT IDENTITY (mp.entrada)` definitely deserves an upvote! – cheesemacfly Feb 25 '13 at 21:59
  • Well, it wasn't the issue at all. I've changed again my code to the original, and its working. The issue here was that **Select IDENTITY**. The rest was fine ^^ – Reinherd Feb 25 '13 at 22:02
  • Then my answer is not valid! I will delete it :) – cheesemacfly Feb 25 '13 at 22:04

2 Answers2

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

This solved the issue. Notice the "IDENTITY"

Reinherd
  • 5,476
  • 7
  • 51
  • 88
  • Can you show me any link about documentation of IDENTITY? I've been looking for it but i have no clue about what is it or what to look for so I've found unrelated topics. Why it is working now? Thank you very much in advance! – NewRehtse Nov 26 '13 at 14:27
  • Sorry. I've no clue. I found answer somewhere I can't remember, but not official documentation. – Reinherd Nov 26 '13 at 16:11
  • 1
    I found the documentation in other question: [Doctrine DQL Selects Examples](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html) – NewRehtse Dec 20 '13 at 13:16
1

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'.

wagagt
  • 11
  • 2