/yii2-phpsessconnector

The SDaiLover PHP Session Connector extension for the Yii 2.0 Framework

Primary LanguagePHPBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

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.

For license information check the LICENSE-file.

Documentation is at docs/guide/README.md.

PHP Language Code Editor PHP Framework CSS Bootstrap JS jQuery

Latest Stable Version Total Downloads GitHub watchers GitHub Repo stars GitHub issues

GitHub contributors GitHub pull requests GitHub issues GitHub Discussions GitHub last commit (by committer)

Report Bug · Request Feature · Provide Feedback · Ask Question

Love the project? Please consider donating or give ⭐ to help it improve!

Copyright © ID 2024 SDaiLover (www.sdailover.com)

All rights reserved.


Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist sdailover/yii2-phpsessconnector

or add

"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:

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:

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:

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:

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. Static Badge
  • Starring and sharing the project ⭐
  • You can make one-time donations via PayPal. I'll probably buy a coffee ☕ or tea 🍵 or cake 🍰
    paypal.me/sdailover
  • It’s also possible to support mine financially by becoming a backer or sponsor through
    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:
team@sdailover.com

🙏 Thanks for your contribute and support! 😍 ❤️

Any Questions & Other Supports? see Support please.


Visit Website · Global Issues · Global Discussions · Global Wiki

Copyright © ID 2024 by SDaiLover (www.sdailover.com)

SDaiLover License

All rights reserved.