简体   繁体   English

在Zend_db_select中更改表名称

[英]Changing table name in Zend_db_select

SELECT SUM(Table.ColumnThatChanges) AS 'Count', Table.B as 'Param'
FROM Table
WHERE Table.C = 4
    AND Table.B = 'Something'
    AND Table.Date BETWEEN '2014-01-01 00:00:00' AND '2014-06-30 00:00:00'

I have this SQL Query that I generate in php. 我有我在php中生成的此SQL查询。 I want to call this query quite a few times with a slight change; 我想稍作更改就多次调用此查询; I am changing ColumnThatChanges every request. 我正在更改ColumnThatChanges每个请求。

What would be the better/fastest way to do this? 什么是更好/最快的方法? Use str_replace? 使用str_replace? Call the entire string everytime? 每次都调用整个字符串吗? Use some type of class that generates my SQL statement? 使用某种类型的类生成我的SQL语句?

I'm currently generating it this way with Zend: 我目前正在Zend中以这种方式生成它:

$somequery = $this->db->select();
$somequery->from(Table, array('SUM(Table.ColumnThatChanges) AS Count', 'Table.B as Param'))
->where('C = ?', $Variable)
->where('B = ?', $Variable2)
->where('Date > ?', $start_date)
->where('Date < ?', $end_date);

I haven't had much success changing the ColumnThatChanges part of the query, maybe I'm doing it wrong? 更改查询的ColumnThatChanges部分并没有取得很大的成功,也许我做错了吗?

If you can use PDO extension, there are prepared statements for this 如果可以使用PDO扩展,则为此准备了一些声明

<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);

// insert one row
$name = 'one';
$value = 1;
$stmt->execute();

// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();

In Zend Framework it can be achieved using Zend_Db_Statement 在Zend Framework中,可以使用Zend_Db_Statement实现

UPD If you want to change columns, as it stated here , you can use columns method or second argument to from method; UPD如果你想改变列,它说在这里 ,你可以使用columns方法或第二个参数from方法;

$select = $db->select()
         ->from(array('p' => 'products'), 'product_id')
         ->columns('product_name');

Then if you want to change anything, all you need is to preserve your $select object. 然后,如果要更改任何内容,只需保留$select对象。

UPD-2 Bill Karwin has provided a better answer for a last version of the question. UPD-2 Bill Karwin为问题的最新版本提供了更好的答案。 Please look at it. 请看一下。

If you're using PDO, you can bind parameters. 如果您使用的是PDO,则可以绑定参数。 So loop through your entries, and execute with the different data. 因此,遍历您的条目,并使用不同的数据执行。 I'm not sure if this is clear or not... ... oh wait, baldrs has an example. 我不确定是否很清楚……哦,等等,baldrs有一个例子。 However, you can also pass an array to execute: 但是,您也可以传递一个数组来执行:

$stuff = array(':name' => 'one', ':value' => 1);
$stmt->execute($stuff);

I wrote Zend_Db_Select for Zend Framework 1.0. 我为Zend Framework 1.0编写了Zend_Db_Select。 There is no method to replace columns in the select-list, only to append or insert columns. 没有方法可以替换选择列表中的列,而只能添加或插入列。

It's not hard to create a custom class to add this capability, though. 不过,创建自定义类以添加此功能并不难。

class My_Db_Select extends Zend_Db_Select
{
  public function clearColumns() {
    $this->_parts[self::COLUMNS] = array();
  }
}

Test out clearing the columns and adding different columns: 测试清除列并添加其他列:

$sel = new My_Db_Select($db);
$sel->from("foo", array());

$sel->columns(array("SUM(foo.red)", "id"), "foo");
echo $sel . "\n";

$sel->clearColumns();

$sel->columns(array("SUM(foo.blue)", "id"), "foo");
echo $sel . "\n";

Output: 输出:

SELECT SUM(foo.red), `foo`.`id` FROM `foo`
SELECT SUM(foo.blue), `foo`.`id` FROM `foo`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM