कई मॉडल एक ही रूप पर मैं दोनों मॉडल पर डेटा सहेजना चाहते हैं न, लेकिन मैं सिर्फ दृश्य फ़ाइल पर एक चर प्रदर्शित करने के लिएही ActiveForm पर एकाधिक मॉडल yii2
2 टेबल Organiser
और Event
Organiser
है चाहते हैं में प्रवेश करें और होगा Create Event
अब कुछ क्षेत्रों जो दोनों तालिकाओं इसलिए जब Create Event
बटन पर उपयोगकर्ता क्लिक में लॉग इन मैं फार्म पर Organiser
मेज से address
प्रदर्शित करना चाहते हैं में सामान्य हैं
यह मैं अब तक क्या किया है जो नहीं हो सकता है सही दृष्टिकोण इसलिए मुझे पता है कि अगर किसी भी अन्य आसान तरीका
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.');
}
}
प्राप्त किया जा सकता और यहाँ मेरी _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(); ?>
अब समस्या यह है कि जब भाग बचाने की बात आती है तो error
unknown property address
कहता है क्योंकि वह फ़ील्ड डेटाबेस तालिका Event
में मौजूद नहीं है, मैं बस उस डेटा को डिस्प्ले करना चाहता हूं y ने इसका स्थान मैदान पर मेरी create form
तो मैं यहाँ क्या करना चाहिए
यहाँ तालिका संरचना
Organiser Event
organiser_id event_id
username organiser_id
clubname title
image location
email
firstname
lastname
address
त्रुटि पृष्ठ का कहना है इस
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: common\models\Event::address
और यहाँ की तरह कुछ मेरा 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']);
}
}
यहाँ मेरी User
मॉडल दृश्यपटल मॉडल में Organiser
मेज और मॉडल का नाम का प्रतिनिधित्व करते है 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;
}
}
तो मैं क्षेत्र में Create event
फार्म पर organiser.address
प्रदर्शित करना चाहते हैं location
आशा है कि आप इसे अब समझ सकते हैं धन्यवाद
मुझे यह बिल्कुल ठीक नहीं हो रहा है। क्या आप अपनी टेबल संरचना दिखा सकते हैं? –
@InsaneSkull मैंने डेटाबेस संरचना अद्यतन की है। मैं बस एक डिफ़ॉल्ट तालिका के रूप में किसी अन्य तालिका से किसी फ़ील्ड पर प्रदर्शन मान करना चाहता हूं। –
आप अपने मॉडल में नाम 'पता' के साथ ** वर्चुअल एट्रिब्यूट ** जोड़ सकते हैं यह http: //www.yiiframework देखें।कॉम/विकी/167/समझ-वर्चुअल-विशेषताएं-और-get-set-methods/ –