https://github.com/sdailover/yii2-phpsessconnector
The SDaiLover PHP Session Connector extension for the Yii 2.0 Framework
https://github.com/sdailover/yii2-phpsessconnector
active-record php-session phpsessconnector sdailover yii2 yii2-extension
Last synced: 6 months ago
JSON representation
The SDaiLover PHP Session Connector extension for the Yii 2.0 Framework
- Host: GitHub
- URL: https://github.com/sdailover/yii2-phpsessconnector
- Owner: SDaiLover
- License: bsd-3-clause
- Created: 2024-03-04T00:11:26.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-04T00:12:49.000Z (over 2 years ago)
- Last Synced: 2025-02-16T09:41:51.414Z (over 1 year ago)
- Topics: active-record, php-session, phpsessconnector, sdailover, yii2, yii2-extension
- Language: PHP
- Homepage: https://www.sdailover.com
- Size: 21.5 KB
- Stars: 0
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
SDaiLover PHPSessionConnector for Yii 2
# yii2-phpsessconnector
Runtime database helper to choose PHP Session or another database without change structure model of [Yii framework 2.0](https://www.yiiframework.com).
For license information check the [LICENSE](LICENSE.md)-file.
Documentation is at [docs/guide/README.md](docs/guide/README.md).
[](https://github.com/sdailover/yii2-phpsessconnector)
[](https://github.com/sdailover/yii2-phpsessconnector)
[](https://github.com/sdailover/yii2-phpsessconnector)
[](https://github.com/sdailover/yii2-phpsessconnector)
[](https://github.com/sdailover/yii2-phpsessconnector)
[](https://packagist.org/packages/sdailover/yii2-phpsessconnector)
[](https://packagist.org/packages/sdailover/yii2-phpsessconnector)
[](https://github.com/sdailover/yii2-phpsessconnector)
[](https://github.com/sdailover/yii2-phpsessconnector)
[](https://github.com/sdailover/yii2-phpsessconnector)
[](https://github.com/sdailover/yii2-phpsessconnector)
[](https://github.com/sdailover/yii2-phpsessconnector/pulls)
[](https://github.com/sdailover/yii2-phpsessconnector/issues)
[](https://github.com/sdailover/yii2-phpsessconnector/discussions)
[](https://github.com/sdailover/yii2-phpsessconnector)
[Report Bug](https://github.com/sdailover/yii2-phpsessconnector/issues/new?assignees=&labels=bug&projects=&template=bug_report.yml)
·
[Request Feature](https://github.com/sdailover/yii2-phpsessconnector/issues/new?assignees=&labels=enhancement&projects=&template=feature_request.yml)
·
[Provide Feedback](https://github.com/sdailover/yii2-phpsessconnector/discussions/new?category=ideas&title=Suggest%20for%20SDaiLover%20Yii2%20PhpSessConnector)
·
[Ask Question](https://github.com/sdailover/yii2-phpsessconnector/discussions/new?category=q-a&title=Ask%20Question%20for%20SDaiLover%20Yii2%20PhpSessConnector)
Love the project? Please consider [donating](https://opencollective.com/sdailover) or give :star: to help it improve!
Copyright © ID 2024 SDaiLover ([www.sdailover.com](https://sdailover.com))
All rights reserved.
***
Installation
------------
The preferred way to install this extension is through [composer](https://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist sdailover/yii2-phpsessconnector
```
or add
```json
"sdailover/yii2-phpsessconnector": "~1.0.0"
```
to the `require` section of your `composer.json`.
App Configuration
-----
To use this extension, simply add the following code in your application configuration:
```php
return [
//....
'components' => [
'db' => [
'class' => '\sdailover\yii\phpsessconnector\SDConnection',
'dsn' => 'phpsession:sdailover',
// prefix name of session
'tablePrefix' => 'sd_'
],
],
];
```
Model Usage
-----
To connect a database using ActiveRecord into a Model class:
```php
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);
}
//....
}
```
Provider Usage
-----
Data Providers are usually used to search in Models or display Models in the form of widgets such as GridView and other extensions. To implement it into the application created, we can configure it as follows:
```php
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;
}
//....
}
```
Controller Usage
-----
To use the `SDActiveRecord` and `SDDataProvider` that have been created, we can implement them into the Controller that will be used as follows:
```php
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]);
}
//....
}
```
# Support the project
We open-source almost everything We can and try to reply to everyone needing help using these projects. Obviously, this takes time. You can use this service for free.
If you are using this project and are happy with it or just want to encourage us to continue creating stuff, there are a few ways you can do it:
- Giving proper credit on the GitHub Sponsors page. [](https://github.com/sponsors/sdailover)
- Starring and sharing the project :star:
- You can make one-time donations via PayPal. I'll probably buy a coffee :coffee: or tea :tea: or cake :cake:
[](https://www.paypal.me/sdailover)
- It’s also possible to support mine financially by becoming a backer or sponsor through
[](https://www.opencollective.com/sdailover)
However, we also provide software development services. You can also invite us to collaborate to help your business in developing the software you need. Please contact us at:
[](mailto:team@sdailover.com)
## :pray: Thanks for your contribute and support! :heart_eyes: :heart:
> Any Questions & Other Supports? see [Support](https://github.com/sdailover/.github/blob/master/SUPPORT.md) please.
***
[Visit Website](https://www.sdailover.com)
·
[Global Issues](https://github.com/sdailover/.github/issues/new/choose)
·
[Global Discussions](https://github.com/sdailover/.github/discussions)
·
[Global Wiki](https://github.com/sdailover/.github/wiki)
Copyright © ID 2024 by SDaiLover ([www.sdailover.com](https://sdailover.com))
[](https://github.com/sdailover/.github/blob/master/LICENSE.md)
All rights reserved.