У меня есть функция, которую я использую для получения идентификатора пользователя компонента Auth. Он отлично работает без использования return json_encode
. Проблема в том, что мне нужно, чтобы это работало с json_encode, потому что я получаю значения из запроса ajax. Используя json_encode
, он всегда возвращает null
в id, и я не понимаю, почему это происходит. Проблема заключается в функции indexAjax()
ниже.
Как я могу использовать $this->Auth->user("id")
с json_encode
и он не возвращает null?
Пытается.
//using $this->set it works fine
public function index() {
$id = $this->Auth->user("id");
$empresas = $this->Empresa->find('all', array(
'fields'=>array("id", "nomeFantasia", "cnpj",
"telefone1", "telefone2", "celular", "aberto"),
'conditions'=>array("users_id = "=> $id)
));
debug($id) or die;
//$this->set(compact('empresas'));
}
//with json_encode always return null
public function indexAjax() {
$this->autoRender = false;
$id = $this->Auth->user("id");
$empresas = $this->Empresa->find('all', array(
'fields'=>array("id", "nomeFantasia", "cnpj",
"telefone1", "telefone2", "celular", "aberto"),
'conditions'=>array("users_id = "=> $id)
));
return json_encode($id);
}