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

Friday, April 22, 2022

[FIXED] how loadModel and modified field in another controller Cakephp 2

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

Issue

I have two tables, cars and locations; I want loadModel Car in LocationsController and I want, when I add location, for it to automatically status column in Cars table registers unavailable into locationsController:

    public function add($car_id) {
        if ($this->request->is('post')) {
            $this->Location->create();
            if ($this->Location->save($this->request->data)) {
                $this->Session->setFlash(__('The location has been saved.'));
                return $this->redirect(array('action' => 'index'));
                App::import('model','Car');
            $this->Car->write('Car.status', 'unavailable');
            } else {
                $this->Session->setFlash(__('The location could not be saved. Please, try again.'));
            }
        }
         $users = $this->Location->User->find('list');
        $agencies = $this->Location->Agency->find('list');
        /*$cars = $this->Location->Car->find('list');*/
        $this->set(compact('agencies'));
        $this->set('car_id', $car_id);



    }

Solution

Remove this lines

App::import('model','Car');
$this->Car->write('Car.status', 'unavailable');

Following code you have to write

 if ($this->Location->save($this->request->data)) {
                $this->Session->setFlash(__('The location has been saved.'));



             $this->loadModel("Car");
             $this->Car->id=$car_id;
             $this->Car->saveField("status","unavailable");

            return $this->redirect(array('action' => 'index'));

            } else {
                $this->Session->setFlash(__('The location could not be saved. Please, try again.'));
            }

Check Manual here.



Answered By - Sadikhasan
Answer Checked By - Terry (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