简体   繁体   中英

cakephp : update another table's data on update

I have these tables

users table :

id  lastname    firstname   email                   company             city        phone
1   Doe         John        doe.john@email.com      somecompany         New-York    85505040

companies table :

id  name            adress              city        phone
1   somecompany     1881 Paper Street   New-York    85505042
2   anothercompany  18  Digital Street  New-York    87505040

companies_users table :

id  user_id companies_id
1   1       2

When I update a user(changing the company they belong to), the companies_users table updates the companies_id. The cakephp app is something I'm inhereting so I don't find everything easily.

What I want to add, is to able to update a user's company when I change something in the companies table : if I change the name, I want the name to change in users table as well.

CompaniesController edit :

 public function edit($id = null){
    $companie = $this->Companies->get($id);
    if ($this->request->is(['post', 'put'])) {
        if ($this->Companies->save($companie)) {
            $this->Flash->success(__('Your company has been updated.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to update your company.'));
    }

}

UsersController edit :

 public function edit($id = null){
    $user = $this->Users->get($id);
    if ($this->request->is(['post', 'put'])) {
        $user = $this->Users->patchEntity($user, $this->request->data,['associated' => ['Companies']]);
        if ($this->Users->save($user)) {
            $this->Flash->success(__('Your user has been updated.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to update your user.'));
    }
    $this->set('user', $user);

        $companies = $this->Users->Companies->find('list');
    $this->set(compact('companies'));
}

Hello Here is easy way to update both table: first create the foreign key in company table user_id(int)(11) NULL When you update User table:

public function edit_user($id = null)
{
   // check if id in empty or not
  if(empty($id))
  {
    // redirect to list page
  }

   $this->User->id = $id;
     if ($this->request->is('put') || $this->request->is('post')) {
     if ($this->User->save($this->request->data))
     {
         $this->Companies = ClassRegistry::init('Companies');
         $comp_id = $this->Companies->find('first',array(
            'contain' => array(),
            'fields' => array('Companies.id'),
            'conditions' => array('Companies.user_id' => $id),
          ));
         $this->Companies->id = $comp_id['Companies']['id'];
         $this->Companies->savefield('name',$this->request->data['User']['company']);
          // success message place here
     }
    else
     {
       // false message write
  }

      }
}

Use Same Method for Company Table Update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM