Expression Surveys.id is missing operator (IS, IS NOT) with null value

I am using the following in my controller to echo out the id variable in my template:

$this->loadModel('Surveys');
$id =  $this->Surveys->find('list')->where(['Surveys.id IS' => $id])->first();
$this->set('id', $id);

But I don’t pass the id as a query parameter so it returns null. When I remove the IS operator, I get the error:

Expression Surveys.id is missing operator (IS, IS NOT) with null value.

So how do I get the Surveys id from the database?

I got the user id from the session in the controller and tried to find the surveys id where user id is equal to the session user id but I got an error

$session = $this->request->getSession();
$user_id = $session->read('Auth.User.id');
$id =  $this->Surveys->find('list')->select(['id'])->where(['Surveys.user_id' => $user_id])->first();

The error is:

Expression Surveys.user_id is missing operator (IS, IS NOT) with null value.

I wasn’t logged in so I guess I need to set a default user id when not logged in so that the error doesn’t appear. It seems to be working now.

Now I have the problem that the admin dashboard displays View Survey even when there is no survey. It’s supposed to display Take Survey if no survey has been submitted. So of course when I click on the View Survey link it doesn’t have a survey id so it displays record not found. To check if the survey has been submitted, this is what I have in my controller:


$session = $this->request->getSession();
$user_id = $session->read('Auth.User.id');
  if ($user_id !== null){
  $this->set('survey', $this->Surveys->find()->where(['Surveys.user_id' => $user_id])->first());
  $id =  $this->Surveys->find('list')->select(['id'])->where(['Surveys.user_id' => $user_id])->first();
  $this->set('id', $id);
  }

This is what I have in my template:

if ($survey === null) {
echo  $this->Html->link('Take the Food and Fitness Survey', ['controller' => 'surveys', 'action' => 'survey', $user_id], ['class'=>'']); 
} else {
echo  $this->Html->link('View Your Survey', ['controller' => 'surveys', 'action' => 'view', $id], ['class'=>'']); 
}

a proper way to check, if your variable is available is debug($survey).And then you see, if $survey->id is empty or not.