4

我不知道我还能尝试什么。

我收到此错误:

[语义错误] 第 0 行,第 10 列“idEntrada FROM”附近:错误:无效的路径表达式。必须是 StateFieldPathExpression。

这是我的查询:

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

这是我的实体:

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;
}

如果我执行此查询:

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

或者

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

完美运行。它只是与 mp.idEntrada。

我的实体中有错字吗?

编辑:也发生在mp.idUsuario.

编辑: 例如,我转储 mysql 查询,它显示如下(何时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

我也可以做 SELECT mp.id

但从来没有 mp.idEntrada / mp.idUser

4

2 回答 2

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

这解决了这个问题。注意"IDENTITY"

于 2013-02-27T11:34:08.223 回答
1

您需要使用实体 FK 定义来获取 PK 字段(属性)。

例子:

我有一个实体:

  • documentoDocumento(文档之间有关系的实体)

  • 文档(带有 document_Type_Id FK 的文档信息)

  • documentTipo(带有 document_type_id 的参考实体 ->(我的目标!!))

在 documentDocument 实体中:

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

在文档实体中:

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

在表格中


[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
************************

我想要从 DocumentoDocumento 中选择数据并从 DocumentoTipo 中显示 Tipo。

这会返回一个错误:

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

但有:

$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

我得到一个别名为“documentoTipoId”的 dt2.id。

于 2013-12-27T06:24:00.603 回答