在 CakePHP 2.5.1 中显示登录数据

Displaying logged-in data in CakePHP 2.5.1

我有 tables:

而且,模型是:

 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>

AppController.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'))

        )
    );

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

我收到以下错误:

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

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

函数

$this->Auth->User() 

不是 return 模型数组。

相反,它 return 类似于:

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>

注意只有一行。