0
votes

I've been digging through similar questions but couldn't get my app to save HABMT relationsships.

I want to have "Test" HABMT "Browser".

The Tables: cake_browsers (id, name, ...)

cake_tests (id, name, ...)

cake_browsers_tests(browser_id, test_id)

The Relationsships that are already in the middle-table can be read.

The result of $this->Test->findById($id) is:

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
            )

    )

)

The Model:

class Test extends AppModel {
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
    'Browser' => array(
            'className' => 'Browser',
    )
); ...

The controller-function:

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.'));
        }
    }
}

The View:

...
<?php echo $this->Form->input('Browser',array('multiple'=>'checkbox'));?>
...

The Content of $this->request->data

Array(
[Test] => Array
    (
        [name] => NM Test
        [description] => 
    )


[Browser] => Array
    (
        [Browser] => Array
            (
                [0] => 2
            )

    )

So, as far as I've been reading, I understand that this is the way it should look like. Unfortunately it is not saving the relationship that Browser with id=2 is connected to the new created Test.

Could anybody give me a hint what I am doing wrong?

Thanks!

1
Try to see in you /model/Entity/Test.php and see if you have 'browsers' => true,wenbert
I think you talk about the folder Entity which is only available since the new 3.x stable version? I am working with 2.5.6.Doena

1 Answers

0
votes

I believe this needs the param deep as in $this->Test->saveAll( $data, array('deep' => true) )