CakePHP 2.x CookBook 中文版 第七章 模型
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-09-17 09:11:56 閱讀次數(shù):4228次
模型
模型是應(yīng)用程序中業(yè)務(wù)層的類。
這意味著它們負(fù)責(zé)管理工作域中幾乎所有的與數(shù)據(jù)有關(guān)的東西:數(shù)據(jù)校驗(yàn)、交互和信息流演化。
通常模型類代理數(shù)據(jù),用于 CakePHP 應(yīng)用程序的數(shù)據(jù)訪問(wèn),多數(shù)時(shí)候它們代理數(shù)據(jù)庫(kù)表,但并不限于此,它也可以訪問(wèn)操縱數(shù)據(jù)的任何事物,如文件、外部 web service、iCal 事件或者一個(gè) CSV 文件的行。
一個(gè)模型可以與其它模型相關(guān)聯(lián)。例如,一個(gè) Recipe 能夠與食譜的 Author 及 食譜的 Ingredient 相關(guān)聯(lián)。
這一節(jié)將說(shuō)明模型的哪些特性可以是自動(dòng)化的,如何覆蓋這些特性,以及模型都有哪些方法和屬性。還說(shuō)明了關(guān)聯(lián)數(shù)據(jù)的不同方法。描述了如果查找、保存和刪除數(shù)據(jù)。最后,講解了數(shù)據(jù)源。
理解模型
模型描述了你的數(shù)據(jù)模型。在面向?qū)ο缶幊讨?,一個(gè)數(shù)據(jù)模型是描述一件事的對(duì)象,比如一輛汽車、一個(gè)人、一幢房子。舉例來(lái)說(shuō),一個(gè)博客可能有很多博客文章,每篇文章可能有很多評(píng)論。博客、文章和評(píng)論是全部示例模型,每一個(gè)都與其它的相關(guān)聯(lián)。
下面是在 CakePHP 中定義的模型的簡(jiǎn)單示例:
1 class Ingredient extends AppModel {
2 public $name = 'Ingredient';
3 }
只需這樣一個(gè)簡(jiǎn)單的聲明,Ingredient 模型就被賦予了建立保存和刪除數(shù)據(jù)的查詢所需的全部功能。這些魔術(shù)方法繼承自 CakePHP 的 Model 類。Ingredient 模型繼承了應(yīng)用程序模型 AppModel(一個(gè)擴(kuò)展自內(nèi)部 Model 類的模型類)。它是向 Ingredient 模型賦予功能的核心模型類。
介于內(nèi)部 Model 類和最終的模型類之間的 AppModel 類,在你創(chuàng)建專屬于自己的 AppModel 類之前是空的,它位于 CakePHP 內(nèi)核文件夾。覆寫(xiě) AppModel 類允許你定義對(duì)應(yīng)用程序中所有的模型類都可用的功能。要做到這一點(diǎn),只需要在 Model 文件夾中建立你自己的 AppModel.php ,和應(yīng)用程序中的其它模型類放在一起。使用 Bake 建立項(xiàng)目時(shí),會(huì)自動(dòng)為你生成這個(gè)文件。
關(guān)于如何向多個(gè)模型添加相同的邏輯的更多信息,請(qǐng)參見(jiàn) Behaviors 。
回到我們的 Ingredient 模型,為了在其上工作,需要在 /app/Model 文件夾創(chuàng)建 PHP 文件。按照約定它的名字應(yīng)該與類相同,對(duì)于本例,就是 Ingredient.php 。
注解
如果 CakePHP 沒(méi)有在 /app/Model 文件夾中找到符合條件的文件,它將動(dòng)態(tài)創(chuàng)建一個(gè)模型對(duì)象。這意味關(guān)如果你的模型文件命名錯(cuò)誤(例如 ingredient.php 或者 Ingredients.php),CakePHP 將使用出乎你意料的 AppModel 的實(shí)例(按照 CakePHP 自己的思維方式)。如果你嘗試使用你已經(jīng)定義在你的模型聽(tīng)方法,或者附加到你的模型上的行為,你會(huì)收到一個(gè)以你調(diào)用的方法名標(biāo)識(shí)的 SQL 錯(cuò)誤信息 - 它是 CakePHP 找不到你的模型的明確提示,你需要檢查文件名、應(yīng)用程序緩存,或者兩者都檢查一下。
注解
一些類名不能用于模型名。舉例來(lái)說(shuō),’‘File’’ 不能使用,因?yàn)?‘’File’’ 是 CakePHP 內(nèi)核中已經(jīng)存在的類。
定義過(guò)的模型就可以在 控制器 中訪問(wèn)了。CakePHP 會(huì)自動(dòng)使與當(dāng)前控制器名字相同的模型可用。例如,如果一個(gè)控制器的名字是 IngredientsController,將自動(dòng)初始化 Ingredient 模型并將其賦予此控制器的 $this->Ingredient 變量:
1 class IngredientsController extends AppController {
2 public function index() {
3 //抓取所有的 ingredients 并將其傳遞給視圖:
4 $ingredients = $this->Ingredient->find('all');
5 $this->set('ingredients', $ingredients);
6 }
7 }
關(guān)聯(lián)模型通過(guò)主模型訪問(wèn)。在上面的例子中,Recipe 與 Ingredient 模型關(guān)聯(lián):
1 class Recipe extends AppModel {
2
3 public function steakRecipes() {
4 $ingredient = $this->Ingredient->findByName('Steak');
5 return $this->findAllByMainIngredient($ingredient['Ingredient']['id']);
6 }
7 }
上面的代碼顯示了如何使用已經(jīng)連接的模型。想了解如何定義關(guān)聯(lián)請(qǐng)移步至 Associations section。
關(guān)于模型的更多...
- 關(guān)聯(lián):將模型連接在一起
- 關(guān)系類型
- hasOne
- belongsTo
- hasMany
- counterCache - 緩存你的 count()
- hasAndBelongsToMany (HABTM)
- hasMany 貫穿 (連接模型)
- 在運(yùn)行期間創(chuàng)建和銷毀關(guān)聯(lián)
- 同一模型上的多個(gè)關(guān)系
- 連接表
- 檢索數(shù)據(jù)
- find
- find(‘first’)
- find(‘count’)
- find(‘a(chǎn)ll’)
- find(‘list’)
- find(‘threaded’)
- find(‘neighbors’)
- 創(chuàng)建自定義 find 類型
- 魔術(shù)查找類型
- Model::query()
- Model::field()
- Model::read()
- 復(fù)雜的查找條件
- 保存數(shù)據(jù)
- Model::set($one, $two = null)
- Model::save(array $data = null, boolean $validate = true, array $fieldList = array())
- Model::create(array $data = array())
- Model::saveField(string $fieldName, string $fieldValue, $validate = false)
- Model::updateAll(array $fields, array $conditions)
- Model::saveMany(array $data = null, array $options = array())
- Model::saveAssociated(array $data = null, array $options = array())
- Model::saveAll(array $data = null, array $options = array())
- 保存相關(guān)模型的數(shù)據(jù)(hasOne, hasMany, belongsTo)
- 通過(guò)數(shù)據(jù)保存 hasMany
- 保存相關(guān)模型數(shù)據(jù) (HABTM)
- 當(dāng) HABTM 變得復(fù)雜時(shí)怎么辦?
- 數(shù)據(jù)表
- 刪除數(shù)據(jù)
- 數(shù)據(jù)校驗(yàn)
- Simple Rules
- One Rule Per Field
- rule
- required
- allowEmpty
- on
- message
- Multiple Rules per Field
- Custom Validation Rules
- Custom Regular Expression Validation
- Adding your own Validation Methods
- Dynamically change validation rules
- Adding new validation rules
- Modifying current validation rules
- Removing rules from the set
- Core Validation Rules
- Localized Validation
- Validating Data from the Controller
- 回調(diào)方法
- beforeFind
- afterFind
- beforeValidate
- beforeSave
- afterSave
- beforeDelete
- afterDelete
- onError
- 行為
- Using Behaviors
- Creating Behaviors
- Creating behavior methods
- Behavior callbacks
- Creating a behavior callback
- 數(shù)據(jù)源
- Basic API For DataSources
- An Example
- Plugin DataSources
- 模型屬性
- useDbConfig
- useTable
- tablePrefix
- 主鍵
- displayField
- recursive
- order
- data
- _schema
- validate
- virtualFields
- name
- cacheQueries
- 附加的方法和附屬
- Model::associations()
- Model::buildQuery(string $type = 'first', array $query = array())
- Model::deconstruct(string $field, mixed $data)
- Model::escapeField(string $field = null, string $alias = null)
- Model::exists($id)
- Model::getAffectedRows()
- Model::getAssociated(string $type = null)
- Model::getColumnType(string $column)
- Model::getColumnTypes()
- Model::getID(integer $list = 0)
- Model::getInsertID()
- Model::getLastInsertID()
- 虛擬列
- Creating virtual fields
- Using virtual fields
- Model::hasField()
- Model::isVirtualField()
- Model::getVirtualField()
- Model::find() and virtual fields
- Pagination and virtual fields
- Virtual fields and model aliases
- Virtual fields in SQL queries
- Limitations of virtualFields
- 事務(wù)
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)