yii框架模型操做

命令行自動生成model模型類

php yii gii/model --ns=app\\modules\\v1\\models --tableName=SCM_tbInvBalance_new --modelClass=SCMTbInvBalanceNewphp


1、relation使用方法 ``` CategoryController: /** * @ hasMany 關係 */ public function actionGetproduct(){ $modelCategory = new Category(); $category = $modelCategory::find()->where(['cid'=>1])->one(); $product = $category->getProduct()->andWhere('status=1')->all(); var_dump($product); } Category(Model): public function getProduct(){ return $this->hasMany(Product::className(), array('cid'=>'cid')); } ``` 二、Yii2.0 數據庫查詢 ``` User::find()->all(); 此方法返回全部數據; User::findOne($id); 此方法返回 主鍵 id=1 的一條數據(舉個例子); User::find()->where(['name' => '小夥兒'])->one(); 此方法返回 ['name' => '小夥兒'] 的一條數據; User::find()->where(['name' => '小夥兒'])->all(); 此方法返回 ['name' => '小夥兒'] 的全部數據; User::find()->orderBy('id DESC')->all(); 此方法是排序查詢; User::findBySql('SELECT * FROM user')->all(); 此方法是用 sql 語句查詢 user 表裏面的全部數據; User::findBySql('SELECT * FROM user')->one(); 此方法是用 sql 語句查詢 user 表裏面的一條數據; User::find()->andWhere(['sex' => '男', 'age' => '24'])->count('id'); 統計符合條件的總條數; User::find()->andFilterWhere(['like', 'name', '小夥兒']); 此方法是用 like 查詢 name 等於 小夥兒的 數據 User::find()->one(); 此方法返回一條數據; User::find()->all(); 此方法返回全部數據; User::find()->count(); 此方法返回記錄的數量; User::find()->average(); 此方法返回指定列的平均值; User::find()->min(); 此方法返回指定列的最小值 ; User::find()->max(); 此方法返回指定列的最大值 ; User::find()->scalar(); 此方法返回值的第一行第一列的查詢結果; User::find()->column(); 此方法返回查詢結果中的第一列的值; User::find()->exists(); 此方法返回一個值指示是否包含查詢結果的數據行; User::find()->batch(10); 每次取 10 條數據 User::find()->each(10); 每次取 10 條數據, 迭代查詢
$userModel = 'app\modules\v1\models\Tbuser';
$user = $userModel::find('fsShopGUID', 'fsUserId')->where(['fsUserId' => $userName, 'fsShopGUID' => $fsShopGUID])->asArray()->one()//查詢獲取一條記錄
$userAll = $userModel::find('fsShopGUID', 'fsUserId')->where(['fsUserId' => $userName, 'fsShopGUID' => $fsShopGUID])->asArray()->all()//查詢獲取全部知足條件的記錄
 
 

 

新增 
$connection->createCommand()->insert('SCM_tbParamValue', [ 'fsParamId' => $account_code, 'fsParamValue' => $set_status, 'fiStatus' => 1, 'fsCreateUserId' => $this->user['fsUserId'], 'fsCreateTime' => date('Y-m-d H:i:s'), 'fsCreateUserName' => $this->user['fsUserName'], 'fsShopGUID' => $this->user['fsShopGUID'],])->execute();
批量新增
$connection->createCommand()->batchInsert('SCM_tbInvBalance', ['fsYYMM', "fsStorageId", 'fsMaterialId', "fsMaterialName", "fsModelno", "fsUnitId", "fiStatus", "fdBeginQty", "fdBeginBal","fdEndQty", "fdEndBal", "fsCreateTime", "fsCreateUserId", "fsCreateUserName", "fsShopGUID","fdBeginCost","fdEndCost"], $InvBalance )->execute();
更新
$modify = [ 'fsParamValue' => $set_status, 'fsUpdateTime' => date('Y-m-d H:i:s'), 'fsUpdateUserId' => $this->user['fsUserId'], 'fsUpdateUserName' => $this->user['fsUserName'] ];
$connection->createCommand()->update('SCM_tbParamValue', $modify, '"fsParamId"=:fsParamId and "fsShopGUID"=:fsShopGUID', [':fsParamId' => $query['fsParamId'], ':fsShopGUID' => $this->user['fsShopGUID']])->execute();
刪除
$connection->createCommand()->delete('SCM_tbInvBalance', '"fsShopGUID" = \'' . $this->user['fsShopGUID'] . '\'')->execute();
相關文章
相關標籤/搜索