【发布时间】:2016-01-28 06:57:26
【问题描述】:
我必须更新多个字段,当我尝试使用外键 ID 更新其他表中的字段时,使用 getRepository() 和 findOneById() 获取错误作为无法识别的字段,因此后来尝试使用查询生成器来实现它。但是查询没有得到执行得到像未定义字段这样的错误。
这是我尝试过的代码:
$this->em
->getRepository('Application_Entity_Company', 'c')
->findOneBy(array('c.userId'=>$post['user_id']));
和
$qb->update('Application_Entity_Company', 'c')
->set('c.name', $post['name'])
->set('c.mobile', $post['mobile'])
->set('c.email', $post['email'])
->where($qb->expr()
->eq('c.userId', ':id'))
->setParameter('id', $post['user_id'])
->getQuery()
->execute();
这里userId 是外键。我必须使用userId 更新用户实体中的用户详细信息字段。
【问题讨论】:
-
您正在更新公司实体,您必须更新用户实体。我猜你应该像这样使用: $qb->update('Application_Entity_User', 'u')->set('u.name', $post['name'])->set('u.mobile', $ post['mobile'])->set('u.email', $post['email'])->where('u.userId', ':id'))->setParameter('id', $ post['user_id'])->getQuery()->execute();
-
我必须在公司实体中更新,更新的字段在公司中有,但我必须根据 userId 进行更新。它在用户实体中。
-
你能发布你的错误信息吗?我认为 c.userId 在这里可能会出错,您在实体中给出的关系字段名称将会出现。我已经基于这句话发表了之前的评论:“我必须使用 userId 更新用户实体中的用户详细信息字段。”
-
感谢您的回复,我已经解决了这个问题。 :)
标签: php doctrine-orm doctrine query-builder