nomac
(A Pani)
May 24, 2015, 4:56pm
1
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.
nomac
(A Pani)
May 25, 2015, 7:57am
3
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,
]);
}
}
nomac
(A Pani)
May 25, 2015, 10:39am
5
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!!
webin2015
(Webin2015)
May 25, 2015, 11:22am
6
nomac:
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
Thank you!!
Marked as ‘+’ if solved.
Yes u have to define rules in each model separately.