PHP code example of yii2tech / filedb

1. Go to this page and download the library: Download yii2tech/filedb 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/ */

    

yii2tech / filedb example snippets


return [
    'components' => [
        'filedb' => [
            'class' => 'yii2tech\filedb\Connection',
            'path' => '@app/data/static',
        ],
        // ...
    ],
    // ...
];

// file 'UserGroup.php'
return [
    [
        'id' => 1,
        'name' => 'admin',
        'description' => 'Site administrator',
    ],
    [
        'id' => 2,
        'name' => 'member',
        'description' => 'Registered front-end user',
    ],
];

// file 'UserGroup.php'
return [
    1 => [
        'name' => 'admin',
        'description' => 'Site administrator',
    ],
    2 => [
        'name' => 'member',
        'description' => 'Registered front-end user',
    ],
];

use yii2tech\filedb\Query;

$query = new Query();
$query->from('UserGroup')
    ->limit(10);
$rows = $query->all();

$query = new Query();
$row = $query->from('UserGroup')
    ->where(['name' => 'admin'])
    ->one();

class UserGroup extends \yii2tech\filedb\ActiveRecord
{
    public static function fileName()
    {
        return 'UserGroup';
    }
}

class UserGroup extends \yii2tech\filedb\ActiveRecord
{
    public function getUsers()
    {
        return $this->hasMany(User::className(), ['groupId' => 'id']);
    }
}

class User extends \yii\db\ActiveRecord
{
    public function getGroup()
    {
        return $this->hasOne(UserGroup::className(), ['id' => 'groupId']);
    }
}

$users = User::find()->with('group')->all();
foreach ($users as $user) {
    echo 'username: ' . $user->name . "\n";
    echo 'group: ' . $user->group->name . "\n\n";
}

$adminGroup = UserGroup::find()->where(['name' => 'admin'])->one();
foreach ($adminGroup->users as $user) {
    echo $user->name . "\n";
}

php composer.phar