我正在使用 cakephp 2.1 和 Wamp,一切都很好。我需要从一个模型(项目)中获取一个值(不是 id 字段)并将其分配给正在保存的记录(设置),例如:
SetupsController.php
$this->request->data['Setup']['client_id'] = $this->Setup->$projects['Project']['client_id'];
模型设置.php
var $belongsTo = array('Client', 'User',
'Project' => array(
'className' => 'Project',
'foreignKey' => 'pr_number'
)
);
在我的 SetupsController 中,我得到了项目:
$projects = $this->Setup->Project->find('list', array(
'order' => 'name',
'foreignKey' => 'pr_number',
'fields' => array('Project.pr_number', 'Project.name'),
'conditions' => array('pr_status' => 2)
));
因此,在添加设置时,我从 $projects 获得了 pr_number,一切都很好:
添加.ctp
echo $this->Form->input('pr_number', array('label' => 'ASC Project:', 'options' => $projects));
现在,我需要的是项目记录中的另一个值(client_id),并将其分配给正在保存的设置模型上的列。就像我在保存之前分配当前 user_id 所做的那样:
SetupsController.php(添加函数)
$this->request->data['Setup']['user_id'] = $this->Auth->user('id'); // this works fine
// Next line needs fixing, returns NULL, Don't know how to access client_id from projects model!
$this->request->data['Setup']['client_id'] = $this->Setup->$projects['Project']['client_id'];
if ($this->Setup->save($this->request->data)) { // then save
尝试了 loadModule 和其他建议,但没有运气。
我不认为这很罕见吗?
任何人都可以帮忙吗?
非常感谢。
来自墨西哥蒂华纳的卡洛斯