简体   繁体   中英

cakephp HABTM save null field

I have a site develop in cakephp 2.0. I have a relation HABTM to the same model like this:

class Product extends AppModel {
    public $name = 'Product';
    public $useTable = 'products';
    public $belongsTo = 'User';
    public $actsAs = array('Containable');
        public $hasAndBelongsToMany = array(
        'Product' => array(
            'className' => 'Product',
            'joinTable' => 'ingredients_products',
            'foreignKey' => 'product_id',
            'associationForeignKey' => 'ingredient_id', 
            'unique' => false
        )
    );

}

I want to save a record into my view with a simple form like this:

echo $this->Form->create('IngredientProduct', array ('class' => 'form', 'type' => 'file'));

        foreach ($product as $prod) {
            echo '<div>'.$prod['ProductAlias']['alias'].'</div>';  
            echo $this->Form->input('IngredientProduct.product_id', array ('type'=>'text', 'value'=>  $prod['ProductAlias']['id'], 'label'=> false, 'id' => 'id'));
        }

        $select = '<select  name="data[IngredientProduct][ingredient_id]" id="[IngredientProductIngredientId">';
        foreach ($other_product as $prod2) {
            $select .= '<option value="'.$prod2['ProductAlias']['id'].'">'.$prod2['ProductAlias']['alias'].'</option>';
        }
        $select .= '</select><br>';
        echo($select);

        echo $this->Form->submit('Collega', array('id'=>'link_product'));
        echo $this->Form->end();

Into my controller I save in this mode:

if ($this->Product->saveAll($this->request->data)){
                $this->Session->write('flash_element','success');
                $this->Session->setFlash ('Prodotto collegato con successo.');
                //$this->redirect(array('action'=>'edit',$alias));
            }
            else{
                $this->Session->write('flash_element','error');
                $this->Session->setFlash('Errore di salvataggio activity');
            }

When I'm going to see into the database I see that ingredient:id is setting well but product_id is 0. I have debugged my request->data and this is the array:

array(
    'IngredientProduct' => array(
        'product_id' => '1',
        'ingredient_id' => '2'
    )
)

I have print the sql query created by cakephp:

INSERT INTO `db`.`ingredients_products` (`product_id`, `ingredient_id`, `modified`, `created`) VALUES ('', 2, '2012-10-09 23:19:22', '2012-10-09 23:19:22')

Why product_id is null instead of 1? Can someone help me? Thanks

I think this line is wrong:

$this->Product->saveAll($this->request->data);

Try:

$this->IngredientProduct->saveAll($this->request->data);

as your form seems to ask data for a relationship, not a new product.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM