条令findBy()从实体调用方法


Doctrine findBy() calling method from entity

从实体调用方法几乎不需要帮助。

这是我试图执行的代码。

$datat = $this->getDoctrine()  
->getRepository('AppBundle:users')  
->findBy(array('userId' => $userId));

之后,当我打电话给时

$data->getUser();

我收到关于exeption的消息"错误:在非对象上调用成员函数getUser()"

当我转储$data时,我从表中获得数据,或者如果我执行
具有ID值的->find()

findBy通常返回一个ArrayCollection。

您应该使用findOneBy,以便只针对一个实体。。。

因此:

$datat = $this->getDoctrine()  
   ->getRepository('AppBundle:users')  
   ->findOneBy(array('userId' => $userId));

您的方法getUser()在users实体中不存在。

只需在$datat上迭代,然后从类似的用户对象调用方法

foreach ($users as $user) {
  // $user is an instance of users
 echo $user->getName(); //if this method exist in your entity model
}