yii2 Несколько моделей в одной таблице/коллекции -- шаблон проектирования
Primary tabs
Можно использовать подход от Александра Макарова: https://github.com/samdark/yii2-cookbook/blob/master/book/ar-single-table-inheritance.md
Спасибо за ссылку Н.Гр.
UpdateAll() DeleteAll() для mongodb
Эти методы можно определить в базовом классе так:
public static function deleteAll($condition = [], $options = [])
{
switch (static::TYPE) {
case 'base':
$cond = $condition;
break;
default:
$cond = self::andCondFunc([$condition, ['type' => static::TYPE]]);
}
return parent::deleteAll($cond, $options);
}
public static function updateAll($attributes, $condition = array(), $options = array())
{
switch (static::TYPE) {
case 'base':
$cond = $condition;
break;
default:
$cond = self::andCondFunc([$condition, ['type' => static::TYPE]]);
}
parent::updateAll($attributes, $cond, $options);
}
Используя такой трейт:
/**
* Present logical operations for array conditions used in mongo pipes and queries.
*
*/
trait LogicOps
{
/**
* Logical AND for pipe conditions
* Empty Array corresponds to
*
* @param array $conditions
* @return type
*/
public static function andCondFunc(Array $conditions)
{
return self::condFunc($conditions, 'and');
}
/**
* Logical OR for pipe conditions.
* But it excludes empty conditions.
*
* @param array $conditions
* @return type
*/
public static function orCondFunc(Array $conditions)
{
return self::condFunc($conditions, 'or');
}
public static function condFunc(Array $conditions, $function)
{
$conjuncts = [];
foreach ($conditions as $condition) {
if (is_array($condition) && !empty($condition)) {
$conjuncts[] = $condition;
}
}
switch (count($conjuncts)) {
case 0:
return [];
case 1:
return $conjuncts[0];
default:
return ['$'.$function => $conjuncts];
}
}
}
- Log in to post comments
- 3232 reads