2
votes

I have a question to ask. I am using cakephp 2.0, i have table products, materials and materials_products.

in table products:

  • id
  • name
  • description
  • sale
  • saleprice
  • frame_material_id

in table materials

  • id
  • name

in table materials_products

  • id
  • product_id
  • material_id

i just want to ask how to retrieve the input data for material_id in productController?(from view). materials and products have a HABTM relationship.

i mean i know if i want to retrieve input data from name in products controller i can use

    $this->request->data['Product']['name']

but if i want to retrieve input material_id from products should i use:

    $this->request->data['Product']['Material']

or

    $this->request->data['Material']['id']

or

    $this->request->data['Material']

or something else? i tried to use all 3 but i keep getting error. different type of error.

this is the code that i have tried so far (productController):

    public function addproductnewcont($colorcounts = null, $groupid = null, $id = null) {
    $start = $id - $colorcounts + 1;
    $setids = range($start, $id);

    if (!$this->Product->exists($id)) {
        throw new NotFoundException(__('Invalid product'));

    }
    if ($this->request->is('post') || $this->request->is('put')) {
        $saleprice = $this->request->data['Product']['saleprice'];
        $sale = $this->request->data['Product']['sale'];
        $framematid = $this->request->data['Product']['frame_material_id'];
        $materialids = $this->request->data['Material']; //this is the problem
        $fieldtoupdate=array(
                'saleprice'=>"'$saleprice'",
                'frame_material_id'=>"'$framematid'",
                'sale'=>"'$sale'");

        $this->loadModel('MaterialsProduct', 2);
        foreach($setids as $setid) {
        if ($this->Product->updateAll($fieldtoupdate,array('Product.id'=> $setid))) {
            $this->_savematerial($materialids, $setid);
            if ($setid == $id) {
            $this->Session->setFlash(__('The product has been saved'));
            return $this->redirect(array('action' => 'index'));
            }
        } else {
            $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
        } }
    }


        public function _savematerial($materialids = null, $setid = null){
        foreach($materialids as $materialid){
                $this->MaterialsProduct->create();
                $this->MaterialsProduct->saveField('product_id', $setid);
                $this->MaterialsProduct->saveField('material_id', $materialid); 
            }

this give me error saying

    Array to string conversion

can you help me? thanks!

1

1 Answers

0
votes

I have found the answer to my question. hope this will help anyone who seek the same answer as me.

my code is alright except for i need to add one more foreach in the code. (i don't know the actual problem).

        public function _savematerial($materialids = null, $setid = null){
        foreach($materialids as $materialid){
            foreach($materialid as $materiali){
                $this->MaterialsProduct->create();
                $this->MaterialsProduct->saveField('product_id', $setid);
                $this->MaterialsProduct->saveField('material_id', $materiali); 
            }
           }
  }

thank you for everyone.