Home >Backend Development >PHP Tutorial >Introduction to the definition method of Model function in CakePHP framework

Introduction to the definition method of Model function in CakePHP framework

巴扎黑
巴扎黑Original
2017-08-15 11:53:481218browse

This article mainly introduces the definition method of CakePHP framework Model function, and analyzes the definition method of CakePHP framework Model function based on specific query function examples. Friends in need can refer to it

The examples in this article describe the CakePHP framework Model Function definition method. I share it with you for your reference, as follows:

In CakePHP, the MVC architecture is clear, but in actual projects, I found that many people still like to stack functions in the Controller, and doing so is also It's not a bad idea, but for a large project with millions of rows, this kind of violation of MVC thinking may temporarily bring convenience to the program structure, but in the long run, it is absolutely undesirable!

We should define certain functions commonly used in the system in the Model, especially pure data processing functions and data query functions:

For example, conditional query like the following in Blog:


/*
*
*  Blog 根据条件得到相应字段结果集
*
* @Param  array  conditions
*      array  fields
* @Return  array
*
*/
function getBlogsByCon($conditions = array(),$fields = null){
  return $this->find('all',array(
      'conditions'=>ife(count($conditions) > 0, am(array('`Blog`.`status` = 1'),$conditions), array('`Blog`.`status` = 1')),
      'fields'=>$fields,
      'order'=>'`Blog`.`created` DESC',
      'page'=>1,
      'recursive'=>0));
}

The above is the detailed content of Introduction to the definition method of Model function in CakePHP framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn