2

先引入Builder类

use Phalcon\Mvc\Model\Query\Builder as QueryBuilder;
public function mytestAction(){
    $builder = new QueryBuilder();
    //确定查询表
    $builder -> from(['parts'=>'Test3\Frontend\Models\Parts']);
    //关联表
    $builder -> innerJoin('Test3\Frontend\Models\Robots', 'robots.id = parts.robots_id','robots');
    // 需要查询的字段,这里两个表的字段都可以
    $builder -> columns([
        'robots.name',
        'parts.id',
        'count(parts.id) as count',  //当数据很大时,统计数据时用
    ]);
    // where条件
    $builder -> where('parts.id = :id:',array('id' =>1));
    // andWhere
    $builder -> andWhere('robots.name = :name:',array('name' => '你好'));
    
    
    //执行搜索
    if (isset($params['conditions'])) {
        foreach ($params['conditions'] as $field => $val) {
            if (!preg_match('/^\s*$/', $val)) {
               //执行模糊搜索
               $builder->andWhere("providers.$field like :$field:", array($field => '%' . trim($val) . '%'));
             }
         }
      }
    // 设置limit条件,order什么的都可以往后加$builder->order()
    $builder->limit(5,5);        // $builder->limit($rows, ($currentPage - 1) * $rows);    注意:这里的limit条件和原始sql语句中的limit语句刚好相反
   
    //获取查询对象
    $query = $builder->getQuery();
    //执行并返回结果
    $result = $query->execute();
    
    var_dump($result -> toArray());die;
    }

thinkeng
189 声望15 粉丝