Tag: laravel

mysql数据表设计经验(结合laravel)

这是一份对前阶段数据表设计经验的总结:

READ MORE

使用laravel封装mysql存储过程的调用接口

在使用存储过程时,调用时通常如下:

1
2
3
set @out_status = 0;
call procedure_name(param1, ...,, @out_status);
select @out_status;

比较啰嗦,有必要进行一次封装,如下:

READ MORE

mysql存储过程管理(laravel下)

直接在mysql这个层面(例如命令行或者phpmyadmin等)管理存储过程是一个非常麻烦的事情:

  • 变更很难管理
  • 无法进行版本控制

第一步:把存储过程文件都放到git下管理

例如放在项目的sql目录下:

1
2
3
4
5
6
$ ls sql/
proc_money_appeal_payout.sql proc_money_apply_cash_payout.sql
proc_money_apply_cash_income_close.sql proc_money_reward_income.sql
proc_money_apply_cash_income_fail.sql proc_money_reward_payout.sql
proc_money_apply_cash_income_finish.sql proc_money_security_deposit_income.sql
proc_money_apply_cash_income.sql proc_money_security_deposit_payout.sql

第二步:生成导入数据库的sql文件

如果项目的存储过程比较多,每次更新时都可能会变得很麻烦,所以需要统一的导入入口,例如sql目录下生成source.sql文件,内容如下:

1
2
3
4
5
6
7
8
9
10
// vim sql/source.sql
source /home/code/ibbd/yanjiuquan-php/sql/proc_money_appeal_payout.sql;
source /home/code/ibbd/yanjiuquan-php/sql/proc_money_apply_cash_income.sql;
source /home/code/ibbd/yanjiuquan-php/sql/proc_money_apply_cash_income_close.sql;
source /home/code/ibbd/yanjiuquan-php/sql/proc_money_apply_cash_income_fail.sql;
source /home/code/ibbd/yanjiuquan-php/sql/proc_money_apply_cash_income_finish.sql;
source /home/code/ibbd/yanjiuquan-php/sql/proc_money_apply_cash_payout.sql;
source /home/code/ibbd/yanjiuquan-php/sql/proc_money_apply_cash_payout_close.sql;
source /home/code/ibbd/yanjiuquan-php/sql/proc_money_apply_cash_payout_finish.sql;
source /home/code/ibbd/yanjiuquan-php/sql/proc_money_reward_income.sql;

说明:这个source文件不应该加入git版本控制中。

这样只需要在命令行中,source这个文件即可:

1
source /path/to/source.sql

第三步:自动生成source.sql

如果手动管理这个文件,也是挺麻烦的,可以结合laravel自动生成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use Illuminate\Database\Seeder;
class ProcdureInitTableSeeder extends Seeder {
public function run()
{
$path = 'sql/proc_*.sql';
$root = getcwd();
$files = glob($path);
$source_file = $root . '/sql/source.sql';
file_put_contents($source_file, "# @desc 导入及更新存储过程结构, 生成命令:php artisan db:seed\n");
file_put_contents($source_file, "# @author Alex\n", FILE_APPEND);
file_put_contents($source_file, "# @date " . date('Y-m-d') . "\n\n", FILE_APPEND);
foreach ($files as $file) {
$sql = "source {$root}/{$file};\n";
file_put_contents($source_file, $sql, FILE_APPEND);
}
}
}

The last

每次变化的时候,还是需要手动source一下,略麻烦。。。暂时没有更好的方式。。。

laravel项目中的数据表设计规范及使用方式

字段设计规范

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 主键
$table->increments('id');
// 加入 created_at 和 updated_at 字段
$table->timestamps();
// 加入 deleted_at 字段于软删除使用
$table->softDeletes();
// 加入整数 taggable_id 与字串 taggable_type
$table->morphs('taggable');
// 加入 remember_token 使用 VARCHAR(100) NULL
$table->rememberToken();

软删除

通过软删除方式删除了一个模型后,模型中的数据并不是真的从数据库被移除。而是会设定 deleted_at时间戳。要让模型使用软删除功能,只要在模型类里加入 SoftDeletingTrait 即可:

1
2
3
4
5
6
7
8
9
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model {
use SoftDeletes;
protected $dates = ['deleted_at'];
}

现在当您使用模型调用 delete 方法时, deleted_at字段会被更新成现在的时间戳。在查询使用软删除功能的模型时,被「删除」的模型数据不会出现在查询结果里。

1
2
3
4
5
// 强制查询软删除数据(使用 withTrashed 方法)
$users = User::withTrashed()->where('account_id', 1)->get();
// 只想查询被软删除的模型数据,可以使用 onlyTrashed 方法
$users = User::onlyTrashed()->where('account_id', 1)->get();

时间戳

默认 Eloquent 会自动维护数据库表的 created_at 和 updated_at 字段。只要把这两个「时间戳」字段加到数据库表, Eloquent 就会处理剩下的工作。

范围查询

定义范围查询

范围查询可以让您轻松的重复利用模型的查询逻辑。要设定范围查询,只要定义有 scope 前缀的模型方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
class User extends Model {
public function scopePopular($query)
{
return $query->where('votes', '>', 100);
}
public function scopeWomen($query)
{
return $query->whereGender('W');
}
}

使用范围查询

1
$users = User::popular()->women()->orderBy('created_at')->get();

动态范围查询

有时您可能想要定义可接受参数的范围查询方法。只要把参数加到方法里:

1
2
3
4
5
6
7
class User extends Model {
public function scopeOfType($query, $type)
{
return $query->whereType($type);
}
}

使用:

1
$users = User::ofType('member')->get();

使用枢纽表

如您所知,要操作多对多关联需要一个中间的数据库表。 Eloquent 提供了一些有用的方法可以和这张表互动。例如,假设 User 对象关联到很多 Role 对象。取出这些关联对象时,我们可以在关联模型上取得 pivot 数据库表的数据:

1
2
3
4
5
6
$user = User::find(1);
foreach ($user->roles as $role)
{
echo $role->pivot->created_at;
}

注意我们取出的每个 Role 模型对象会自动给一个 pivot 属性。这属性包含了枢纽表的模型数据,可以像一般的 Eloquent 模型一样使用。

默认 pivot 对象只会有关联键的属性。如果您想让 pivot 可以包含其他枢纽表的字段,可以在定义关联方法时指定那些字段:

1
return $this->belongsToMany('App\Role')->withPivot('foo', 'bar');

其他操作:

1
2
3
4
5
// 删除枢纽表的关联数据
User::find(1)->roles()->detach();
// 更新枢纽表的数据
User::find(1)->roles()->updateExistingPivot($roleId, $attributes);

集合

所有 Eloquent 查询返回的数据,如果结果多于一条,不管是经由 get 方法或是 relationship,都会转换成集合对象返回。这个对象实现了 IteratorAggregate PHP 接口,所以可以像数组一般进行遍历。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 确认集合中里是否包含特定键值
$roles = User::find(1)->roles;
if ($roles->contains(2))
{
//
}
// 集合遍历
$roles = $user->roles->each(function($role)
{
//
});
// 集合过滤
$users = $users->filter(function($user)
{
return $user->isAdmin();
});

待续。。。