⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How to add one product to multiple categories?



askbapi
  • profile picture
  • Member

Posted 23 July 2013 - 06:28 AM

I have table called product and category

 

Product

-----------------

id

product_name

cat_id

 

 

Category

------------------

id

Category Name

 

 

How do I add one product  to multiple categories through GROCERY CURD.


davidoster
  • profile picture
  • Member

Posted 23 July 2013 - 07:56 AM

Hello and welcome to the forums [member=askbapi].

Your question how to add one product to multiple categories is partially wrong because your database schema is wrong.

You should actually have 3 tables for what you're trying to do.

For example,

1. the user inserts all the available categories that might exist for a product - table: category

2. the user enters all the products along with any other product data that might exist, except the category - table : product

3. the user maps a product to the category/categories it belongs - table : product_category <- new table

 

The Grocery CRUD library doesn't provide an automatic way to do the 3rd step, the mapping, you need to built it yourself if this is what you need.

It offers options to automate though via callbacks, like callback_before_insert,callback_after_insert etc. Just study the documentation.

I suggest you study about databases before trying to build such a project.


askbapi
  • profile picture
  • Member

Posted 23 July 2013 - 08:41 AM

@ davidoster Thanks for your reply.

Your suggested way is correct way of doing it. I should rethink the ERD


askbapi
  • profile picture
  • Member

Posted 30 July 2013 - 06:09 AM

@davidoster

 

I have now create four Tables name

  1. Product (id, pd_name)
  2. Product_price(id, pro_id, price)
  3. category(id, cat_name)
  4. product_category (pd_id, cat_id)
 public function products() {

       
        try {
            $crud = new grocery_CRUD();

            $crud->set_table('product');
            $crud->set_subject('Product');
            $crud->columns('id', 'pd_name');
            $crud->fields('pd_name',   'price',  'category');

            // Add extr filed
            $crud->callback_add_field('price', array($this, 'add_price'));
            $crud->callback_add_field('category', array($this, '_add_category'));

            $output = $crud->render();

            $this->load->view('catalog/catalog', $output);
        } catch (Exception $e) {
            show_error($e->getMessage() . '--- ' . $e->getTraceAsString());
        }

        return $this;
    }

    
    /**
     * CALL BACK FUNCTION
     */
    public function add_price() {
        return '<input type="text" maxlength="8" value="" name="price"  value="0.00"> (in INR)';
    }

   

    public function _add_category() {
        return '<select multiple name="categoty">' . $this->product_model->category_options() . '</select> ';
    }

What I am trying to do

1> All data form from

 

2> Use call back insert function to insert  data into other two tables

  1. Product_price(id, pro_id, price)
  2. product_category (pd_id, cat_id)

 

3> Form show correct but on SAVE nothing happens, even no error neither any data insertion in table.

 

What wrong am I doing?


davidoster
  • profile picture
  • Member

Posted 30 July 2013 - 11:33 AM

Well, you are $crud->set_table('product');

This means that the only available field for add/edit is pd_name.

So when the Grocery CRUD library bumps into the fields $crud->fields('pd_name', 'price', 'category');

and you press the button for save it tries to find the fields, price and category to the table product.

Because they don't exist it just stays there.

So in order to make this save you need to use this, /topic/486-about-the-fake-filed/#entry1914

Check the callback_insert documentation here.


askbapi
  • profile picture
  • Member

Posted 05 August 2013 - 07:04 AM

Thanks for your value suggestion.