【发布时间】:2015-03-29 16:53:56
【问题描述】:
我有 3 张桌子:
建筑物:
+-----+-------------+-----------+--------------------------+------------+---------------+----------------------+---------------------+
| id | order_flag | type | key | unit_cost | unit_produce | created_at | updated_at |
+-----+-------------+-----------+--------------------------+------------+---------------+----------------------+---------------------+
| 1 | 10 | resource | building1 | 10 | 0 | 2015-01-30 08:54:44 | 2015-01-30 08:54:44 |
| 2 | 20 | resource | building2 | 5 | 0 | 2015-01-30 08:54:44 | 2015-01-30 08:54:44 |
| 3 | 30 | resource | building3 | 10 | 0 | 2015-01-30 08:54:44 | 2015-01-30 08:54:44 |
| 4 | 10 | basic | building4 | 0 | 25 | 2015-01-30 08:54:44 | 2015-01-30 08:54:44 |
+-----+-------------+-----------+--------------------------+------------+---------------+----------------------+---------------------+
建筑用户:
+-----+--------------+----------+--------+----------------------+---------------------+
| id | building_id | user_id | level | created_at | updated_at |
+-----+--------------+----------+--------+----------------------+---------------------+
| 1 | 2 | 12 | 3 | 2015-01-30 08:52:57 | 2015-01-30 08:55:37 |
| 2 | 4 | 12 | 1 | 2015-01-30 08:53:53 | 2015-01-30 08:53:53 |
| 3 | 1 | 12 | 2 | 2015-01-30 08:54:08 | 2015-01-30 08:55:10 |
+-----+--------------+----------+--------+----------------------+---------------------+
建筑要求:
+-----+--------------+-------------+--------+----------------------+---------------------+
| id | building_id | require_id | level | created_at | updated_at |
+-----+--------------+-------------+--------+----------------------+---------------------+
| 1 | 1 | 4 | 1 | 2015-01-30 08:54:44 | 2015-01-30 08:54:44 |
| 2 | 3 | 1 | 5 | 2015-01-30 08:54:44 | 2015-01-30 08:54:44 |
+-----+--------------+-------------+--------+----------------------+---------------------+
我在建筑模型中写下这个关系:
# Building Require another Building(s)
public function requires() {
return $this->belongsToMany('Building', 'building_require', 'building_id', 'require_id')->withPivot(array(
'level',
'updated_at',
'created_at'
));
}
public function users() {
return $this->belongsToMany('User')->withPivot(array('level'));
}
现在我想从桌子建筑物中选择所有建筑物...... 以下限制:
如果 building_require 表中没有来自该建筑物的条目,则该建筑物可以在结果中。
-
如果该建筑物在 building_require 表中有条目,则必须检查 building_user 表中是否有条目,其中 building_id 与 building_required 表中的 required_id 相同。
(例如 building_id 1 需要 building_id 4,而 building_id 3 需要 building_id 1 的条目。)
除此之外(这是困难的部分),必须检查 building_user 表中的条目是否具有 level 值 >= 作为 building_require 表中定义的级别。
总结一下:
在 这种情况 building_id 1 需要在 building_user 中的条目 building_id 为 4 并且级别至少为 1。 building_id 3 需要一个条目 building_user 其中 building_id 为 1 且级别至少为 5。因此 - building_id 1 和 3 具有所需的建筑物。建筑与 id 2 和 4 在结果中没有限制。 在这种情况下,只有 building_id 3 是不允许的。因为 building_user 表中 building_id 1 的级别只有 2 而不是至少 5。
对我来说这很困难,我认为这不是最好的解决方案,遗憾的是没有检查级别值:
# all buildings there has a required building:
$allRequiredBuildingIds = DB::table('building_require')->lists('building_id');
# if user has buildings, this buildings must remove from $allRequiredBuildingIds
if(Sentry::getUser()->buildings()->count() > 0) {
# list all building_id 's of the user_building table
$userBuildingIds = Sentry::getUser()->buildings()->lists('building_id');
# list all building_id's of the buildings there are required and already stored in the building_user table
$userBuildingRequiredIds = DB::table('building_require')->whereIn('require_id', $userBuildingIds)->lists('building_id');
# there are all the building_id's which are not allowed to display
$allRequiredBuildingIds = array_values(array_diff($allRequiredBuildingIds,$userBuildingRequiredIds));
}
# if there are buildings there are not allowed to display?
if(count($allRequiredBuildingIds) > 0) {
$buildings = Building::whereType($type)->whereNotIn('id', $allRequiredBuildingIds)->paginate(10);
}else{
$buildings = Building::whereType($type)->paginate(10);
}
有人可以帮我优化它并集成级别检查吗? 我的头在怦怦直跳。
【问题讨论】:
标签: php mysql laravel model relationship