1. Go to this page and download the library: Download solutosoft/yii-linkmany library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
solutosoft / yii-linkmany example snippets
class Post extends ActiveRecord
{
public function behaviors()
{
return [
'linkManyBehavior' => [
'class' => LinkManyBehavior::class,
'relations' => [
'tags',
'messages' => [
'formName' => 'Post[messages]',
'validate' => false,
'deleteOnUnlink' => false
]
]
],
];
}
public function getMessages()
{
return $this->hasMany(Message::class, ['post_id' => 'id']);
}
public function getTags()
{
return $this->hasMany(Tag::class, ['id' => 'tag_id'])
->viaTable('post_tag', ['post_id' => 'id']);
}
}
use yii\web\Controller;
class PostController extends Controller
{
public function actionCreate()
{
$model = new Post();
/**
* $_POST could be something like:
* [
* 'tags' => [1,2]
* 'comments' => [
* [
* 'subject' => 'First comment',
* 'content' => 'This is de fist comment',
* ], [
* 'subject' => 'Second comment',
* 'content' => 'This is de second comment',
* ]
* ]
* ];
*/
if ($model->fill(Yii::$app->request->post())) {
$model->save(); // save the model and relations
return $this->redirect(['view']);
}
return $this->render('create', [
'model' => $model,
]);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.