模型
模型是應用程序中業務層的類。
這意味着它們負責管理工作域中幾乎所有的與數據有關的東西:數據校驗、交互和信息流演化。
通常模型類代理數據,用於 CakePHP 應用程序的數據訪問,多數時候它們代理數據庫表,但並不限於此,它也可以訪問操縱數據的任何事物,如文件、外部 web service、iCal 事件或者一個 CSV 文件的行。
一個模型可以與其它模型相關聯。例如,一個 Recipe 能夠與食譜的 Author 及 食譜的 Ingredient 相關聯。
這一節將說明模型的哪些特性可以是自動化的,如何覆蓋這些特性,以及模型都有哪些方法和屬性。還說明了關聯數據的不同方法。描述瞭如果查找、保存和刪除數據。最後,講解了數據源。
理解模型
模型描述了你的數據模型。在面向對象編程中,一個數據模型是描述一件事的對象,比如一輛汽車、一個人、一幢房子。舉例來說,一個博客可能有很多博客文章,每篇文章可能有很多評論。博客、文章和評論是全部示例模型,每一個都與其它的相關聯。
下面是在 CakePHP 中定義的模型的簡單示例:
class Ingredient extends AppModel {
public $name = 'Ingredient';
}
只需這樣一個簡單的聲明,Ingredient 模型就被賦予了建立保存和刪除數據的查詢所需的全部功能。這些魔術方法繼承自 CakePHP 的 Model 類。Ingredient 模型繼承了應用程序模型 AppModel(一個擴展自內部 Model 類的模型類)。它是向 Ingredient 模型賦予功能的核心模型類。
介於內部 Model 類和最終的模型類之間的 AppModel 類,在你創建專屬於自己的 AppModel 類之前是空的,它位於 CakePHP 內核文件夾。覆寫 AppModel 類允許你定義對應用程序中所有的模型類都可用的功能。要做到這一點,只需要在 Model 文件夾中建立你自己的 AppModel.php ,和應用程序中的其它模型類放在一起。使用 Bake 建立項目時,會自動爲你生成這個文件。
關於如何向多個模型添加相同的邏輯的更多信息,請參見 Behaviors 。
回到我們的 Ingredient 模型,爲了在其上工作,需要在 /app/Model 文件夾創建 PHP 文件。按照約定它的名字應該與類相同,對於本例,就是 Ingredient.php 。
註解
如果 CakePHP 沒有在 /app/Model 文件夾中找到符合條件的文件,它將動態創建一個模型對象。這意味關如果你的模型文件命名錯誤(例如 ingredient.php 或者 Ingredients.php),CakePHP 將使用出乎你意料的 AppModel 的實例(按照 CakePHP 自己的思維方式)。如果你嘗試使用你已經定義在你的模型聽方法,或者附加到你的模型上的行爲,你會收到一個以你調用的方法名標識的 SQL 錯誤信息 - 它是 CakePHP 找不到你的模型的明確提示,你需要檢查文件名、應用程序緩存,或者兩者都檢查一下。
註解
一些類名不能用於模型名。舉例來說,’‘File’’ 不能使用,因爲 ‘’File’’ 是 CakePHP 內核中已經存在的類。
定義過的模型就可以在 控制器 中訪問了。CakePHP 會自動使與當前控制器名字相同的模型可用。例如,如果一個控制器的名字是 IngredientsController,將自動初始化 Ingredient 模型並將其賦予此控制器的 $this->Ingredient 變量:
class IngredientsController extends AppController {
public function index() {
//抓取所有的 ingredients 並將其傳遞給視圖:
$ingredients = $this->Ingredient->find('all');
$this->set('ingredients', $ingredients);
}
}
關聯模型通過主模型訪問。在上面的例子中,Recipe 與 Ingredient 模型關聯:
class Recipe extends AppModel {
public function steakRecipes() {
$ingredient = $this->Ingredient->findByName('Steak');
return $this->findAllByMainIngredient($ingredient['Ingredient']['id']);
}
}
上面的代碼顯示瞭如何使用已經連接的模型。想了解如何定義關聯請移步至 Associations section。
關於模型的更多...
- 關聯:將模型連接在一起
- 關係類型
- hasOne
- belongsTo
- hasMany
- counterCache - 緩存你的 count()
- hasAndBelongsToMany (HABTM)
- hasMany 貫穿 (連接模型)
- 在運行期間創建和銷燬關聯
- 同一模型上的多個關係
- 連接表
- 檢索數據
- find
- find(‘first’)
- find(‘count’)
- find(‘all’)
- find(‘list’)
- find(‘threaded’)
- find(‘neighbors’)
- 創建自定義 find 類型
- 魔術查找類型
- findAllBy
- findBy
- Model::query()
- Model::field()
- Model::read()
- 複雜的查找條件
- 子查詢
- 預處理語句
- 保存數據
- 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())
- 保存相關模型的數據(hasOne, hasMany, belongsTo)
- 通過數據保存 hasMany
- 保存相關模型數據 (HABTM)
- 當 HABTM 變得複雜時怎麼辦?
- 保存相關模型數據 (HABTM)
- 數據表
- 使用 created 和 modified 列
- 刪除數據
- delete
- deleteAll
- 數據校驗
- Simple Rules
- One Rule Per Field
- rule
- required
- allowEmpty
- on
- message
- Multiple Rules per Field
- last
- 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
- 回調方法
- beforeFind
- afterFind
- beforeValidate
- beforeSave
- afterSave
- beforeDelete
- afterDelete
- onError
- 行爲
- Using Behaviors
- Creating Behaviors
- Creating behavior methods
- Mapped methods
- Behavior callbacks
- Creating a behavior callback
- 數據源
- 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
- 事務
- Nested Transactions