How To Insert And Update On Non Primary Key Table?

User have many companies.

So i want to insert user companies in user_companies table aftersave() user details.

Below is my code where save and update not working on it.


public function afterSave()

    {


        if(!empty($_POST["user_companies"])){


            foreach($_POST["user_companies"] as $val){





                $UserCompanies = new UserCompanies;

                $UserCompanies->person_id   = $this->primaryKey;

                $UserCompanies->company_id  = $val;


                if(UserCompanies::model()->exists('person_id = :person_id && company_id = :company_id',

                    array(":person_id"      => $this->primaryKey,

                        ":company_id"     => $val))){

                   

                    $UserCompanies->update(); //Update

                }

                else{

                   

                    $UserCompanies->save();  // Insert

                }


            }

        }

I have find solution by,

whenever current exist then dont make update.

if it is new record for user then it is inserted.

But when user deleted then how manage this user companies




public function afterSave()

    {


        if(!empty($_POST["user_companies"])){


            foreach($_POST["user_companies"] as $val){





                $UserCompanies = new UserCompanies;

                $UserCompanies->person_id   = $this->primaryKey;

                $UserCompanies->company_id  = $val;


                if(!UserCompanies::model()->exists('person_id = :person_id && company_id = :company_id',

                    array(":person_id"      => $this->primaryKey,

                        ":company_id"     => $val))){

                   

                    $UserCompanies->save();  // Insert

                }


            }

        }

You can use updateAll() instead and assign the conditions for person_id and company_id.

Don’t update really all :wink:

Hello Joblo,

Thanks for the reply.

I have realized that when user update his user_companies then two thing possible

1. He assign new companies to him

So i have to add it to user_companies table.

2. Remove companies from already assigned companies

So i have to delete other companies with this user.

Check my updaed code :


public function afterSave()

    {


        //Person Type user then it can insert user companies

        if(!empty($_POST["user_companies"]) && $this->person_type == "user"){


            foreach($_POST["user_companies"] as $val){


                $UserCompanies              = new UserCompanies;

                $UserCompanies->person_id   = $this->primaryKey;

                $UserCompanies->company_id  = $val;


                if(!UserCompanies::model()->exists('person_id = :person_id && company_id = :company_id',

                    array(":person_id"      => $this->primaryKey,

                        ":company_id"     => $val))){


                    $UserCompanies->save();

                }


            }


            // while updating when user remove company from his/her old company so remove all comapnies that are not current selected companies of this person


            UserCompanies::model()->deleteAll("person_id =".$this->primaryKey." && company_id NOT IN(".implode(",",$_POST["user_companies"]).")");


        }else{

              // Remove Companies from user_companies whenever user becomes person then it requires.

            UserCompanies::model()->deleteAll("person_id =".$this->primaryKey);


        }

        parent::afterSave();


    }

And Thank you for your valuable reply.

Hi, To work with Many-To-Many relations you can use esaverelatedbehavior extension.

In this tutorial you can read more details about this extension.