[Solved] relaitions -> edit all fields

Hi

I managed (after long trying) to make with Gii a testproject with 2 relationed tables (1:1).

in frontend/models/PoiCore.php


    

public function getPoiDetail()

    {

        return $this->hasOne(PoiDetail::className(), ['poi_id' => 'id']);

    }




I also managed to view in ‘index’ all fields from both tables.




   <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],


            'id',

            'owner',

            'poi_name',

           'poiDetail.poi_lon',

           'poiDetail.poi_lat',



But what do I have to do for editing / creating / saveing all fields from both tables?

e.g. editing of all fields in one view -> so your have a good overview over your row of data.

Or is it not possible?

Thank you.

you can refer this

http://www.yiiframework.com/forum/index.php/topic/53935-solved-subforms/page__p__248184#entry248184

Thank you lost rups g.

But my problem starts in the view.

As I told, I can see the field (attribut?) from related tabler in index.

But in e.g. create (_form.php), how can I display (editable) this field?


    <?= $form->field($model, 'poi_lat')->textInput() ?>



or


 <?= $form->field($model, 'poiDetail.poi_lat')->textInput() ?>

is not working (as it does in index.php).

You have to use two model in create/update model.

and also have to pass model para in return.


public function actionCreate()

{   

    $user = new poiDetail;

    $profile = new PoiCore;


    if ($user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post()) && Model::validateMultiple([$user, $profile])) {

        

        $user->save(false); // skip validation as model is already validated

        $profile->user_id = $user->id; // no need for validation rule on user_id as you set it yourself

        $profile-save(false); 

        

        return $this->redirect(['view', 'id' => $user->id]);

    } else {

        return $this->render('create', [

            'user' => $user,

            'profile' => $profile,

        ]);

    }

}

Thank you ‘rups webin’!

It works so far. Slowly, realy slowly, I beginn to figure out how the framework works ;)

In your code was




    if ($user->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post()) && Model::validateMultiple([$user, $profile])) { 



But the validation seams to fail.

No output on screen, only redirect to create

(is there a logfile to look at?)

I had to add propper rules in both models


    public function rules()

    {

No id rule for example ;)

and for deleting both, I found

that helped

Thank you!!

Marked as ‘+’ if solved.

Yes u have to define rules in each model separately.