PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] How to create multiple objects in add method of controller with CakePHP

 April 22, 2022     cakephp, cakephp-2.3, php     No comments   

Issue

I have a ProductsController with an add view that as an autocomplete field ('brand_name') on it where the user can enter a brand name. In my handler I want to check if the brand name already exists in the database, the brand_id should be set on the product. If it does not exists in the database, a new Brand should be created and inserted in the database.

The code beneath is simplified:

app/Model/Product.php

class Product extends AppModel {
    public $belongsTo = array('Brand');
}

app/Controller/ProductsController.php

class ProductsController extends AppController {
    public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session');

    public function add() {
        if ($this->request->is('post')) {
            $this->Product->create();

            $brand = $this->Product->Brand->find(
                'first', 
                array(
                    'conditions' => array(
                        'Brand.name' => $this->request->data['Product']['brand_name']
                    )
                )
            );
            if($brand)
            {
                $this->request->data['Product']['brand_id'] = intval($brand['Brand']['id']);
            }
            else
            {
                // Add new brand
                //$this->Product->Brand->create();
                $this->Product->Brand->name = $this->request->data['Product']['brand_name'];
            }

            if ($this->Product->saveAll($this->request->data)) {
                $this->Session->setFlash(__('The product has been saved.'));
                //$this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Unable to add the product.'));
            }
        }
    }
}

Everything works fine, except for creating and inserting the new Brand object. How do I create this object in code? I'm brand new to CakePHP so if my approach is wrong, please let me know.


Solution

I've fixed this by adding a new value to the $this->request->data array, like this:

if($brand)
{
    $this->request->data['Product']['brand_id'] = intval($brand['Brand']['id']);
}
else
{
    $this->request->data['Brand']['name'] = $this->request->data['Product']['brand_name'];
}


Answered By - Mark
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing