On my app a user may or may not have a profile. An example would be:
Array
(
[User] => Array
(
[id] => 7
[username] => noprofile
[password] => 3b8cdb0c849f00f3634d9b29def1dac4e9235795
[email] => noprofile@noprofile.com
[status] => 0
[code] => 114a10ebb6ffe364805aa70cd44bee7c
)
[Profile] => Array
(
[id] =>
[firstname] =>
[lastname] =>
[gender] =>
[dob] =>
[user_id] =>
)
)
Here the user noprofile has no matching content in the profiles table.
How do I show a 404 for when this happens? I’ve tried:
public function view ( $username )
{
$this->Profile->User->recursive = 2;
$profile = $this->Profile->User->find('first', array(
'conditions' => array('User.username' => $username)
));
if (empty($profile))
{
echo 'echo1';
throw new NotFoundException('No profile');
}
else {
echo 'echo2';
}
$this->set(compact('profile'));
}
But it doesn’t show the 404! I’m using CakePHP 2.0.
It’s because it finds the user so therefore profile is not empty! How do I check for the profile being empty then?
I expect there are better ways to do it but that’s a quick fix.
$profile, as you said, will not be empty as theuseris found.