gii CLI
php yii help gii/mode
php yii gii/model --generateLabelsFromComments=1 --overwrite=1 --standardizeCapitals=1 --ns='app\models\gii' --tableName="*"
php yii gii/model --generateLabelsFromComments=1 --overwrite=1 --standardizeCapitals=1 --db="hub_db" --ns='app\models\hub\gii' --tableName="*"
|
连接数据库时设置时区
'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=mysql;port=3306;dbname=hub', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8',
'enableLogging' => YII_DEBUG ? true : false, 'enableProfiling' => YII_DEBUG ? true : false,
'on afterOpen' => static function ($event) { $event->sender->createCommand("SET time_zone='+08:00';")->execute(); },
|
ActiveRecord one
yii\db\ActiveRecord::findOne()
和 yii\db\ActiveQuery::one()
都不会添加 LIMIT 1 到 生成的 SQL 语句中。如果你的查询会返回很多行的数据, 你明确的应该加上 limit(1)
来提高性能,比如 Customer::find()->limit(1)->one()
。
DB where
- 字符串格式,例如:
'status=1'
- 哈希格式,例如:
['status' => 1, 'type' => 2]
- 操作符格式,例如:
['like', 'name', 'test']
- 对象格式,例如:
new LikeCondition('name', 'LIKE', 'test')
简单条件
$cond = ['type' => 1, 'status' => 2]
$cond = ['id' => [1, 2, 3], 'status' => 2]
$cond = ['status' => null]
|
AND OR
$cond = ['and', 'id=1', 'id=2']
$cond = ['and', 'type=1', ['or', 'id=1', 'id=2']]
$cond = [ 'and', ['=', 'type', 1], [ 'or', ['=', 'id', '1'], ['=', 'id', '2'], ] ]
|
NOT
$cond = ['not', ['attribute' => null]]
|
BETWEEN
$cond = ['between', 'id', 1, 10]
|
IN
$cond = ['between', 'id', 1, 10] $cond = ['id' => [1, 2, 3]]
$cond = ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]]
$cond = ['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]
|
LIKE
$cond = ['like', 'name', 'tester']
$cond = ['like', 'name', ['test', 'sample']]
$cond = ['like', 'name', '%tester', false]
|
EXIST
$cond = ['exists', (new Query())->select('id')->from('users')->where(['active' => 1])]
|
References
– EOF –