Symfony 尝试执行查询时PathExpression无效
我不知道我还能尝试什么 我得到了这个错误: [语义错误]第0行,第10列“idEntrada FROM”附近:错误:无效的PathExpression。必须是StateFieldPathExpression 这是我的疑问:Symfony 尝试执行查询时PathExpression无效,symfony,doctrine,dql,Symfony,Doctrine,Dql,我不知道我还能尝试什么 我得到了这个错误: [语义错误]第0行,第10列“idEntrada FROM”附近:错误:无效的PathExpression。必须是StateFieldPathExpression 这是我的疑问: $query=$em->createQuery("SELECT mp.idEntrada FROM PAVPrimerAvisoBundle:ModeradoPor mp WHERE mp.idUsuario = $userId"); 这是我的实体: class Mo
$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");
或
工作完美。只是和议员艾德拉达在一起
我的实体中有任何打字错误吗
编辑:在mp.idUsuario中也会发生。
编辑:
例如,我转储mysql查询,如下所示(当选择mp
)
我也可以选择mp.id
但从不使用mp.idEntrada/mp.idUser
SELECT IDENTITY (mp.entrada)FROM PAVPrimerAvisoBundle:ModeradoPor mp WHERE mp.idUsuario = $userId
这就解决了问题。请注意
“IDENTITY”
您需要使用实体FK定义来获取PK字段(属性)
例如:
我有一个愿望:
- 文档文档(文档之间有关系的实体)
- documento(文档类型为FK的文档信息)
- documentTipo(具有文档类型\u id->(我的目标!!)的引用实体)
/**
* @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中选择数据并从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。你的目标是得到你的
ModeradoPor
实体的相关Entrada
对象(或者Entrada\u id
),其中mp.idUsuario=$userId
?我不想得到整个对象,只有id。但该id只是Entity Entity的外键。是否有原因使用createQuery
而不是$em->getRepository('MyBundle:ModeradoPor')->find($id)
?是的,因为该查询当前正在调试,只是子查询的一部分。如果您仍然拥有我的答案中的代码,$query=$em->createQuery(“从PAVPrimerAvisoBundle:ModeradoPor mp中选择mp.entrada\u id,其中mp.idUsuario=$userId”)代码>应该有效,是这样吗?你能给我看一下关于身份证明文件的链接吗?我一直在寻找它,但我不知道它是什么或要寻找什么,所以我找到了无关的话题。为什么它现在起作用了?提前非常感谢!很抱歉我不知道。我在不记得的地方找到了答案,但不是官方文件。我在其他问题中找到了文件:
[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
************************
$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