[英]Symfony3 Doctrine querybuilder
I am writing Symfony3 app using Doctrine ORM. 我正在使用Doctrine ORM编写Symfony3应用程序。
I have a entity with all my attributes and their setters and getters. 我有一个实体,其中包含我的所有属性以及其设置方法和获取方法。
Trying to build so that i can retrieve a row based on a email address. 尝试构建以便我可以根据电子邮件地址检索行。
my query:
/**
* @param $email
*/
public function getUserByEmail($email)
{
$repository = $this->db->EntityManagerORM()
->getRepository('TestBundle:User');
$query = $repository->createQueryBuilder('t')
->where('t.emial = :email')
->setParameter(':email', $email)
->getQuery();
$test = $query->getResult();
var_dump($test);
$this->db is a EntityManager which is a service and instantiated in my __construct. $ this-> db是EntityManager,它是一项服务,在我的__construct中实例化。 so what i do is retrieve the entity, they create a queryBuilder where i want to retrieve a entire row which email == to the given email. 所以我要做的就是检索实体,他们创建了一个queryBuilder,我想在其中检索电子邮件==到给定电子邮件的整行。
response I get: 我得到的回应:
"Notice: Undefined index: [Semantical Error] line 0, col 63 near 'emial
= :ema': Error: Class Test\\TestBundle\\Entity\\User has no field or =:ema':错误:类Test \\ TestBundle \\ Entity \\ User没有字段或
association named emial" 名为emial的协会”
anything suspicious in my code....? 我的代码中有任何可疑的...。
There is no need for :
in setParameter function. 在setParameter函数中不需要:
。
Another thing wrong in your code is, the User table has no column with the name emial
. 代码中的另一件事是,User表没有名称为emial
列。 Double check and replace it with appropriate name. 仔细检查并用适当的名称替换。
So your query will look something like this, 因此,您的查询将如下所示:
$query = $repository->createQueryBuilder('t')
->where('t.RIGHT_COLUMN_NAME = :email')
->setParameter('email', $email)
->getQuery();
In above code replace RIGHT_COLUMN_NAME
with the correct column name. 在上面的代码中,将RIGHT_COLUMN_NAME
替换为正确的列名。
it's the spelling of email. 这是电子邮件的拼写。 Change to this: 更改为此:
$query = $repository->createQueryBuilder('t')
->where('t.email = :email')
->setParameter(':email', $email)
->getQuery();
That should fix the problem. 那应该解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.