0

I have 1 Category table and second Features table and have association among them is HABTM in cakephp

Now i don't know exactly how to insert values in categories_features table. I used HABTM before but at that time i insert values in this table when one item is created by using "saveAll()" method. but in this case i have seprate page when categories in drop down menu and features in checkbox form.

i tried to create new model categoriesFeature to solve this problem, but when i submit data , data in in this form

Array
(
    [CategoriesFeature] => Array
        (
            [Category_id] => 2
            [feature_id] => Array
                (
                    [0] => 1
                    [1] => 2
                )

        )

)

so i get error

"Array to string conversion [CORE\Cake\Model\Datasource\DboSource.php, line 1009]"  
Column not found: 1054 Unknown column 'Array' in 'field list'
"INSERT INTO `ecomerce`.`categories_features` (`feature_id`) VALUES (Array)"

if i'm using wrong approach kindly guide me.

View code to add data in categories_features table

 <?php echo $this->Form->create('CategoriesFeature', array('id' => 'cf'));?>
    <fieldset>
    <legend>Feature Add Form</legend>
    <?php
        echo $this->Form->input('Category_id',array(
                'data-rel' => 'chosen',
                'placeholder' => 'Select Category'
        ));

        $keys= array();
        $values =array();


 echo "<h2> Select Features for above selected Category </h2>";
echo $this->Form->input('feature_id',
        array('label'=>'',
        'type'=>'select',
        'multiple'=>'checkbox',
        'options'=>$features)
        );


        echo $this->Form->submit();
    ?>   
    </fieldset>

table structure

id | category_id | feature_id

coder
  • 156
  • 3
  • 23
  • i'm using this way to insert data if($this->CategoriesFeature->saveAll($this->request->data)){ echo "all features saved"; } – coder Mar 10 '15 at 12:58

1 Answers1

0

If you are using the saveAll method, you will need to adjust your array:

Array
(
    [CategoriesFeature] => Array
        0 => array(
            [Category_id] => 2
            [feature_id] => 1

        ),
        1 => array(
            [Category_id] => 2
            [feature_id] => 2

        )
)

You should be using saveMany: as per Model::saveMany(array $data = null, array $options = array())

$data = array(
    array('Article' => array('title' => 'title 1')),
    array('Article' => array('title' => 'title 2')),
);
Roovdwalt
  • 28
  • 4
  • another solution ? because if i will do this it mean i have to create category a lot of times equeal to features or need to use some hidden field and js to set values in those. i'm trying to do this in cakish way – coder Mar 10 '15 at 13:26
  • This is the cake way. You will need to format your array with a foreach loop on feature_id, or some javascript to adjust your array before being posted. – Roovdwalt Mar 10 '15 at 13:53
  • i followed you and my work has done, even i'm not happy with this way, but thanks for help – coder Mar 10 '15 at 14:47