PHP code example of caio-brendo / yii2-dynamicgridform
1. Go to this page and download the library: Download caio-brendo/yii2-dynamicgridform 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/ */
caio-brendo / yii2-dynamicgridform example snippets
use app\models\Telephone;
use caiobrendo\dynamicgridform\ActionColumn;
use caiobrendo\dynamicgridform\DynamicGridForm;
use caiobrendo\dynamicgridform\NormalColumn;
use yii\helpers\Html;
use yii\web\JsExpression;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $form yii\widgets\ActiveForm */
/* @var $telephones Telephone[] */
namespace app\controllers;
use app\models\Model;
use app\models\Telephone;
use app\models\User;
use caiobrendo\dynamicgridform\Helper;
use Yii;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
/**
* UserController implements the CRUD actions for User model.
*/
class UserController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Creates a new User model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new User();
$post = Yii::$app->request->post();
$telephones = Helper::createMultiple(Telephone::class);
if ($model->load($post) && Model::loadMultiple($telephones, $post) && $model->save()) {
foreach ($telephones as $telephone) {
$telephone->user_id = $model->id;
if (!$telephone->save()) {
return false;
}
}
return $this->redirect(['update', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
'telephones' => $telephones
]);
}
/**
* Updates an existing User model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$telephones = $model->telephones;
$post = Yii::$app->request->post();
if ($model->load($post) && $model->save()) {
$telephones = Helper::createMultiple(Telephone::class, $telephones);
Model::loadMultiple($telephones, $post);
$olds = ArrayHelper::map($model->telephones, 'id', 'id');
$news = ArrayHelper::map($telephones, 'id', 'id');
$delete = array_diff($olds, $news);
Telephone::deleteAll(['id' => $delete]);
foreach ($telephones as $telephone) {
$telephone->user_id = $model->id;
if (!$telephone->save()) {
return false;
}
}
return $this->redirect(['update', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
'telephones' => $telephones
]);
}
/**
* Finds the User model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return User the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = User::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}