我的模型不包含任何标题.所有有趣的信息都在相关模型中.
我已经阅读过有关虚拟字段的内容,您可以使用它来组合这样的字段:
public $virtualFields = array("full_name"=>"CONCAT(event_id, ' ' ,begin)");
public $displayField = 'full_name';
Run Code Online (Sandbox Code Playgroud)
但这给了我一个简单的id和日期.我需要id作为其他模型的名称.
这真的听起来像是一份工作Set::combine()
.您的显示字段不应该引用不同的模型,因为它并不总是保证可以加入.因此,如果某个地方的调用没有引入数据,则会引发错误.
相反,使用Set::combine()
您可以使用您想要的任何内容创建键值数组.虽然这不那么"神奇",但它会减少错误的可能性.
对于UsersController示例,假设您有hasOne Profile,并希望用户使用自动填充的下拉列表(即使用FormHelper)从其配置文件中显示用户的全名来选择用户.我们将使用Containable来引入配置文件数据.
class AppModel extends Model {
$actsAs = array(
'Containable'
);
}
Run Code Online (Sandbox Code Playgroud)
然后在UsersController中:
function choose($id = null) {
// regular view code here
$users = $this->User->find('all', array(
'contain' => array(
'Profile'
)
));
// create a key-value that the FormHelper recognizes
$users = Set::combine($users , '{n}.User.id', '{n}.Profile.full_name');
}
Run Code Online (Sandbox Code Playgroud)
你会注意到它full_name
现在在Profile模型上,因为它使用了该模型中的字段.combine方法创建一个类似的数组
array(
1 => 'skerit',
2 => 'jeremy harris'
);
Run Code Online (Sandbox Code Playgroud)
当您使用FormHelper创建下拉列表时,将自动使用该选项
echo $this->Form->input('user_id');
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2189 次 |
最近记录: |