The PHP Call () method implements database coherence operations

Source: Internet
Author: User
Background: The pair of jquery has a DOM called chained operation, and PHP can use a similar method when writing SQL, such as:

/* * Get user paged album from database * * @param str|array $where query criteria * @param int $page current page * @param int $perpage number of records displayed per page * @param str $o Rder Sort Type * @param str $asc Ascending/descending * @return Array satisfies the condition of the record/public function Getusersalbumfromdb ($where, $page, $perpage, $ Order, $ASC) {$offset = ($page-1) * $perpage; $dataList = $this->getselectobj ()->where ($where)->order (er , $ASC)->limit ($perpage, $offset)->fetchobject (); return $dataList; }

This is a bit like jquery's chained notation, as follows:

$ (this). Find ("div"). CSS ("Background", "red"). End (). siblings (). Find ("div"). CSS ("Background", "green");

__call function Description: http://justwinit.cn/post/2834/

=================================================

Using the __call () method to implement database coherence operations

  "", "where" = "", "order" = "" "," limit "=" "", "group" = "", "having" =       > "",); The coherent operation calls the field () where () order () limit () group () method, which combines the SQL statement function __call ($methodName, $args) {//sets the first parameter               Number (representing the method name that does not exist), all in lowercase, get the method name $methodName = Strtolower ($methodName); If the calling method name and the member property array $sql The subscript, the second argument is given to the element that corresponds to the subscript in the array if (Array_key_exists ($methodName, $this->sql)) {$this-&G        t;sql[$methodName] = $args [0]; }else{echo ' Call class '. Get_class ($this). ' The method in the '. $methodName. '        () does not exist ';    }//Return your own object, you can continue to invoke the method in this object to form a coherent operation return $this; }//output coherent operation after the combination of a SQL statement, is the last method of coherent operation function Select () {echo "Select {$this->sql[' field '} from the user {$thi s->sql[' where '} {$this->sql[' order '} {$this->sql[' limit '} {$this->sql[' group '}} {$this->s    ql[' having ']} "; }} $db = new db ();//coherent Operation $db->field (' Sex, count (Sex) ')->where (' wheRe Sex in ("Male", "female") ')->group (' Group by Sex ')->having (' have AVG (age) > ')->select ();? >

----------Debugging PHP----------

Select Sex, COUNT (sex) from the user where sex in ("Male", "female") Group by sex

have avg (age) > 25

Output Completed (0 sec consumed)-Normal termination

From: http://blog.sina.com.cn/s/blog_6109978501017154.html

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.