以下是我的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] => [email protected]
[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
)
您的表中的designer
和developer
字段projects
似乎是作为ID来引用users
表中的记录的。这些绝对应该是整数,而不是varchars,如果您还不很了解这个项目,我建议您也将它们更改为designer_id
and 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
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句