-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBeforeQueryTrait.php
34 lines (30 loc) · 1.09 KB
/
BeforeQueryTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
namespace sibds\components;
trait BeforeQueryTrait
{
public static function find()
{
/**
* @var $obj ActiveRecord
*/
$obj = new static;
$class = new \ReflectionClass($obj);
$condition = [];
foreach ($class->getProperties(\ReflectionProperty::IS_STATIC) as $property) {
if (strpos($property->getName(), 'BEFORE_QUERY') !== false && is_array($property->getValue($obj))) {
$params = $property->getValue($obj);
foreach($params as $key => $value){
$params[$obj::tableName().'.'.$key] = $value;
unset($params[$key]);
}
$condition = array_merge($condition, $params);
}
}
if ($obj->hasAttribute($obj->removedAttribute))
return (new DynamicQuery($obj))->findRemoved()->andFilterWhere($condition);
elseif ($obj->isNestedSet())
return (new DynamicQuery($obj))->andFilterWhere($condition);
else
return parent::find()->andFilterWhere($condition);
}
}