Search This Blog

Sunday, December 5, 2010

Save new record or Update a record if it exist using Doctrine object

Are getting error when you save a doctrine object saying primary key violation?
Before you save the object you must search if its already there. When you use find it uses the primary key.

public function saveFamilyDetails($form) {

$employeeFamilyDetails = Doctrine::getTable('EmployeeFamilyDetails')->find($form['txtFamilyID']);

        if(!($employeeFamilyDetails instanceof EmployeeFamilyDetails) ) {
            $employeeFamilyDetails = new EmployeeFamilyDetails();
        }

        $employeeFamilyDetails->employee_id       = $form['txtEmpID'];
        $employeeFamilyDetails->fm_name       = $form['txtFamName'];
        $employeeFamilyDetails->relationship       = $form['txtFamRelationship'];
        $employeeFamilyDetails->date_of_birth        = $form['txtFamDob'];
        $employeeFamilyDetails->address        = $form['txtFamAddress'];
        $employeeFamilyDetails->mobile_no      = $form['txtFamMobileNo'];
        $employeeFamilyDetails->residence_no    = $form['txtFamRecidenNo'];
        $employeeFamilyDetails->workcon_no   = $form['txtFamWorkConNo'] ;
        $employeeFamilyDetails->dependent      = $form['txtFamDependant'];
        $employeeFamilyDetails->nric_fin    = $form['txtFamNricFin'] ;
        $employeeFamilyDetails->passpot_no   = $form['txtFamPassport'] ;

        return $employeeFamilyDetails->save();



    }

No comments: