CakePHP 1.2RC2 手册学习笔记

本文介绍了CakePHP中模型之间的各种关联方式,包括hasOne、belongsTo、hasMany及hasAndBelongsToMany等,并详细解释了每种关联所需的参数及其作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Titles
    An object, in the physical sense, often has a name or a title in which to refer to it by. A person has a name like John or Mac or Buddy. A blog post has a title. A category has a name.

    By specifying a title or name field, CakePHP will automatically use this label in various circumstances:

        * Scaffolding — page titles, fieldset labels
        * Lists — normally used for <select> drop-downs
        * TreeBehavior — reordering, tree views

    If you have a title and name field in your table, the title will be used.
=========================
2.created and modified
=========================
3.hasOne
    *   className: the classname of the model being associated to the current model. If you’re defining a ‘User hasOne Profile’ relationship, the className key should equal ‘Profile.’
    * foreignKey: the name of the foreign key found in the other model. This is especially handy if you need to define multiple hasOne relationships. The default value for this key is the underscored, singular name of the current model, suffixed with ‘_id’. In the example above it would default to 'user_id'.
    * conditions: An SQL fragment used filter related model records. It’s good practice to use model names in SQL fragments: “Profile.approved = 1” is always better than just “approved = 1.”
    * fields: A list of fields to be retrieved when the associated model data is fetched. Returns all fields by default.
    * dependent: When the dependent key is set to true, and the model’s delete() method is called with the cascade parameter set to true, associated model records are also deleted. In this case we set it true so that deleting a User will also delete her associated Profile.
=========================
4. belongsTo
    *   className: the classname of the model being associated to the current model. If you’re defining a ‘Profile belongsTo User’ relationship, the className key should equal ‘User.’
    * foreignKey: the name of the foreign key found in the current model. This is especially handy if you need to define multiple belongsTo relationships. The default value for this key is the underscored, singular name of the other model, suffixed with ‘_id’.
    * conditions: An SQL fragment used filter related model records. It’s good practice to use model names in SQL fragments: “User.active = 1” is always better than just “active = 1.”
    * fields: A list of fields to be retrieved when the associated model data is fetched. Returns all fields by default.
    * counterCache: (bool) If set to true the associated Model will automatically increase or decrease the “[singular_model_name]_count” field in the foreign table whenever you do a save() or delete(). The value in the counter field represents the numer of related rows.


=========================
5.hasMany
    *   className: the classname of the model being associated to the current model. If you’re defining a ‘User hasMany Comment’ relationship, the className key should equal ‘Comment.’
    * foreignKey: the name of the foreign key found in the other model. This is especially handy if you need to define multiple hasMany relationships. The default value for this key is the underscored, singular name of the other model, suffixed with ‘_id’.
    * conditions: An SQL fragment used filter related model records. It’s good practice to use model names in SQL fragments: “Comment.status = 1” is always better than just “status = 1.”
    * fields: A list of fields to be retrieved when the associated model data is fetched. Returns all fields by default.
    * order: An SQL fragment that defines the sorting order for the returned associated rows.
    * limit: The maximum number of associated rows you want returned.
    * offset: The number of associated rows to skip over (given the current conditions and order) before fetching and associating.
    * dependent: When dependent is set to true, recursive model deletion is possible. In this example, Comment records will be deleted when their associated User record has been deleted.
    * Note: the second parameter of the Model->delete() method must be set to true in order for recursive deletion to occur.
    * finderQuery: A complete SQL query CakePHP can use to fetch associated model records. This should be used in situations that require very custom results.


=========================
6.hasAndBelongsToMany
    *   className: the classname of the model being associated to the current model. If you’re defining a ‘User hasMany Comment’ relationship, the className key should equal ‘Comment.’
    * joinTable: The name of the join table used in this association (if the current table doesn’t adhere to the naming convention for HABTM join tables).
    * foreignKey: the name of the foreign key found in the other model. This is especially handy if you need to define multiple HABTM relationships. The default value for this key is the underscored, singular name of the other model, suffixed with ‘_id’.
    * associationForeignKey: the name of the foreign key found in the current model. This is especially handy if you need to define multiple HABTM relationships. The default value for this key is the underscored, singular name of the current model, suffixed with ‘_id’.
    * conditions: An SQL fragment used filter related model records. It’s good practice to use model names in SQL fragments: “Comment.status = 1” is always better than just “status = 1.”
    * fields: A list of fields to be retrieved when the associated model data is fetched. Returns all fields by default.
    * order: An SQL fragment that defines the sorting order for the returned associated rows.
    * limit: The maximum number of associated rows you want returned.
    * offset: The number of associated rows to skip over (given the current conditions and order) before fetching and associating.
    * finderQuery, deleteQuery, insertQuery: A complete SQL query CakePHP can use to fetch, delete, or create new associated model records. This should be used in situations that require very custom results.


=========================
7.find
   1).  find(string $conditions, array $fields, string $order, int $recursive);
   2).  find($type, $params);
   
   $type is either 'all', 'list', 'first', or 'count'. First is the default find type.
   $params is an array with any of the following available options as keys:
     
array(
          'conditions' => array('Model.field' => $thisValue), //array of conditions
          'recursive' => 1, //int
          'fields' => array('Model.field1', 'Model.field2'), //array of field names
          'order' => 'Model.created', //string or array defining order
          'limit' => n, //int
          'page' => n //int
      )



=========================
8.recursive
    Depth    Description
    -1    Cake fetches Group data only, no joins.
    0      Cake fetches Group data and its domain
    1      Cake fetches a Group, its domain and its associated Users
    2      Cake fetches a Group, its domain, its associated Users, and the Users' associated Articles
=========================
9.Changing Encryption Type
  
Security::setHash('md5'); // or sha1 or sha256.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值