3
votes

I want to save multiple categories for a product.

I have the models:

Category.php
public $hasAndBelongsToMany = array(
        'Product' => array(
            'className' => 'Product',
            'joinTable' => 'product_categories',
            'foreignKey' => 'category_id',
            'associationForeignKey' => 'product_id',
            'unique' => 'keepExisting',
        )
    );

Product.php
public $hasMany = array(
        'ProductCategory' => array(
            'className' => 'ProductCategory',
            'foreignKey' => 'product_id',
            'dependent' => false,

        ),

ProductCategory.php
public $belongsTo = array(
        'Product' => array(
            'className' => 'Product',
            'foreignKey' => 'product_id',

        ),
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'category_id',
        )
    );

So in Product/add view I add a set of checkboxes of the Categories by:

echo $this->Form->input('ProductCategory.category_id',array(
        'label' => __('Category',true),
        'type' => 'select',
        'multiple' => 'checkbox',
        'options' => $categories
    )); 

But this produces a set of inputs with the names of: name="data[ProductCategory][category_id][]" rather than name="data[ProductCategory][0][category_id]" the zero being incremented.

If they are in the format with the key between the model and field then I can use saveAll()? As it is in the format I am getting I would have to manipulate the request->data to get it into the form I want to be able to save()

Am I going about this the correct way? Or maybe my models are set up incorrectly?

Also, what happens with editing hasMany data? What if for instance I uncheck an option and add another? Does cake automatically delete all associated records before adding new ones?

EDIT.

Essentially what I am asking is is there a better or quicker way of doing this, which works right now:

if ($this->Product->save($this->request->data)) {
    $this->Product->ProductCategory->deleteAll(array('ProductCategory.product_id' => $this->Product->id));
    foreach ($this->request->data['ProductCategory']['category_id'] as $cat_id) {
        $this->Product->ProductCategory->create();
        $this->Product->ProductCategory->set(array(
            'product_id' => $this->Product->id,
            'category_id' => $cat_id
        ));
        $this->Product->ProductCategory->save();
    }
}   
2

2 Answers

1
votes

in your form iterate however many you wish to display along the lines of

for/while/do()
{       
    $counter++
    $this->Form->text('Product.'.$counter.'.price');
    $this->Form->text('Product.'.$counter.'.description');

}
0
votes

I would use saveAll() for this as it's designed to save a record and all it's related records for you automatically, assuming that your id's are in the data and it's formatted correctly.

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