在 CakePHP 2.5.1 中显示登录数据


Displaying logged-in data in CakePHP 2.5.1

我有表格:

  • users(型号User) - ID、person_id、用户名、密码
  • people(模型 Person) - id、parent_id(同一表的外键)、last_name first_name
  • addressesAddress ) - ID, person_id, 街道, 城市

并且,模型是:

 class User extends AppModel {
       public $actsAs = array('Containable');
       public $belongsTo = array(
            'Person'
        );
  }  
class Person extends AppModel {
    public $belongsTo = array(
        'Parent' => array(
            'className' => 'Person',
            'foreignKey' => 'parent_id',
        ),
    );
    public $hasMany = array(
        'User',
        'Address',
        'Subordinate' => array(
            'className' => 'Person',
            'foreignKey' => 'parent_id',
        ),
    );
}
class Address extends AppModel{
    public $belongsTo = array(
        'Person'
    );
}

View/Users/文件夹中的视图:

register.ctp
login.ctp
logout.ctp
profile.ctp

注册并登录后,我需要显示个人资料数据。为此,我在UsersController使用以下函数:-

public function profile() { 
       $this->set('userProfile', $this->Auth->User());
}

我有以下profile.ctp

<table>
    <tr>
       <th>Username </th>
       <th>First Name </th>
       <th>Last Name </th>
       <th>street</th>
       <th>house_no</th>
 </tr>
   <?php foreach ($userProfile as $profiledata){ ?> 
    <tr>
        <td><?php echo $profiledata['username']; ?> </td>
        <td><?php echo $profiledata['Person']['first_name']; ?> </td>
        <td><?php echo $profiledata['Person']['last_name']; ?> </td>
        <td><?php echo $profiledata['Person']['Address']['street']; ?> </td>
        <td><?php echo $profiledata['Person']['Address']['house_no']; ?> </td>
    </tr>
    <?php } ?>
</table>

应用控制器中的身份验证组件.php

 public $components = array(
    'DebugKit.Toolbar',
    'Session',
    'Flash',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
        'authenticate' => array('Form' => array(
                        'contain'   => array('Person'=>'Address'),
                        'passwordHasher' => 'Blowfish'))
        )
    );

它可以从用户和人员表中检索数据,但不能从地址表中检索数据。

我收到以下错误:

Notice (8): Undefined index: street [APP'View'Users'profile.ctp, line 32]

谁能告诉我这里出了什么问题?我是否需要为配置文件 ( Profile.php) 创建另一个模型来显示配置文件数据?

函数

$this->Auth->User() 

不返回模型数组。

相反,它返回类似于以下内容的内容:

Array
(
    [id] => 1
    [user_name] => john.doe
    [Person] => Array
        (
            [id] => 1
            [first_name] => John
            [last_name] => Doe
        )
    [Address] => Array
        (
            [id] => 1
            [street] => Piccadilly Street
            [house_no] => 4
        )
)

因此,您必须删除foreach循环,并通过执行以下操作来访问数据:

<tr>
    <td><?php echo $userProfile['user_name']; ?> </td>
    <td><?php echo $userProfile['Person']['first_name']; ?> </td>
    <td><?php echo $userProfile['Person']['last_name']; ?> </td>
    <td><?php echo $userProfile['Address']['street']; ?> </td>
    <td><?php echo $userProfile['Address']['house_no']; ?> </td>
</tr>

请注意,只有一行。