我有以下设置:
模特:用户
型号:客户端
用户hasAndBelongsToMany客户端
客户端hasBelongsToMany用户
数据库表:
用户
客户
Cliens_Users
一切都在起作用,但我想知道是否有更好的方法来获取我的客户端模型的状态字段(与HABTM表相关联)。
以下是我正在做的事情:
ClientsController.php
public function view($id = null) {
$this->Client->id = $id;
if (!$this->Client->exists()) {
throw new NotFoundException(__('Invalid client'));
}
$this->Client->contain('User.id');
$this->Client = $this->Client->read(null, $id);
$this->set('client', $this->Client);
}
我的$ this->客户端上的var_dump如下所示:
array
'Client' =>
array
'id' => string '1' (length=1)
'first_name' => string 'Matt' (length=4)
'last_name' => string 'Moses' (length=4)
'address_1' => string '123 S Happy Lane' (length=16)
'address_2' => string '' (length=0)
'city' => string 'Yourtown' (length=5)
'state' => string 'TX' (length=2)
'zip' => string '12345' (length=5)
'phone_home' => string '8014567899' (length=10)
'phone_work' => string '8014567899' (length=10)
'phone_cell' => string '8014567899' (length=10)
'email' => string 'matt@matt.com' (length=13)
'created' => string '2012-06-13 04:41:19' (length=19)
'modified' => string '2012-06-13 04:41:19' (length=19)
'User' =>
array
0 =>
array
'id' => string '1' (length=1)
'ClientsUser' =>
array
...
客户端/ view.ctp
...
<?php echo $client['User'][0]['ClientsUser']['status']; ?>
...
对我来说,最后一行尖叫“有一种更清洁的方式”,但我似乎无法找到任何关于此事的内容。有什么想法吗?