我一直在挖掘类似的问题,但无法通过我的应用程序来节省HABMT关系。
我想要"测试" HABMT"浏览器"。
表格: cake_browsers(id,name,...)
cake_tests(id,name,...)
cake_browsers_tests(browser_id,test_id)
可以读取中间表中已有的关系。
$ this-> Test-> findById($ id)的结果是:
Array(
[Test] => Array
(
[id] => 94
[name] => NM Test 2
[description] =>
[created] => 2015-03-22 18:54:41
[modified] => 2015-03-22 19:53:28
)
[Browser] => Array
(
[0] => Array
(
[id] => 2
[name] => Firefox
[height] => 768
[width] => 1366
[active] => 1
)
)
)
模特:
class Test extends AppModel {
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
'Browser' => array(
'className' => 'Browser',
)
); ...
控制器功能:
public function add() {
$this->set('testDomains', $this->Test->DomainSchedule->TestDomain->find('all'));
$this->set('browsers', $this->Test->Browser->find('list'));
if ($this->request->is('post')) {
$this->Test->create();
$newTest = $this->request->data;
print "<pre>"; print_r($newTest); print "</pre>";
if ($this->Test->saveAll($newTest)) {
if($this->scheduleAutoTestRun($this->Test->id)){
$this->Session->setFlash(__('Your test has been saved. Additionally it has been added to the queue.'));
}else{
$this->Session->setFlash(__('Your test has been saved.'));
}
//return $this->redirect(array('action' => 'index'));
}else{
$this->Session->setFlash(__('Unable to add your test.'));
}
}
}
观点:
...
<?php echo $this->Form->input('Browser',array('multiple'=>'checkbox'));?>
...
$ this-&gt; request-&gt; data
的内容Array(
[Test] => Array
(
[name] => NM Test
[description] =>
)
[Browser] => Array
(
[Browser] => Array
(
[0] => 2
)
)
所以,就我一直在阅读而言,我明白这就是它的样子。不幸的是,它没有保存与id = 2的Browser连接到新创建的Test的关系。
有人能给我一个提示我做错了吗?
谢谢!
答案 0 :(得分:0)
我认为这需要深层次,如$ this-&gt; Test-&gt; saveAll($ data,array(&#39; deep&#39; =&gt; true))