0
votes

I try to save multiple data with one query with the 'hasMany through' model. Returning the value of $this->request->data shows this:

Array
(
[Order] => Array
    (
        [id] => 
        [customer_id] => 4711
        [orderDate] => Array
            (
                [month] => 01
                [day] => 14
                [year] => 2014
            )
        ... 
    )

[ArticleOrder] => Array
    (
        [article_id] => Array
            (
                [0] => 2002
            )

        [quantity] => Array
            (
                [0] => 99
            )

    )

)

The Array [ArticleOrder] hasn't been saved in the Database. In my Controller I tried saveAll (yes deep => true), saveMany, saveAssociated...

Here is the Controller:

public function add() {
    $this->set('customers', $this->Order->Customer->find('list')); //Auflösung der Schlüssel
    //$this->set('articles', $this->Order->ArticleOrder->Article->find('list'));
    if(!empty($this->request->data)) {
        $this->Order->create();
        pr($this->data);
        if($this->Order->ArticleOrder->saveAll($this->request->data, array('deep' => true))) {          
            $this->Session->setFlash("Die Bestellung wurde gespeichert!");
            $this->redirect(array('action'=>'index'));
        } else {
            $this->Session->setFlash('Die Bestellung konnte nicht gespeichert werden.');
        }           
    }


}

Please help me ... i'm despaired...

Greetings from Germany

2

2 Answers

0
votes

The array is wrong? I think it should look like this:

'ArticleOrder' => array(
    array('article_id' => 2002, 'quantity' => 99),
    array('article_id' => 3151, 'quantity' => 14),
    /* ... */
);

Check your code that generates the form or data structure.

See http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-savemany-array-data-null-array-options-array

0
votes

I solved the problem :-)

Here is the code of the controller:

    public function add() {
    $this->set('customers', $this->Order->Customer->find('list')); //Auflösung der Schlüssel
    //$this->set('articles', $this->Order->ArticleOrder->Article->find('list'));
    if(!empty($this->request->data)) {
        $this->Order->create();
        $data = array(
            'Order' => $this->request->data['Order'],
            'ArticleOrder' => array()
        );  
        for($i = 0; $i< count($this->request->data['ArticleOrder']['article_id']); $i++) {
            $data['ArticleOrder'][] = array(
                'article_id' => $this->request->data['ArticleOrder']['article_id'][$i],
                'quantity' => $this->request->data['ArticleOrder']['quantity'][$i]
            );
        }           
        if($this->Order->saveAssociated($data, array('deep'=>true))) {          
            $this->Session->setFlash("Die Bestellung wurde gespeichert!");
            $this->redirect(array('action'=>'index'));
        } else {
            $this->Session->setFlash('Die Bestellung konnte nicht gespeichert werden.');
        }
    }
}

I just formatted the array in the for-block

Thank you