在我的应用程序中,用户可能有也可能没有配置文件。一个例子是:如果用户没有个人资料显示404
Array
(
[User] => Array
(
[id] => 7
[username] => noprofile
[password] => 3b8cdb0c849f00f3634d9b29def1dac4e9235795
[email] => [email protected]
[status] => 0
[code] => 114a10ebb6ffe364805aa70cd44bee7c
)
[Profile] => Array
(
[id] =>
[firstname] =>
[lastname] =>
[gender] =>
[dob] =>
[user_id] =>
)
)
在这里,用户noprofile在配置文件表中没有匹配的内容。
如何在发生这种情况时显示404?我试过了:
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'));
}
但它没有显示404!我正在使用CakePHP 2.0。
这是因为它找到了用户,因此配置文件不是空的!如何检查配置文件为空?
检查$ profile的值,并检查是否确实正在输入if语句并执行cakeError。 – Jrod 2011-12-30 16:02:35
如何检查,因为我没有IDE来检查是否到达 – Cameron 2011-12-30 16:10:54
您不需要IDE。在if语句之前打印/回显$ profile变量,然后在if语句中打印/回显一个字符串,如“嘿,我在IF语句中”。 – Jrod 2011-12-30 16:17:47