2016-07-14 3 views
0

CakePHP 3.2에서 작업하고 있습니다. 테이블 user_addresses이있는 내가CakePHP 3 : 데이터베이스에서 데이터를 가져 오지 않은 경우에도 true를 반환합니다.

public function myFun() 
{ 
    $this->loadModel('UserAddresses'); 
    $user_id = $this->Auth->user('id'); 
    $userAddresses = $this->UserAddresses->find('all', [ 
     'conditions' => [ 
      'user_id' => $user_id 
     ] 
    ]); 
    if (empty($userAddresses)) { 
     echo 'Hello';    // for testing only 
    } else { 
     echo 'World'; 
    } 
} 

그것을 확인하려면 회사 사용자의 모든 레코드를 가져가 시도하고있다, 나는 컨트롤러의 beforeFilter

$this->Auth->allow(['myFun']); 

myFun을 추가하고이 세계 대신을 인쇄 안녕 사용자가 로그인하지 않은 경우 $user_id이 비어 있어야하므로 데이터베이스에서 데이터를 검색하지 않으므로

+0

좀 더 편하게 디버깅해야합니다. 그래서 여러분은 기대했던 것 같지 않은 값에 문제가 있습니다. 우선,해야 할 일은 그 값을 디버깅하여 실제적으로 정확히 무엇인지 알아내는 것입니다 ('debug ($ userAddresses)')! 문제가있는 곳의 케이크를 아는 사람들에게는 명백 할 수 있지만 여전히 그러한 세부 사항은 귀하의 질문에 있어야합니다. 종종 문제는 그러한 정보를 수집 할 때 심지어 스스로 해결합니다. – ndm

답변

1

먼저 $user_id을 사용할 수 있는지 확인한 다음 find() 메서드를 호출하십시오.

public function myFun() 
{ 
    $this->loadModel('UserAddresses'); 
    $user_id = $this->Auth->user('id'); 
    if(empty($user_id)){ 
     echo "Not able to access this method"; 
     die(); 
    } 
    $userAddresses = $this->UserAddresses->find('all', [ 
     'conditions' => [ 
      'user_id' => $user_id 
     ] 
    ]); 
    if (empty($userAddresses)) { 
     echo 'Hello';    // for testing only 
    } else { 
     echo 'World'; 
    } 
} 
관련 문제