如何打印cakephp3中3个表连接的字段的准确值

How to print the exact value of the field in cakephp3 with 3 tables joining

下面是我的 3 tables,包括用户、网站和项目,请参见下图了解我想要的输出

241为当前登录用户ID

用户table 字段

id
email
username
password

网站 table 字段

id
domain
company

项目table 领域

id
int user_id   // select option to get from user 
varchar domain    // select option to get from websites table
varchar designer  // select option to get from user 
varchar developer // select option to get from user 
int price

UsersTable.php

public function initialize(array $config)
{
    parent::initialize($config);
    $this->table('users');
    $this->belongsTo('Users');
    $this->hasMany('Subjects');
    $this->hasMany('Websites');
    $this->hasMany('Projects');
    $this->displayField('username');  // called in project add.ctp
    $this->primaryKey('id'); 
}

UsersController.php

public function project($id = null)
{
    $users = $this->Users->get($id, [
        'contain' => [
            'Projects'
        ]
    ]);
    $this->set('users', $users);
    $this->set('_serialize', ['users']);

}

当我点击一个按钮时我的项目

我的项目

<table>
<tr>
    <td>Designer</td>
    <td>Price</td>
    <td>Developer</td>
    <td>Domain</td>
</tr>
<?php 
    if(!empty($users->projects)) {
        foreach($users->projects as $project) {
?>
    <tr>                              
    <td> <?php echo $project->designer;?> </td>
    <td> <?php echo $project->price;?> </td>
    <td> <?php echo $project->developer;?> </td>
    <td> <?php echo $project->domain;?> </td>
    </tr>
<?php
        }
    }
    else{
        echo"You dont have any projects yet";
    }
?>
</table>

241是当前登录用户的ID,开发者和设计者可以不同,如果管理员分配其他开发者和其他设计者

  1. 它将打印数据而不是值和 ID
  2. 我不知道怎么打印公司

pr($users) 的输出:

App\Model\Entity\User Object
(
    [username] => pcsodaily
    [password] => y$u3W..vnZ3ygXH3BhFJ2U0ui7qI2bwRJcM85tVR.SPUlNXsTdM/GNa
    [id] => 241
    [email] => pcsodailyresult@gmail.com
    [profile_pic] => Resource id #256
    [destination] => E:\xampp-for-cakephp3\htdocs\sibonga\webroot\img\users\pcsodaily643696_1656261664406406_1426837475_n.jpg
    [created] => 2018-04-06
    [age] => 30
    [address] => Bato, Sibonga Cebu
    [gender] => Male
    [firstname] => pcso
    [lastname] => calderon
    [activation_key] => f6aff532-2632-4440-a144-6006ae1aa374
    [status] => 1
    [pass_key] => 
    [timeout] => 
    [websites] => Array
        (
        )

    [projects] => Array
        (
            [0] => App\Model\Entity\Project Object
                (
                    [id] => 9
                    [finished_time] => 2018-04-30
                    [designer] => 241
                    [developer] => 241
                    [price] => 200
                    [status] => live
                    [user_id] => 241
                    [domain] => 9
                    [[new]] => 
                    [[accessible]] => Array
                        (
                            [*] => 1
                            [id] => 
                        )

                    [[dirty]] => Array
                        (
                        )

                    [[original]] => Array
                        (
                        )

                    [[virtual]] => Array
                        (
                        )

                    [[errors]] => Array
                        (
                        )

                    [[invalid]] => Array
                        (
                        )

                    [[repository]] => Projects
                )

        )

    [[new]] => 
    [[accessible]] => Array
        (
            [*] => 1
            [id] => 
        )

    [[dirty]] => Array
        (
        )

    [[original]] => Array
        (
        )

    [[virtual]] => Array
        (
        )

    [[errors]] => Array
        (
        )

    [[invalid]] => Array
        (
        )

    [[repository]] => Users
)

您的 projects table 中的 designerdeveloper 字段似乎旨在用作引用 users [=52= 中的记录的 ID ].这些绝对应该是整数,而不是 varchars,如果您还没有深入了解这个项目,我建议您也将它们更改为 designer_iddeveloper_id。如果你能做到这一点,那么你的 ProjectsTable 应该有一个像这样的初始化函数:

public function initialize(array $config)
{
    parent::initialize($config);
    $this->table('projects');
    $this->belongsTo('Designers', [
        'foreign_key' => 'designer_id',
        'className' => 'Users',
    ]);
    $this->belongsTo('Developers', [
        'foreign_key' => 'developer_id',
        'className' => 'Users',
    ]);
    $this->primaryKey('id'); 
}

完成后,您的 project 函数将变得更像这样:

$users = $this->Users->get($id, [
    'contain' => [
        'Projects' => [
            'Designers',
            'Developers',
        ]
    ]
]);

然后你的模板就可以使用

<?php echo $project->designer->username; ?>
<?php echo $project->developer->username; ?>

如果由于某种原因您无法更改字段名称以将 _id 添加到末尾,您将需要在 belongsTo 调用中添加 propertyName 选项,以更改您将能够访问相关实体的 属性 的名称。例如,'propertyName' => 'designer_record',然后您的模板需要说

<?php echo $project->designer_record->username; ?>

相反。

您还可以添加

$this->hasMany('DesignedProjects', [
    'foreign_key' => 'designer_id',
    'className' => 'Projects',
]);

到您的 UsersTable::initialize 功能,以及与 DevelopedProjects 类似的功能,如果它对您获取用户设计或开发的项目列表有用的话。然后,您可以在阅读用户时根据需要在您的容器中包含 DesignedProjects,并使用 $user->designed_projects.

引用它们

旁注:

很确定用户不属于用户,这与那里的初始化函数相反。

在获取单个实体时,使用 $user 作为变量名比 $users 更标准。