我是cakephp的新手。我有拖表经纪人信息和经纪人银行。我能够从BrokerInfosController发送数据添加方法。但编辑中存在问题。
我已将此代码用于编辑
public function edit($id = null) {
$this->BrokerInfo->id = $id;
if (!$this->BrokerInfo->exists()) {
throw new NotFoundException(__('Invalid broker info'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->BrokerInfo->save($this->request->data)) {
$this->Session->setFlash(__('The broker info has been saved'), 'flash_success');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The broker info could not be saved. Please, try again.'), 'flash_fail');
}
} else {
$this->request->data = $this->BrokerInfo->read(null, $id);
}
}
这里BrokerInfo属于与BrokerBank的关系。应用此代码后我得到了这个输出
这里我如何发送BrokerBank的请求?
我试过这个代码
$this->request->data = $this->BrokerInfo->BrokerBank->find('all', $options);
但它没有用。
答案 0 :(得分:0)
永远不要尝试,但我认为你可以看看这个功能: http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array
希望它可以提供帮助
答案 1 :(得分:0)
我通过这种方式解决了这个问题,这是代码
public function edit($id = null) {
$this->BrokerInfo->id = $id;
$this->BrokerBank->id = $id;
if (!$this->BrokerInfo->exists()) {
throw new NotFoundException(__('Invalid broker info'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->BrokerInfo->save($this->request->data)) {
$brokerBank=$this->BrokerBank->find('first', array('conditions'=>array('broker_info_id'=>$id) ,'recursive' => -1));
$this->request->data['BrokerBank']['id'] = $brokerBank['BrokerBank']['id'];
$this->BrokerBank->save($this->request->data);
$this->Session->setFlash(__('The broker info has been saved'), 'flash_success');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The broker info could not be saved. Please, try again.'), 'flash_fail');
}
} else {
$brokerBank=$this->BrokerBank->find('first', array('conditions'=>array('broker_info_id'=>$id) ,'recursive' => -1));
$brokerInfo=$this->BrokerInfo->find('first', array('conditions'=>array('id'=>$id) ,'recursive' => -1));
$this->request->data['BrokerInfo'] = $brokerInfo['BrokerInfo'];
$this->request->data['BrokerBank'] = $brokerBank['BrokerBank'];
}
$tradingTypes = $this->BrokerInfo->TradingType->find('list');
$accountStatuses = $this->BrokerInfo->AccountStatus->find('list');
$teams = $this->BrokerInfo->Team->find('list');
$this->set(compact('tradingTypes', 'accountStatuses', 'teams'));
}
在这里,我认为这段代码很清楚我做了什么。第一个问题是我需要模型id
。这里我有BrokerInfo模型id
,所以我需要通过查找方法得到的BrokerBank id
$brokerBank=$this->BrokerBank->find('first', array('conditions'=>array('broker_info_id'=>$id) ,'recursive' => -1));
我已经通过此行id
$this->request->data['BrokerBank']['id'] = $brokerBank['BrokerBank']['id'];
所以现在我可以写这一行$this->BrokerBank->id = $id;
现在我将如何阅读字段
中的两个数据这里我使用了这四行
$brokerBank=$this->BrokerBank->find('first', array('conditions'=>array('broker_info_id'=>$id) ,'recursive' => -1));
$brokerInfo=$this->BrokerInfo->find('first', array('conditions'=>array('id'=>$id) ,'recursive' => -1));
$this->request->data['BrokerInfo'] = $brokerInfo['BrokerInfo'];
$this->request->data['BrokerBank'] = $brokerBank['BrokerBank'];
这里的前两行根据broker_info_id
和BrokerInfo id
查找BrokerBank数据。然后按字段定义$ this-> request-> data。
我想你已经明白了。可能会说英语不好但是我尽力解释。