任何PHP框架在操作数据库时都可以使用同调操作。如在TP3.2框架中:Laravel5中的另一个例子:本文用一个简单的例子来说明如何在框架中实现连贯的查询操作。当然,由于是一个比较简单的例子,与框架中的实现方法有很大的不同,希望能起到一个引流的作用。代码示例'','fields'=>'','where'=>'','订单'=>''];//$model->table('t1')publicfunctiontable($table){$this->options['table']=$table;返回$这个;}//$model->table('t1')->fields('a,b,c')publicfunctionfields($fields){$this->options['fields']=$fields;返回$这个;}//$model->table('t1')->fields('a,b,c')->where('id=1')publicfunctionwhere($where){$this->options['哪里']='哪里'。$在哪里;返回$这个;}//$model->table('t1')->fields('a,b,c')->where('id=1')->order('iddesc')publicfunctionorder($order){$this->options['order']='ORDERBY'.$订单;返回$这个;}//$model->table('t1')->fields('a,b,c')->where('id=1')->order('iddesc')->select()publicfunctionselect(){$sql="SELECT%FIELD%FROM%TABLE%%WHERE%%ORDER%";$sql=str_replace(['%FIELD%','%TABLE%','%WHERE%','%ORDER%'],[$this->options['fields'],$this->options['table'],$this->options['where'],$this->options['order']],$sql);返回$sql;/***正常的逻辑是Executethesqlstatement这个演示代码直接返回了sql语句*/}}/***call*/$model=newModel();$sql=$model->table('t1')->fields('a,b,c')->where('id=1')->order('iddesc')->select();var_dump($sql);//SELECTa,b,cFROMt1WHEREid=1ORDERBYiddesckey1.每次调用table()fields()或where()方法都会返回$this所以该方法可以连续执行。2.总体原理是记录每个方法用户传入的参数,最后对sql进行替换拼接。
