【问题标题】:Creating a database query using dsql使用 dsql 创建数据库查询
【发布时间】:2011-09-21 09:17:38
【问题描述】:

是否可以使用 dsql() 的任何方法进行查询?

类似:

$dq=$this->api->db->dsql()->execute('insert into table xx (field1,field2) values ('a','b')');

谢谢

【问题讨论】:

    标签: php mysql sql frameworks atk4


    【解决方案1】:

    DSQL 现在被重构为一个单独的库:https://git.io/dsql

    $this->api->db->dsql()->table('table')->set('field1','a')->set('field2','b')->do_insert();
    

    当然也可以使用

    $this->api->db->dsql()->table('table')->set($associative_array)->do_insert();
    

    【讨论】:

    【解决方案2】:

    罗马书已经展示了一个插入的例子。您可以像这样选择一个数据数组(注意 debug() 是可选的,并将在页面顶部输出生成的 sql 语句)

       $db=$this->api->db->dsql()->table('table1 x')
                 ->field('x.col1')
             ->field('sum(x.col2) col2') 
      //         ->debug()
             ->join('table2 y','x.id=y.some_id')
             ->where('x.col3','Y')
             ->where('x.col4',$this->auth->api->get('id'))
             ->group('x.col2')
                         ->order('x.col1')
             ->do_getAll();
    

    然后循环遍历结果集做某事

       foreach ($db as $row) {
          if (is_array($row)) {
             //do something here with row['col1'], row['col2']            
          }
        }
    

    只选择一个值

       $db=$this->api->db->dsql()->table('table1 x')
                 ->field('x.col1')
             ->where('x.col2,'constant')
             ->do_getOne();
    

    您可以更新记录

               $this->api->db->dsql()->table('table1 x')
                 ->where('id',$somevariable)
                 ->set('col1',$somevalue)
                 ->debug()
                 ->do_update();
    

    并删除记录

                $this->api->db->dsql()->table('table1 x')
                 ->where('id',$somevariable)
                 ->do_delete();
    

    在可能的情况下,最好使用如下模型来持久化您的数据,但 ATK4 的好处是它不会妨碍您,因此如果需要,您可以在页面中的任何位置执行 SQL 以获得简单的结果。

           $s=$p->add('Model_Story')->loadData($story);
           $s->set('points', $value);
           $s->update();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 2020-02-08
      相关资源
      最近更新 更多