承接 vigihdev/yii2-bridge-active-record 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vigihdev/yii2-bridge-active-record

Composer 安装命令:

composer require vigihdev/yii2-bridge-active-record

包简介

Enhanced Active Record bridge for Yii2 with service container integration, validation, caching, and multilingual support

README 文档

README

Latest Stable Version Total Downloads License Build Status

Enhanced Active Record bridge for modern PHP applications, integrating a powerful service container, advanced validation, caching, and multilingual support. This library provides a flexible and robust ORM layer, designed to be easily integrated into any project requiring a solid data persistence solution.

Features

  • Service Container Integration: Decouples database connections, making your models more portable and testable.
  • Modern Validation: Leverages yiisoft/validator for powerful and flexible validation rules (via vigihdev/yii2-bridge-validator).
  • Event-Driven Architecture: Dispatch events on AfterInsert, AfterUpdate, and AfterDelete to hook into the model lifecycle.
  • Seamless Relations: Easily define and query relationships between your entities.
  • Modern Data Types: Automatic handling of DateTimeImmutable for date/time fields.

Installation

Install the package via Composer:

composer require vigihdev/yii2-bridge-active-record

Basic Usage

First, ensure your service container is configured to provide database connections. This library relies on vigihdev/yii2-bridge-db for connection management.

1. Create a Base Model

Create your own base model that extends VigihDev\Yii2Bridge\ActiveRecord\BaseActiveRecord and specifies the database service name.

<?php

namespace App\Models;

use VigihDev\Yii2Bridge\ActiveRecord\BaseActiveRecord;

abstract class YourBaseModel extends BaseActiveRecord
{
    protected static function getServiceName(): string
    {
        // The name of the database connection service configured in your container
        return 'db';
    }
}

2. Define an Entity

Create an entity class that represents a database table.

<?php

namespace App\Models;

use Yiisoft\ActiveRecord\ActiveQuery;

/**
 * @property int $id
 * @property string $username
 * @property string $email
 *
 * @property UserProfile $profile
 */
class User extends YourBaseModel
{
    public function tableName(): string
    {
        return '{{%user}}';
    }

    public function getProfile(): ActiveQuery
    {
        return $this->hasOne(UserProfile::class, ['user_id' => 'id']);
    }
}

3. Querying Data

You can now use the powerful query builder to fetch data.

// Find all users
$users = User::query()->all();

// Find a single user by primary key
$user = User::query()->where(['id' => 1])->one();

// Eager load relations
$usersWithProfiles = User::query()->with('profile')->all();

// Access related data
$username = $usersWithProfiles[0]->profile->full_name;

Testing

To run the test suite, use the following command:

./vendor/bin/phpunit

License

This project is licensed under the MIT License. See the LICENSE.md file for details.

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-01