在CakePHP 2.5.1中显示登录的数据

在CakePHP 2.5.1中显示登录的数据,php,authentication,cakephp,cakephp-2.x,Php,Authentication,Cakephp,Cakephp 2.x,我有以下表格: 用户(型号用户)-id、个人id、用户名、密码 人员(型号人员)-id、父项id(同一表的外键)、姓、名 地址(地址)-身份证、身份证、街道、城市 模型包括: class User extends AppModel { public $actsAs = array('Containable'); public $belongsTo = array( 'Person' ); } class Pers

我有以下表格:

  • 用户
    (型号
    用户
    )-id、个人id、用户名、密码
  • 人员
    (型号
    人员
    )-id、父项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
注册和登录后,我需要显示配置文件数据。为此,我在
userscoontroller
中使用以下函数:-

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>
它可以从用户和人员表中检索数据,但不能从地址表中检索数据

我遇到以下错误:

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>


请注意,只有一行。

当您遇到类似“无法将字符串偏移量用作数组”的错误时,解决该错误的最快方法是调试变量并检查它包含的内容。使用
debug($userProfile)
print\r($userProfile)
会发现变量的结构与您的
foreach
循环不兼容。再次感谢好友!:)现在,它显示了Person的登录数据,但显示了另一个错误:未定义的index:User和未定义的index:address对不起。我修正了一个打字错误。您不需要包含键
“User”
。关于
地址
,我相信它与
有关,而不是与
用户
有关,所以它不会被
AuthComponent
获取。要使其工作,必须在
AuthComponent
中使用contain。你可以在中找到一个例子。记住要包括
数组('Person'=>'Address')
。在您看来,您必须使用
echo$userProfile['Person']['Address']['house\u no']
。但它仍然显示出同样的问题(它从用户和人员表中检索数据,但不能从地址表中显示街道、房屋号等数据。显示错误:未定义的索引:street[APP\View\users\profile.ctp在使用echo$userProfile['Person'][0]['address']['street']后;显示错误:注意(8):未定义的偏移量:0[APP\View\users\profile.ctp,
<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>