【问题标题】:Dynamic query retrieval动态查询检索
【发布时间】:2012-09-28 20:23:06
【问题描述】:

我正在使用 NOTORM 生成查询,在应用程序中有一种情况,用户可以选择字段、条件等,因此基本上可以提出自定义查询。这样,用户就可以生成一个报告,例如“名字的人 = Jack,花费 > 800”。

我正在使用 NOTORM 生成此查询,但我想存储查询以供未使用 NOTORM 的应用程序的另一部分使用。是否有任何函数可以检索生成的查询?

这是 NOTORM 对象在生成和执行查询后的样子:

    object(NotORM_Result)#55 (33) {
  ["single":protected]=>
  bool(false)
  ["select":protected]=>
  array(0) {
  }
  ["conditions":protected]=>
  array(7) {
    [0]=>
    string(9) "name = ? "
    [1]=>
    string(12) "surname = ? "
    [2]=>
    string(15) "custom_field_19"
    [3]=>
    string(31) "custom_field_20 BETWEEN ? AND ?"
    [4]=>
    string(21) "custom_field_18 <= ? "
    [5]=>
    string(15) "custom_field_16"
    [6]=>
    string(10) "email = ? "
  }
  ["where":protected]=>
  array(7) {
    [0]=>
    string(9) "name = ? "
    [1]=>
    string(12) "surname = ? "
    [2]=>
    string(52) "custom_field_19 IN ('Yes', 'Sent lender info', 'No')"
    [3]=>
    string(31) "custom_field_20 BETWEEN ? AND ?"
    [4]=>
    string(21) "custom_field_18 <= ? "
    [5]=>
    string(28) "custom_field_16 IN ('Buyer')"
    [6]=>
    string(10) "email = ? "
  }
  ["parameters":protected]=>
  array(6) {
    [0]=>
    string(1) "a"
    [1]=>
    string(6) "Franco"
    [2]=>
    int(1218146400)
    [3]=>
    int(1249682400)
    [4]=>
    string(3) "800"
    [5]=>
    string(1) "?"
  }
  ["order":protected]=>
  array(0) {
  }
  ["limit":protected]=>
  NULL
  ["offset":protected]=>
  NULL
  ["group":protected]=>
  string(0) ""
  ["having":protected]=>
  string(0) ""
  ["lock":protected]=>
  NULL
  ["union":protected]=>
  array(0) {
  }
  ["unionOrder":protected]=>
  array(0) {
  }
  ["unionLimit":protected]=>
  NULL
  ["unionOffset":protected]=>
  NULL
  ["data":protected]=>
  NULL
  ["referencing":protected]=>
  array(0) {
  }
  ["aggregation":protected]=>
  array(0) {
  }
  ["accessed":protected]=>
  NULL
  ["access":protected]=>
  NULL
  ["keys":protected]=>
  array(0) {
  }
  ["connection":protected]=>
  NULL
  ["driver":protected]=>
  NULL
  ["structure":protected]=>
  NULL
  ["cache":protected]=>
  NULL
  ["notORM":protected]=>
  object(NotORM)#3 (12) {
    ["connection":protected]=>
    object(PDO)#2 (0) {
    }
    ["driver":protected]=>
    string(5) "mysql"
    ["structure":protected]=>
    object(NotORM_Structure_Convention)#4 (4) {
      ["primary":protected]=>
      string(2) "id"
      ["foreign":protected]=>
      string(5) "%s_id"
      ["table":protected]=>
      string(2) "%s"
      ["prefix":protected]=>
      string(0) ""
    }
    ["cache":protected]=>
    NULL
    ["notORM":protected]=>
    NULL
    ["table":protected]=>
    NULL
    ["primary":protected]=>
    NULL
    ["rows":protected]=>
    NULL
    ["referenced":protected]=>
    array(0) {
    }
    ["debug":protected]=>
    bool(false)
    ["freeze":protected]=>
    bool(false)
    ["rowClass":protected]=>
    string(10) "NotORM_Row"
  }
  ["table":protected]=>
  string(5) "users"
  ["primary":protected]=>
  string(2) "id"
  ["rows":protected]=>
  NULL
  ["referenced":protected]=>
  array(0) {
  }
  ["debug":protected]=>
  bool(false)
  ["freeze":protected]=>
  bool(false)
  ["rowClass":protected]=>
  string(10) "NotORM_Row"
}

我可以做一些迭代“where”和“parameters”的脚本,但看起来不太优雅,特别是因为这些数组的顺序不正确(我必须做一些事情,比如替换每个“?” “参数”中的下一项)...任何NOTORM函数?或者您建议的任何更优雅的方式?

这是 'where' 和 'parameters' 部分,以防您对最后一个选项有一些优雅:

    ["where":protected]=>
  array(7) {
    [0]=>
    string(9) "name = ? "
    [1]=>
    string(12) "surname = ? "
    [2]=>
    string(52) "custom_field_19 IN ('Yes', 'Sent lender info', 'No')"
    [3]=>
    string(31) "custom_field_20 BETWEEN ? AND ?"
    [4]=>
    string(21) "custom_field_18 <= ? "
    [5]=>
    string(28) "custom_field_16 IN ('Buyer')"
    [6]=>
    string(10) "email = ? "
  }
  ["parameters":protected]=>
  array(6) {
    [0]=>
    string(1) "a"
    [1]=>
    string(6) "Franco"
    [2]=>
    int(1218146400)
    [3]=>
    int(1249682400)
    [4]=>
    string(3) "800"
    [5]=>
    string(1) "?"
  }

【问题讨论】:

    标签: php arrays notorm


    【解决方案1】:

    只需将 NotORM_Result 对象转换为字符串

    $sql = (string)$result;
    //or
    $sql = $result->__toString();
    

    【讨论】:

    • 谢谢...但我仍然看到“?”标志,转换后我得到: SELECT * FROM users WHERE (surname != ? ) AND (custom_field_19 IN ('Yes', 'Sent Lender info', 'No')) AND (name = ? )... 为什么?完整的查询在哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2011-09-26
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多