Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp hasmany form data

Tags:

cakephp

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();
    }
}   
like image 238
charliefarley321 Avatar asked Mar 04 '26 18:03

charliefarley321


1 Answers

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

}
like image 145
Leo Avatar answered Mar 07 '26 11:03

Leo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!