zoukankan      html  css  js  c++  java
  • CakePHP 2.x CookBook 中文版 第七章 模型

    模型

    模型是应用程序中业务层的类。

    这意味着它们负责管理工作域中几乎所有的与数据有关的东西:数据校验、交互和信息流演化。

    通常模型类代理数据,用于 CakePHP 应用程序的数据访问,多数时候它们代理数据库表,但并不限于此,它也可以访问操纵数据的任何事物,如文件、外部 web service、iCal 事件或者一个 CSV 文件的行。

    一个模型可以与其它模型相关联。例如,一个 Recipe 能够与食谱的 Author 及 食谱的 Ingredient 相关联。

    这一节将说明模型的哪些特性可以是自动化的,如何覆盖这些特性,以及模型都有哪些方法和属性。还说明了关联数据的不同方法。描述了如果查找、保存和删除数据。最后,讲解了数据源。

    理解模型

    模型描述了你的数据模型。在面向对象编程中,一个数据模型是描述一件事的对象,比如一辆汽车、一个人、一幢房子。举例来说,一个博客可能有很多博客文章,每篇文章可能有很多评论。博客、文章和评论是全部示例模型,每一个都与其它的相关联。

    下面是在 CakePHP 中定义的模型的简单示例:

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

    只需这样一个简单的声明,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 变量:

    1 class IngredientsController extends AppController {
    2     public function index() {
    3         //抓取所有的 ingredients 并将其传递给视图:
    4         $ingredients = $this->Ingredient->find('all');
    5         $this->set('ingredients', $ingredients);
    6     }
    7 }

    关联模型通过主模型访问。在上面的例子中,Recipe 与 Ingredient 模型关联:

    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 }

    上面的代码显示了如何使用已经连接的模型。想了解如何定义关联请移步至 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 变得复杂时怎么办?
      • 数据表
        • 使用 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
  • 相关阅读:
    hdu 2485 Destroying the bus stations 迭代加深搜索
    hdu 2487 Ugly Windows 模拟
    hdu 2492 Ping pong 线段树
    hdu 1059 Dividing 多重背包
    hdu 3315 My Brute 费用流,费用最小且代价最小
    第四天 下载网络图片显示
    第三天 单元测试和数据库操作
    第二天 布局文件
    第一天 安卓简介
    Android 获取存储空间
  • 原文地址:https://www.cnblogs.com/matchless/p/2888826.html
Copyright © 2011-2022 走看看