Answer the question
In order to leave comments, you need to log in
Yii2 relations NULL Foreign key How to do CREATE/UPDATE correctly?
People, such trouble:
There is a groups table
...
...
And there is a files table
...
'file_id' => $this->integer(),
'id' => $this->primaryKey()->notNull()->unique(),
'filename' => $this->string(),
$this->addForeignKey(
'file-id',
'groups',
'file_id',
'files',
'id',
'CASCADE'
);
class Groups extends \yii\db\ActiveRecord
{
...
[['file_id'], 'default', 'value' => null],
[['file_id'], 'integer'],
[['file_id'], 'exist', 'skipOnError' => true, 'targetClass' => Files::className(), 'targetAttribute' => ['file_id' => 'id']],
...
public function getFile()
{
return $this->hasOne(Files::className(), ['id' => 'file_id']);
}
...
class Files extends \yii\db\ActiveRecord
{
...
public function getGroups()
{
return $this->hasMany(Groups::className(), ['file_id' => 'id']);
}
...
public function actionMyaction($id)
{
...
$model = $this->findModel($id);
...
return $this->render('Myaction', [
'model' => $model,
]);
<?= $form->field($model->file, 'filename')->textInput(); ?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question