Yii Framework2.0開發教程(6)資料庫mysql--ActiveRecord,yiiactiverecord

來源:互聯網
上載者:User

Yii Framework2.0開發教程(6)資料庫mysql--ActiveRecord,yiiactiverecord

Active Record(活動記錄,以下簡稱AR)提供了一個物件導向的介面,用以訪問資料庫中的資料。

一個AR 類關聯一張資料表,每個 AR 對象對應表中的一行,對象的屬性(即 AR 的特性Attribute)映射到資料行的對應列。

 一條活動記錄(AR對象)對應資料表的一行,AR對象的屬性則映射該行的相應列。 

您可以直接以物件導向的方式來操縱資料表中的資料,媽媽再不用擔心我需要寫原生 SQL 陳述式啦。

編輯models/Country.php

<?php namespace app\models; use yii\db\ActiveRecord;//你不用在裡面寫任何代碼。只需要像現在這樣,Yii 就能根據類名去猜測對應的資料表名。class Country extends ActiveRecord{}

或者

<?php namespace app\models; use yii\db\ActiveRecord;//你不用在裡面寫任何代碼。只需要像現在這樣,Yii 就能根據類名去猜測對應的資料表名。class Country extends ActiveRecord{//@return string 返回該AR類關聯的資料表名public static function tableName(){return 'country';}}

編輯views/zhyoulun/helloworld.php

<?php$country = new \app\models\Country;$country->code = 'ZZ';$country->name = 'Youlun';$country->population = '1';$country->save();?>

訪問網頁http://localhost/basic/web/index.php?r=zhyoulun/helloworld,可以看到資料庫中多了一條記錄


查詢資料

// 擷取 country 表的所有行並以 name 排序$countries = Country::find()->orderBy('name')->all();echo '<table border="1">';for($i=0;$i<count($countries);$i++){echo '<tr>';echo '<td>'.$countries[$i]['code'].'</td>';echo '<td>'.$countries[$i]['name'].'</td>';echo '<td>'.$countries[$i]['population'].'</td>';echo '</tr>';}echo '</table>';

// 用原生 SQL 陳述式檢索客戶:$sql = 'SELECT * FROM customer';$customers = Customer::findBySql($sql)->all();

以數組形式擷取資料

// 以數組而不是對象形式取回客戶資訊:$customers = Customer::find()    ->asArray()    ->all();// $customers 的每個元素都是索引值對數組


批量擷取資料

// 一次提取 10 個客戶資訊foreach (Customer::find()->batch(10) as $customers) {    // $customers 是 10 個或更少的客戶對象的數組}// 一次提取 10 個客戶並一個一個地遍曆處理foreach (Customer::find()->each(10) as $customer) {    // $customer 是一個 ”Customer“ 對象}// 貪婪載入模式的批處理查詢foreach (Customer::find()->with('orders')->each() as $customer) {}


操作資料(AR 提供以下方法插入、更新和刪除與 AR 對象關聯的那張表中的某一行)

[[yii\db\ActiveRecord::save()|save()]]
[[yii\db\ActiveRecord::insert()|insert()]]
[[yii\db\ActiveRecord::update()|update()]]
[[yii\db\ActiveRecord::delete()|delete()]]

// 插入新客戶的記錄$customer = new Customer();$customer->name = '詹姆斯';$customer->email = '007@example.com';$customer->save();  // 等同於 $customer->insert(); // 更新現有客戶記錄$customer = Customer::find($id);$customer->email = '邦德@demo.com';$customer->save();  // 等同於 $customer->update(); // 刪除已有客戶記錄$customer = Customer::find($id);$customer->delete();

AR 同時提供了一下靜態方法,可以應用在與某 AR 類所關聯的整張表上。 用這些方法的時候千萬要小心,因為他們作用於整張表! 比如,deleteAll() 會刪除掉表裡所有的記錄。

[[yii\db\ActiveRecord::updateCounters()|updateCounters()]]
[[yii\db\ActiveRecord::updateAll()|updateAll()]]
[[yii\db\ActiveRecord::updateAllCounters()|updateAllCounters()]]
[[yii\db\ActiveRecord::deleteAll()|deleteAll()]]


參考:

http://www.yiichina.com/guide/2/db-active-record


轉載請註明出處:http://blog.csdn.net/zhyoulun/article/details/40476471


yii framework運作流程的一個問題教 - PHP架構開發

這個機制從簡單的講無外乎就兩點:1、DRY2、autoload
 
剛學習Yii Framework,問什地方有視頻教程?大家給推薦一下

現在關於Yii Framework的視頻還是比較少的。但是也不是沒有,推薦個給你
www.yiichina.org/forum/forum-9-1.html
這個網站還是挺好的 視頻雖然才有兩期但是一直在不斷更新,內容也挺豐富的~

呵呵~希望我的回答對你有所協助!
參考資料:www.yiichina.org/
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.