smorken/ext-database

Laravel 9 的数据库扩展

v9.0.1 2022-06-06 19:23 UTC

README

许可证

本软件是开源软件,遵循MIT许可证

Laravel 框架是开源软件,遵循MIT许可证

需求

安装

  • 将以下内容添加到您的 Laravel 应用程序 composer.json 文件中
"require": {
    "smorken/ext-database": "~5.5"
}
  • 运行 composer update

  • 将服务提供者添加到 config/app.php 文件中

'providers' => [
...
    \Smorken\Ext\Database\DatabaseServiceProvider::class,

构建器扩展

  • ::createOrUpdate($keys, array $values) - 这将尝试执行 mergeinsert ... on duplicate key update

  • ::concatenate($values, $separator = null, $as_expression = false) - 这将返回一个连接的字符串或表达式

  • ::complexJoin($toTable, $fromTable, $keys, $joinType = 'join') - 该方法是简化使用多个键构建连接的辅助工具

模型扩展

  • ::createOrUpdate($keys, array $data) - 参见构建器

  • ::compositeBelongsTo($related, array $foreignKey, array $otherKey, $relation = null) - 使用复合键创建属于关系

  • ::compositeHasMany($related, array $foreignKey, array $localKey) - 使用复合键创建拥有多个关系

  • ::compositeHasOne($related, array $foreignKey, array $localKey) - 使用复合键创建拥有一个关系

  • ::hasManyThroughArray($related, $through, array $nearToMid, array $midToFar) - 创建具有所有表上不同键的多个拥有通过关系

  • ::belongsToManyArray($related, array $parentToMid, array $midToFar, $table = null, $relation = null) - 创建具有所有表上不同键的多个属于关系

  • ::belongsToModel($related, array $attributes = [], $relation = null) 使用在加载的模型上找到的属性创建属于关系 - 默认为所有属性,属性可以是 'local_key' => 'model_key' 或 0 => 'key'(如果共享)

关系示例

复合属于
class MyModel extends \Smorken\Ext\Database\Eloquent\Model
{
    public function anotherModel()
        {
            return $this->compositeBelongsTo(
                AnotherModel::class,
                ['t2_join_1', 't2_join_2', 't2_join_3', 't2_join_4'], //$this columns
                ['t1_join_1', 't1_join_2', 't1_join_3', 't1_join_4']  //AnotherModel columns
            );
        }
    
        public function complicatedModel()
        {
            $func = function($start, $count) {
                return function($ev, $model) use ($start, $count) {
                    return substr($model->t2_join_1, $start, $count);
                };
            };
            $exp1 = new Expression('SUBSTR(t2_join_1, 1, 3)');
            $exp2 = new Expression('SUBSTR(t2_join_1, 4, 6)');
            $col1 = new ExpressionValue('t2_join_1', $exp1, $func(0, 3));
            $col2 = new ExpressionValue('t2_join_1', $exp2, $func(3, 3));
            return $this->compositeBelongsTo(
                ComplicatedModel::class,
                [$col1, $col2,],
                ['t1_join_1', 't1_join_2',]
            );
        }
}
复合拥有多个
class AnotherModel extends \Smorken\Ext\Database\Eloquent\Model
{
    public function myModels()
        {
            return $this->compositeHasMany(
                MyModel::class,
                ['t2_join_1', 't2_join_2', 't2_join_3', 't2_join_4'], //$this
                ['t1_join_1', 't1_join_2', 't1_join_3', 't1_join_4']  //MyModel
            );
        }
}

class ComplicatedModel extends \Smorken\Ext\Database\Eloquent\Model
{
    public function myModels()
        {
            $func = function($start, $count)
            {
                return function($ev, $model) use ($start, $count) {
                    return substr($model->t2_join_1, $start, $count);
                };
            };
            $exp1 = new Expression('SUBSTR(t2_join_1, 1, 3)');
            $exp2 = new Expression('SUBSTR(t2_join_1, 4, 6)');
            $col1 = new ExpressionValue('t2_join_1', $exp1, $func(0, 3));
            $col2 = new ExpressionValue('t2_join_1', $exp2, $func(3, 3));
            return $this->compositeHasMany(
                MyModel::class,
                [$col1, $col2],
                ['t1_join_1', 't1_join_2']
            );
        }
}
复合拥有一个
class MyModel extends \Smorken\Ext\Database\Eloquent\Model
{
    public function anotherModel()
        {
            return $this->compositeHasOne(
                AnotherModel::class,
                ['t2_join_1', 't2_join_2', 't2_join_3', 't2_join_4'], //$this columns
                ['t1_join_1', 't1_join_2', 't1_join_3', 't1_join_4']  //AnotherModel columns
            );
        }
}