phpshko/ yii2-magic-scopes
Yii2 查询魔法作用域的行为
0.1.0
2015-03-30 10:43 UTC
Requires
- yiisoft/yii2: *
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-13 06:01:59 UTC
README
此行为可以帮助您避免为模型创建 QueryClass,如果只需要简单的作用域。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令:
php composer.phar require --prefer-dist phpshko/yii2-magic-scopes "dev-master"
或者将以下内容添加到您的 composer.json
文件的 require 部分:
"phpshko/yii2-magic-scopes": "dev-master"
In Gii
您可以使用 "Magic Scopes Generator" 生成一个模型。这将自动生成需要 PHPDoc 并覆盖 find() 方法。在此生成器中,您可以选择 "创建模式" 和 "保存 DocBlock 到"。
创建模式
-
使用魔法查询
* @method ActiveQuery|UserWithMagic orAddressIdNotLike($addressId) * @method ActiveQuery|UserWithMagic orAddressIdNotBetween($from, $to) * @method ActiveQuery|UserWithMagic orAddressIdNotMore($than, $include = false) * @method ActiveQuery|UserWithMagic orAddressIdNotLess($than, $include = false) * @method ActiveQuery|UserWithMagic orAddressId($addressId) * */ class UserWithMagic extends \yii\db\ActiveRecord { /** * @inheritdoc * @return MagicActiveQuery|UserWithMagic */ public static function find() { return new MagicActiveQuery(get_called_class()); }
-
创建查询
/** * This is the model class for table "user". * * @property integer $id * @property string $username * @property string $forename * @property string $surname * @property integer $year * @property integer $address_id */ class UserWithCreate extends \yii\db\ActiveRecord { /** * @inheritdoc * @return UserWithCreateQuery|UserWithCreate */ public static function find() { return new UserWithCreateQuery(get_called_class()); }
并自动创建查询类,附加行为,并放置 PhpDoc
... * @method UserWithCreateQuery|UserWithCreate orAddressIdNot($addressId) * @method UserWithCreateQuery|UserWithCreate orAddressIdNotIn($array) * @method UserWithCreateQuery|UserWithCreate orAddressIdNotLike($addressId) * @method UserWithCreateQuery|UserWithCreate orAddressIdNotBetween($from, $to) * @method UserWithCreateQuery|UserWithCreate orAddressIdNotMore($than, $include = false) * @method UserWithCreateQuery|UserWithCreate orAddressIdNotLess($than, $include = false) * @method UserWithCreateQuery|UserWithCreate orAddressId($addressId) * */ class UserWithCreateQuery extends \yii\db\ActiveQuery { /** * @inheritdoc */ public function behaviors() { return [ [ 'class' => \phpshko\magicscopes\MagicScopesBehavior::className() ] ]; } }
-
附加行为
... * @method ActiveQuery|UserWithAttach orAddressIdNotIn($array) * @method ActiveQuery|UserWithAttach orAddressIdNotLike($addressId) * @method ActiveQuery|UserWithAttach orAddressIdNotBetween($from, $to) * @method ActiveQuery|UserWithAttach orAddressIdNotMore($than, $include = false) * @method ActiveQuery|UserWithAttach orAddressIdNotLess($than, $include = false) * @method ActiveQuery|UserWithAttach orAddressId($addressId) * */ class UserWithAttach extends \yii\db\ActiveRecord { /** * @inheritdoc * @return ActiveQuery|UserWithAttach */ public static function find() { $query = parent::find(); $query->attachBehavior('MagicScopesBehavior', MagicScopesBehavior::className()); return $query; }
如果您选择 "保存 DocBlock 到 MagicAutoComplete.php",则此模型的所有 PhpDoc 将与文件 MagicAutoComplete.php 中的旧数据合并(在模型相同的文件夹中)
您可以在这里看到不同设置创建的模型示例。
示例
$queryWith = UserWith::find() ->andId(45) ->andUsernameLike('php') ->andYearBetween(1980, 2000) ->addressIdMore(10); $queryWithout = UserWithout::find() ->andWhere(['id' => 45]) ->andWhere(['like', 'username', 'php']) ->andWhere(['between', 'year', 1980, 2000]) ->andWhere(['>', 'address_id', 10]);
更多示例可以在此测试中找到
您不能写 "and"
UserWith::find()->id(5) == UserWith::find()->andId(5)
允许的方法完整列表(例如 id)
->id($id) ->idIn($array) ->idLike($id) ->idBetween($from, $to) ->idMore($than, $include = false) ->idLess($than, $include = false) ->idNot($id) ->idNotIn($array) ->idNotLike($id) ->idNotBetween($from, $to) ->idNotMore($than, $include = false) ->idNotLess($than, $include = false) ->andId($id) ->andIdIn($array) ->andIdLike($id) ->andIdBetween($from, $to) ->andIdMore($than, $include = false) ->andIdLess($than, $include = false) ->andIdNot($id) ->andIdNotIn($array) ->andIdNotLike($id) ->andIdNotBetween($from, $to) ->andIdNotMore($than, $include = false) ->andIdNotLess($than, $include = false) ->orIdIn($array) ->orIdLike($id) ->orIdBetween($from, $to) ->orIdMore($than, $include = false) ->orIdLess($than, $include = false) ->orIdNot($id) ->orIdNotIn($array) ->orIdNotLike($id) ->orIdNotBetween($from, $to) ->orIdNotMore($than, $include = false) ->orIdNotLess($than, $include = false) ->orId($id)