PHP code example of yii1x / active-record

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

    

yii1x / active-record example snippets


use Yii1x\ActiveRecord\ORMContext;

ORMContext::bootstrap($container, debug: false);


return [
    'db_name' => [
        'class' => \Yii1x\ActiveRecord\Db\DbConnection::class,
        '__construct()' => [
            'dsn' => 'mysql:host=db;port=3306;dbname=yii3',
            'username' => 'root',
            'password' => 'root',
            'connectionName' => 'db_name',
        ],
    ],
];

#[Database(name: 'db_name')]
class User extends ActiveRecord
{
    // ...
}

use Yii1x\ActiveRecord\ActiveRecord;
use Yii1x\ActiveRecord\Attributes\Table;
use Yii1x\ActiveRecord\Attributes\Database;

#[Table(name: 'user')]
#[Database(name: 'db_name')]
class User extends ActiveRecord
{
    public function relations(): array
    {
        return [
            'posts' => [self::HAS_MANY, Post::class, 'user_id'],
        ];
    }
}

// Find one
$user = User::model()->findByAttributes(['email' => '[email protected]']);

// new fluent query builder
$users = User::queryBuilder()
    ->where('status', 1)
    ->orderBy('created_at DESC')
    ->limit(10)
    ->with('posts')
    ->findAll();