cakephp菜鸟笔记3

来源:互联网 发布:阿里云邮箱客服400 编辑:程序博客网 时间:2024/05/01 20:28
接下里便是model部分,model是用<?php开始,但没有?>结束。但cakephp的model部分做得非常好了,已有简单ORM。
<?phpclass Product extends AppModel { var $name = 'Product'; var $belongsTo = array(         "Taxonomy"=> array(        "className" => 'Taxonomy',        "foreignKey" => 'taxonomy_id',//这里是product表的外键。       )       ); //ORM属性,产品属于分类。  var $validate = array(  'tid' => array(   'numeric' => array(    'rule' => array('numeric'),    //'message' => 'Your custom message here',    //'allowEmpty' => false,    //'required' => false,    //'last' => false, // Stop validation after this rule    //'on' => 'create', // Limit validation to 'create' or 'update' operations   ),  ),  'name' => array(   'rule' => array('notEmpty'),   'message' => '產品名不能為空',   //'allowEmpty' => false,   //'required' => false,   //'last' => false, // Stop validation after this rule   //'on' => 'create', // Limit validation to 'create' or 'update' operations  ),  'url' => array(           'rule' => array('url','notEmpty'),//检验规则可以几个一起          'message' => 'url请正确填写',          'last' => true,          ),  'thumb_url' => array(           'rule' => array('url','notEmpty'),          'message' => 'url请正确填写',          'last' => true,          ),  'lang' => array(   'boolean' => array(    'rule' => array('boolean'),    //'message' => 'Your custom message here',    //'allowEmpty' => false,    //'required' => false,    //'last' => false, // Stop validation after this rule    //'on' => 'create', // Limit validation to 'create' or 'update' operations   ),  ), );}

从上面代码可知道,Product属于Taxonomy,关键属性就是$belongsTo,然后按以上格式就行数组定义,便能查询时获得相应的Taxonomy信息。

 除了ORM信息之外,上面还可知道,数据检验等信息,一般简单的数据检验都可以按照以上处理,cakephp官网上有数据检验等参数,就是'rule'=>所指向的数组,url、boolean、notEmpty都是已经有定义,也可以使用正则表达式,需要自行查找使用方式。如果以上简单的数据检验逻辑都无法满足就必须要使用检验方法,在controller和model里面都可以使用,具体请查找cakephp手册。 

一般简单的数据逻辑都可以如此处理,当然还可以在model里面使用方法。这就需要自己查阅cakephp手册。