yii mysql语法_yii2 + mysql 常用增删改查操作语法以及事务
關(guān)于數(shù)據(jù)庫(kù)mysql的使用: 1.查詢(xún): Salesorderitem::find()-asArray()-where([order_id=$order_id])-all();Salesorderitem::find()-asArray()-where([order_id=$order_id])-one();Quote::findOne([customer_id = $customer_id]); 2.插入: $order = new S
關(guān)于數(shù)據(jù)庫(kù)mysql的使用:
1.查詢(xún):
Salesorderitem::find()->asArray()->where(['order_id'=>$order_id])->all();
Salesorderitem::find()->asArray()->where(['order_id'=>$order_id])->one();
Quote::findOne(['customer_id' => $customer_id]);
2.插入:
$order = new Salesorder();
$order->order_status = $order_status;
$order->store = $store;
$order->save();
$order_id = Yii::$app->db->getLastInsertID();
$db->createCommand('INSERT INTO customer (name) VALUES (:name)', [ ':name' => 'Qiang',])->execute();
3更新:
3.1
Yii::$app->db->createCommand()->update(self::QUOTE_ITEM, [
'qty' => $qty,
'row_weight'=>$row_weight,
'base_row_total'=>$base_row_total,
'row_total'=>$row_total,
],
'quote_id='.self::$_quote_id.' and item_id='.$item_id )
->execute();
3.2//?to?update?an?existing?customer?record
$customer = Customer::findOne($id);
$customer->email = 'james@example.com';
$customer->save();
//?equivalent?to?$custmer->update();
4.?刪除:
Quoteitem::deleteAll('quote_id='.self::$_quote_id.' and item_id='.$item_id);
注:使用mysql的時(shí)候一定要注意sql注入攻擊的屏蔽
# 開(kāi)始事務(wù)
$innerTransaction = Yii::$app->db->beginTransaction();
try {
# 保存quoteitems
self::removeQuoteItems($data['item_id']);
# 獲取quoteitems ,保存到 self::$_quote_items
self::getQuoteItems();
# 保存 quote
self::saveQuote();
$innerTransaction->commit();
} catch (Exception $e) {
$innerTransaction->rollBack();
}
總結(jié)
以上是生活随笔為你收集整理的yii mysql语法_yii2 + mysql 常用增删改查操作语法以及事务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 23年吉林省军队文职拟录取人员公示了吗
- 下一篇: 广汉飞行学院空管可以考公务员吗