0

I want do a project with YiiFramework. in this project I have 1 table that have many relation I want create a form that insert data in main table and all relation for example : I want a form that add student information I have 2 table first : id name family and second: id student_id field I want add data in table 1 and then add data in table 2 and all of the this jobs do in a form do u have any solution for that?

my really reations :

'homehouse' => array(self::HAS_ONE, 'Homehouse', 'HouseId'),
        'houseType' => array(self::BELONGS_TO, 'Parametervalues', 'HouseTypeId'),
        'owner' => array(self::BELONGS_TO, 'Person', 'OwnerId'),
        'region' => array(self::BELONGS_TO, 'Region', 'RegionId'),
        'housemultimedias' => array(self::HAS_MANY, 'Housemultimedia', 'HouseId'),
        'housestages' => array(self::HAS_MANY, 'Housestage', 'HouseId'),
        'tradehouse' => array(self::HAS_ONE, 'Tradehouse', 'HouseId'),
1
  • 1
    Whats the problem what have you tried ??
    – Ninad
    Jun 27, 2013 at 8:40

1 Answer 1

1

You have to do it with each model, so, let's say you have 2 models (student and job) and you're sending the data from the form with POST method.

Them, in your controller, you save first the main data and second the relation, for example: (ps. this is just a hypothetical example)

Controller

public function actionSave(){
    if(isset($_POST['Student'])) {

        $Student                  = new Student();
        $Student->Job             = new Job();
        $Student->attributes      = $_POST['Student'];
        $Student->Job->attributes = $_POST['Job'];

        if($Student->save()){         
             $Student->Job->student_id = $Student->id;
             $Student->Job->save();
        }    
    }
}

With this idea, you can save the data in many relations that you model has.

Reference: How to save related objects?

1
  • Hi , sorry for late response . your answer was great and worked fine for me , thanks!!!
    – amin
    Aug 6, 2013 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.