2015-09-10 20 views
6

mieć wiele modelu na samej formie nie chcę zapisywać dane zarówno na modelu, ale po prostu chcę, aby wyświetlić jedną zmienną na widoku plikuMultiple Model on sam ActiveForm yii2

Mam 2 tabele Organiser i Event Organiser będzie zalogować się i Create Event Obecnie istnieje kilka pól, które są wspólne w obu tabelach więc po zalogowaniu się użytkownika kliknięcia na Create Event przycisku chcę, aby wyświetlić address z Organiser tabeli na formularzu

To, co zrobiłem do tej pory które mogą nie być odpowiednie podejście więc daj mi znać, jeśli to można osiągnąć jakikolwiek inny prostszy sposób

public function actionCreate() 
{ 
    $model = new Event(); 

    $model2 = $this->findModel2(Yii::$app->user->id); 


    if ($model->load(Yii::$app->request->post())) { 

     if($_POST['User']['address'] == null) 
     { 
      $model->location = $model2->address; 
     } 
     else{ 
      $model->location = $_POST['User']['address']; 
     } 

     $model->is_active = 1 ; 

     $model->save(); 


     return $this->redirect(['view', 'id' => $model->id]); 
    } else { 

     return $this->render('create', [ 
      'model' => $model, 
      'model2' => $model2 
     ]); 
    } 
} 
protected function findModel2($id) 
{ 
    if (($model2 = User::findOne($id)) !== null) { 
     return $model2; 
    } else { 
     throw new NotFoundHttpException('The requested page does not  exist.'); 
    } 
} 

I tu jest mój kod _form.php

<?php $form = ActiveForm::begin([ 
    'options' => [ 
     'id' => 'create-event-form' 
    ] 
]); ?> 


<?= $form->field($model, 'interest_id')->dropDownList(
    ArrayHelper::map(Areaintrest::find()->all(),'id','area_intrest'), 
    ['prompt'=> 'Select Event Category'] 
) ?> 

<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?> 
    <?php 
     if ($model->isNewRecord){ 
     echo $form->field($model2,'address')->textInput(['class' => 'placepicker form-control'])->label('Location'); 
    } 
    else 
     echo $form->field($model,'location')->textInput(['class' => 'placepicker form-control']); 

?> 

<di"form-group"> 
     <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update',  ['class' => $model->isNewRecord ? 'btn btn-danger' : 'btn btn-primary']) ?> 
</div> 

<?php ActiveForm::end(); ?> 

Teraz wydaje się, że problem polega na tym, że zapisywanie rzutów częściowych error mówi: unknown property address, ponieważ to pole nie istnieje w tabeli bazy danych Event Chcę tylko, aby te dane były wyświetlane y go na polu lokalizacji mojego create form

więc co należy zrobić tutaj

Oto struktura tabeli

Organiser   Event 
organiser_id  event_id 
username   organiser_id 
clubname   title 
image    location 
email 
firstname 
lastname  
address 

strona Błąd mówi coś takiego

Unknown Property – yii\base\UnknownPropertyException 

Getting unknown property: common\models\Event::address 

A tutaj to mój model Event

class Event extends \yii\db\ActiveRecord 
{ 

    public static function tableName() 
    { 
     return 'event'; 
    } 


public function rules() 
{ 
    return [ 
     [['organiser_id', 'interest_id', 'title', 'location'], 'required'], 
     [['organiser_id', 'interest_id'], 'integer'], 
     [['title', 'location'], 'string', 'max' => 255], 
    ]; 
} 

/** 
* @inheritdoc 
*/ 
public function attributeLabels() 
{ 
    return [ 
     'id' => 'ID', 
     'organiser_id' => 'Organiser ID', 
     'interest_id' => 'Event Type', 
     'title' => 'Event Title', 
     'location' => 'Location' 

    ]; 
} 


public function getOrganiser() 
{ 
    return $this->hasOne(User::className(), ['organiser_id' => 'organiser_id']); 
} 

}

Oto moja User modelu reprezentują Organiser tabeli i nazwę modelu w modelu frontend SignUp.php

class SignupForm extends Model 
{ 
public $username; 
public $address; 
public $password; 
public $password_repeat; 



public function rules() 
{ 
    return [ 
     ['username', 'filter', 'filter' => 'trim'], 
     ['username', 'required'], 
     ['address', 'required'], 
     ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'], 
     ['username', 'string', 'min' => 2, 'max' => 255], 


     [['file'], 'file', 'extensions'=>'jpg, gif, png'], 



     ['password', 'required'], 
     ['password', 'string', 'min' => 6], 

     ['password_repeat', 'compare', 'compareAttribute' => 'password'], 
    ]; 
} 

public function attributeLabels() 
{ 
    return [ 
     'password_repeat' => 'Confirm Password', 
     'address' => 'Address', 
     'username' => 'Username', 
    ]; 
} 



public function signup() 
{ 
    if ($this->validate()) { 
     $user = new User(); 
     $model = new SignupForm(); 
     $user->address = $this->address; 
     $user->username = $this->username; 

     $user->setPassword($this->password); 
     $user->generateAuthKey(); 
     if ($user->save()) { 
      return $user; 
     } 
    } 

    return null; 
} 

}

więc chcę, aby wyświetlić organiser.address na formularzu w polu Create eventlocation Mam nadzieję, że teraz możesz to zrozumieć dziękuję

+0

Nie otrzymuję tego dokładnie. Czy możesz pokazać swoją strukturę stołu? –

+0

@InsaneSkull Zaktualizowałem strukturę bazy danych. Wszystko, co chcę zrobić, to wyświetlać wartości w polu z innej tabeli jako wartość domyślną. –

+0

Możesz dodać ** Wirtualne atrybuty ** z nazwą 'address' w swoim modelu sprawdź ten http: //www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-metody/ –

Odpowiedz

3

Może być czynność powinna wyglądać

public function actionCreate() 
{ 
    $event= new Event(); 

    $user= $this->findModel2(Yii::$app->user->id); 
    $event->location = $user->address; 

    if ($event->load(Yii::$app->request->post()) && $event->save()) {//active can set by default validator 
     return $this->redirect(['view', 'id' => $event->id]); 
    } 
    return $this->render('create', [ 
     'event' => $event 
    ]); 

} 

iw formie pokazać lokalizację zawsze. Następnie możesz użyć go w aktualizacji i tworzyć również bez ifs w widoku. Mam nadzieję, że to ci pomoże.

+0

Perfekcyjne rozwiązanie ... W ten sposób nie muszę przekazywać wielu modeli do formularza i żadnych niepotrzebnych warunków. Dziękuję bardzo –