Tom
Tom

Reputation: 51

why some foreign keys are not saved in CakePHP

I am developing a web application in CakePHP 2.5.1. i am facing problems in saving foreign keys in database and showing associated data in web pages. i have users, managers, customers, houses, addresses and emails tables.

their relationships are ,

my requirements are,

Now, i can save all the data. but in the database, foreign keys are not saved, thus creating problem in showing associated data. for example, when manager's registration is done, it saves manager_id in the addresses table; but customer_id and house_id remain NULL. when a customer is added for the manager, it saves customer_id in addresses table; but manager_id and house_id remain NULL. But, it should save manager_id, since a customer belongs to a manager. similarly, when a house is added for a customer, it saves house_id in addresses table; but manager_id and customer_id remain NULL. But, it should save manager_id and customer_id, since a house belongs to a customer, and that customer belongs to a manager.

in AppController, i defined the relations like following:

'Auth' => array(
        'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
        'authenticate' => array('Form' => array(

       'contain'   => array(
           'Manager'  => array(
                'Address' , 'Email' , 'Customer' => array('House')))

in the userscontrollers, i used following methods:

 public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->saveAll($this->request->data, array('deep' => true))) {
                $components = array('Session'); 
                $this->Session->setFlash('Welcome!');
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __(' Please, try again.')
            );
        }
    }

in the CustomersControllers, i used following method:

public function AddCustomer() {

   if ($this->request->is('post')) {
            $this->Customer->create();
            if ($this->Customer->saveAll($this->request->data, array('deep' => true))) {
                $primaryKey =   $this->Customer->id; 
                $components = array('Session'); 
                $this->Session->setFlash('Customer DATA have been saved!');
                return $this->redirect(array('action' =>  ‘index'));
            }
            $this->Session->setFlash(
                __('The Customer Data could not be saved. Please, try again.')
            );
        }
     }

in the HousesControllers, i used following method:

  public function AddHouse() {

         if ($this->request->is('post')) {
            $this->House->create();

            $this->loadModel('House');

            $houseData = $this->data['House'];
            $houseData[‘house_id'] = $primaryKey;

            if ($this->House->saveAll($houseData)) {
                $components = array('Session'); 
                $this->Session->setFlash(‘House DATA have been saved!');
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __('The House Data could not be saved. Please, try again.')
            );
        }
     }

when add() is used in userscontroller, it saves users and managers data with foreign keys.i.e manager_id is saved in users table as foreign key. but similar functions addCustomer() and addHouse() are not saving the foreign keys. customers table should save manager_id as foreign key and houses table should save customer_id as foreign key. since their relations are like - manager hasMany customer hasMany house. why theses similar functions are behaving differently here ? Can anybody give me any hints, why foreign keys are not saved ?

Upvotes: 0

Views: 230

Answers (1)

Akshay Sharma
Akshay Sharma

Reputation: 1112

You should first save data in your parent table like users table Like this

$saveData = $this->data['User'];
$this->loadModel('User');
$this->User->save($saveData);

Now get its primary key like this:

$userId = $this->User->id;

Now to save data in other table which are depend on '$userId' means $userId is now foreign Key for these tables like address , user details, managers etc; Now to save data in address table

$addressData = $this->data['Address'];
$addressData['user_id'] = $userId; // This be foreign Key for address table
$this->loadModel('Address');
$this->Address->save($addressData);

Like this you should save data in dependent tables
This complete code should be in your controller function where you want to save data.

Upvotes: 1

Related Questions