PHP code example of sdailover / yii2-phpsessconnector
1. Go to this page and download the library: Download sdailover/yii2-phpsessconnector 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/ */
sdailover / yii2-phpsessconnector example snippets
namespace app\models;
use sdailover\yii\phpsessconnector\SDActiveRecord;
class ModelClass extends SDActiveRecord
{
//....
/* Create list attribute or name's field of database. */
public $attribute;
/**
* Default data imported into the php session,
* this data only load to php session and not
* import data to real database (mysql, sqlite, others).
*/
private static $data = [
[
'attribute' => 'value',
//....
]
];
/**
* Set the name of the database table or table session.
*/
public static function tableName()
{
return '{{tablename}}';
}
/**
* Load and import default data to php session.
*/
public static function loadTable()
{
parent::records(static::$data);
}
//....
}
namespace app\models;
use app\models\ModelClass;
use sdailover\yii\phpsessconnector\SDActiveProvider;
class ModelSearchClass extends ModelClass
{
//....
public function search($params)
{
$query = ModelClass::find();
$dataProvider = new SDActiveProvider([
'query' => $query
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
// Add filter condition
if ($this->attribute !== null && !empty($this->attribute))
$query->andFilterWhere(['attribute' => $this->attribute]);
return $dataProvider;
}
//....
}
namespace app\controllers;
use yii\web\Controller;
use app\models\ModelClass;
use app\models\ModelSearchClass;
class SiteController extends Controller
{
//....
/**
* Display model from SDActiveRecord.
*/
public function actionView()
{
$pkId = Yii::$app->request->isGet ? Yii::$app->request->get('attribute') : Yii::$app->request->post('attribute');
$model = ModelClass::findOne($pkId);
return $this->render('view', ['model'=>$model]);
}
/**
* Display many model from SDActiveRecord with Data Provider.
*/
public function actionSearch()
{
$searchModel = new ModelSearchClass();
$searchParams = Yii::$app->request->isGet ? Yii::$app->request->get() : Yii::$app->request->post();
$dataProvider = $searchModel->search($searchParams);
return $this->render('search', ['dataProvider'=>$dataProvider]);
}
//....
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.