1
Ich versuche mehrsprachige Inhalte im Modell zu speichern. Ich habe zwei ModelleWie kann ich Inhalte speichern?
PostLang
class PostLang extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'postlang';
}
public function rules()
{
return [
[['post_id', 'lang_id', 'title', 'content'], 'safe'],
[['post_id', 'lang_id'], 'integer'],
[['title', 'content'], 'string'],
[['post_id'], 'exist', 'skipOnError' => true, 'targetClass' => Post::className(), 'targetAttribute' => ['post_id' => 'id']],
];
}
...
public function getPost()
{
return $this->hasOne(Post::className(), ['id' => 'post_id']);
}
}
und Post
class Post extends \yii\db\ActiveRecord {
public static function tableName() {
return 'post';
}
public function behaviors() {
return [
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
\yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['date_create', 'date_update'],
\yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => ['date_update'],
],
],
];
}
public function rules() {
return [
[['status', 'date_update', 'date_create'], 'integer'],
[['date_update', 'date_create'], 'safe'],
];
}
...
public function getPostlangs() {
return $this->hasMany(Postlang::className(), ['post_id' => 'id']);
}
}
habe ich eine Postcontroller mit create-Methode
public function actionCreate() {
$model = new Post();
$post_ru = new PostLang();
$post_en = new PostLang();
if ($model->load(Yii::$app->request->post())) {
if ($model->save()) {
$dbPost = new PostLang();
$dbPost->title = need to save title;
$dbPost->content = need to save content;
$dbPost->lang_id = need to save lang_id;
$dbPost->post_id = $model->id;
$dbPost->save();
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
Ich brauche es zu speichern in für dich ch aber ich verstehe nicht, wie kann ich es tun. Formular
...
<?= $form->field($post_ru, 'title')->textInput() ?>
<?= $form->field($post_ru, 'content')->textInput() ?>
<?= $form->field($post_en, 'title')->textInput() ?>
<?= $form->field($post_en, 'content')->textInput() ?>
...
Fehler bei 'if ($ post_ru-> save()) {'" Integritätseinschränkung: 1452 Hinzufügen oder Aktualisieren einer untergeordneten Zeile: Eine Fremdschlüsseleinschränkung schlägt fehl ('asur'.postlang', CONSTRAINT' lang-to-post 'FOREIGN KEY (' post_id') REFERENZEN 'post' (' id') ON DELETE CASCADE AUF UPDATE CASCADE) " – Slip
Gehe zur yii2 Debug-Symbolleiste und wähle db tab. Es gibt sql Anfrage, die fehlschlägt.Senden Sie mich, ich werde überprüfen. –