我想通过自定义组件中的ID获取用户对象。我试着这样做:
$query = $this->Users->find('all', [
'conditions' => [
'Users.id' => $uid,
'Users.state' => 'ACTIVE'
]
]);
$results = $query->first();
但我收到以下错误,因为Users模型为null
Call to a member function find() on null
是否可以从自定义组件中的表(模型)中获取这些数据以及如何获取?
非常感谢您的任何建议。
答案 0 :(得分:1)
这个怎么样:
<?php
namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Network\Exception\InternalErrorException;
use Cake\Utility\Text;
use Cake\ORM\TableRegistry; // <----
class YourComponent extends Component
{
public function getUser($id = null)
{
$this -> Users = TableRegistry::get('Users'); // <----
$user = $this -> Users -> find()
-> where(['state' => 'ACTIVE', 'id' => $id])
-> first();
return $user;
}
}