以下是我的3个表格,包括用户,网站和项目,请参阅下面的图片了解我想要的输出
241是当前登录用户的ID。
用户表字段
id
email
username
password
网站表格字段
id
domain
company
项目表字段
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,如果管理员将分配其他开发人员和其他设计人员,开发人员和设计人员可能会有所不同
pr($users)
的输出:
App\Model\Entity\User Object
(
[username] => pcsodaily
[password] => $2y$10$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\pcsodaily\20643696_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
)
答案 0 :(得分:2)
designer
表中的developer
和projects
字段似乎是ID,以引用users
表中的记录。那些应该是整数,而不是varchars,如果你还没有进入这个项目,我建议你也改为designer_id
和developer_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
添加到最后,则需要在propertyName
来电中添加belongsTo
选项,更改您可以访问相关实体的属性的名称。例如,'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
作为变量名称会更加标准。