日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php框架 > CakePHP > CakePHP 2.x CookBook 中文版 第七章 模型

CakePHP 2.x CookBook 中文版 第七章 模型

來源:程序員人生   發(fā)布時(shí)間:2014-09-17 09:11:56 閱讀次數(shù):4235次

模型

模型是應(yīng)用程序中業(yè)務(wù)層的類。

這意味著它們負(fù)責(zé)管理工作域中幾乎所有的與數(shù)據(jù)有關(guān)的東西:數(shù)據(jù)校驗(yàn)、交互和信息流演化。

通常模型類代理數(shù)據(jù),用于 CakePHP 應(yīng)用程序的數(shù)據(jù)訪問,多數(shù)時(shí)候它們代理數(shù)據(jù)庫表,但并不限于此,它也可以訪問操縱數(shù)據(jù)的任何事物,如文件、外部 web service、iCal 事件或者一個(gè) CSV 文件的行。

一個(gè)模型可以與其它模型相關(guān)聯(lián)。例如,一個(gè) Recipe 能夠與食譜的 Author 及 食譜的 Ingredient 相關(guān)聯(lián)。

這一節(jié)將說明模型的哪些特性可以是自動(dòng)化的,如何覆蓋這些特性,以及模型都有哪些方法和屬性。還說明了關(guān)聯(lián)數(shù)據(jù)的不同方法。描述了如果查找、保存和刪除數(shù)據(jù)。最后,講解了數(shù)據(jù)源。

理解模型

模型描述了你的數(shù)據(jù)模型。在面向?qū)ο缶幊讨校粋€(gè)數(shù)據(jù)模型是描述一件事的對象,比如一輛汽車、一個(gè)人、一幢房子。舉例來說,一個(gè)博客可能有很多博客文章,每篇文章可能有很多評(píng)論。博客、文章和評(píng)論是全部示例模型,每一個(gè)都與其它的相關(guān)聯(lián)。

下面是在 CakePHP 中定義的模型的簡單示例:

1 class Ingredient extends AppModel { 2 public $name = 'Ingredient'; 3 }

只需這樣一個(gè)簡單的聲明,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)核文件夾。覆寫 AppModel 類允許你定義對應(yīng)用程序中所有的模型類都可用的功能。要做到這一點(diǎn),只需要在 Model 文件夾中建立你自己的 AppModel.php ,和應(yīng)用程序中的其它模型類放在一起。使用 Bake 建立項(xiàng)目時(shí),會(huì)自動(dòng)為你生成這個(gè)文件。

關(guān)于如何向多個(gè)模型添加相同的邏輯的更多信息,請參見 Behaviors 。

回到我們的 Ingredient 模型,為了在其上工作,需要在 /app/Model 文件夾創(chuàng)建 PHP 文件。按照約定它的名字應(yīng)該與類相同,對于本例,就是 Ingredient.php 。

注解

如果 CakePHP 沒有在 /app/Model 文件夾中找到符合條件的文件,它將動(dòng)態(tài)創(chuàng)建一個(gè)模型對象。這意味關(guān)如果你的模型文件命名錯(cuò)誤(例如 ingredient.php 或者 Ingredients.php),CakePHP 將使用出乎你意料的 AppModel 的實(shí)例(按照 CakePHP 自己的思維方式)。如果你嘗試使用你已經(jīng)定義在你的模型聽方法,或者附加到你的模型上的行為,你會(huì)收到一個(gè)以你調(diào)用的方法名標(biāo)識(shí)的 SQL 錯(cuò)誤信息 - 它是 CakePHP 找不到你的模型的明確提示,你需要檢查文件名、應(yīng)用程序緩存,或者兩者都檢查一下。

注解

一些類名不能用于模型名。舉例來說,’‘File’’ 不能使用,因?yàn)?‘’File’’ 是 CakePHP 內(nèi)核中已經(jīng)存在的類。

定義過的模型就可以在 控制器 中訪問了。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)模型通過主模型訪問。在上面的例子中,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)請移步至 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ù)查找類型
      • findAllBy
      • findBy
    • Model::query()
    • Model::field()
    • Model::read()
    • 復(fù)雜的查找條件
      • 子查詢
      • 預(yù)處理語句
  • 保存數(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)
    • 通過數(shù)據(jù)保存 hasMany
      • 保存相關(guān)模型數(shù)據(jù) (HABTM)
        • 當(dāng) HABTM 變得復(fù)雜時(shí)怎么辦?
    • 數(shù)據(jù)表
      • 使用 created 和 modified 列
  • 刪除數(shù)據(jù)
    • delete
    • deleteAll
  • 數(shù)據(jù)校驗(yàn)
    • 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
  • 回調(diào)方法
    • beforeFind
    • afterFind
    • beforeValidate
    • beforeSave
    • afterSave
    • beforeDelete
    • afterDelete
    • onError
  • 行為
    • Using Behaviors
    • Creating Behaviors
    • Creating behavior methods
      • Mapped 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ù)
    • Nested Transactions
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 久久国产一区二区三区 | 欧美日韩亚洲天堂 | 国产精品久久久久久久久久久久 | 91免费看片网站 | 日日干天天射 | 午夜欧美 | 中字一区| 中文天堂网 | 亚洲激情欧美 | 成人妇女免费播放久久久 | 国产视频一区在线观看 | 国产成人免费网站 | 国产一区| 日本国产一区 | 久久av一区二区三区亚洲 | 日韩毛片在线看 | 91福利视频网站 | 国产成人精品一区二区三区在线 | 国产在线播放精品 | 激情综合社区 | 日本三级网址 | 精品久久久久久久久久中文字幕 | 日韩不卡一区二区三区 | 国产精品国产三级国产aⅴ原创 | 国产成人精品网站 | 亚洲国产成人精品女人 | 亚洲天堂成人 | 亚洲精品视频成人 | 麻豆国产一区 | 日韩精品视频国产 | 国产成人在线电影 | 自拍 亚洲 | 亚洲精品美女久久久 | 不卡三区 | 精精国产xxxx视频在线野外 | 亚洲精品一区二区三 | 91精品国产综合久久小美女 | 亚洲精品国产第一综合99久久 | 久久鬼| 一区二区三区日韩欧美 | 免费看v片 |