Zend Framework:我如何“自动”构建正确生成 SQL 查询

发布于 2024-09-13 06:36:45 字数 1655 浏览 9 评论 0原文

我想生成以下 SQL:

SELECT `rc`.*, `c`.`name` FROM `RunConfigurations` AS `rc` INNER JOIN `Clients` AS `c` ON rc.client_id = c.id WHERE (rc.client_id = ?) ORDER BY `rc`.`config_name` ASC

但是我得到:

SELECT `rc`.*, `c`.* FROM `RunConfigurations` AS `rc` INNER JOIN `Clients` AS `c` ON rc.client_id = c.id WHERE (rc.client_id = ?) ORDER BY `rc`.`config_name` ASC

区别是我想要 c.name,而不是 c.*
使用以下 ZF PHP 代码:

public function fetchConfigurations($clientId = null, $order = 'rc.config_name ASC')
    {
        $db = $this->getDb();
        $stmt = $db->select()
                ->from(array('rc' => 'RunConfigurations','c.name'))
                ->join(array('c' => 'Clients'),'rc.client_id = c.id')
                ->order($order);

        if(is_numeric($clientId))
        {
            $stmt->where('rc.client_id = ?')
                ->bind(array($clientId));
        }

        $results = $db->fetchAll($stmt);
        if(sizeof($results) > 0)
        {
            $configs = array();
            foreach($results as $row)
            {
                $configs[] = $this->createRunConfigurationFromRow($row);
            }
            return $configs;
        }
        else
        {
            die($stmt->__toString());
            return null;
        }
    }

这很令人恼火,我觉得我在以下任一处遗漏了一些东西:

->from(array('rc' => 'RunConfigurations','c.name'))

->join(array('c' => 'Clients'),'rc.client_id = c.id')

ZF 示例没有对此提供任何说明。

I want to generate the following SQL:

SELECT `rc`.*, `c`.`name` FROM `RunConfigurations` AS `rc` INNER JOIN `Clients` AS `c` ON rc.client_id = c.id WHERE (rc.client_id = ?) ORDER BY `rc`.`config_name` ASC

However I am getting:

SELECT `rc`.*, `c`.* FROM `RunConfigurations` AS `rc` INNER JOIN `Clients` AS `c` ON rc.client_id = c.id WHERE (rc.client_id = ?) ORDER BY `rc`.`config_name` ASC

The difference is I want c.name, not c.*
Using the following ZF PHP code:

public function fetchConfigurations($clientId = null, $order = 'rc.config_name ASC')
    {
        $db = $this->getDb();
        $stmt = $db->select()
                ->from(array('rc' => 'RunConfigurations','c.name'))
                ->join(array('c' => 'Clients'),'rc.client_id = c.id')
                ->order($order);

        if(is_numeric($clientId))
        {
            $stmt->where('rc.client_id = ?')
                ->bind(array($clientId));
        }

        $results = $db->fetchAll($stmt);
        if(sizeof($results) > 0)
        {
            $configs = array();
            foreach($results as $row)
            {
                $configs[] = $this->createRunConfigurationFromRow($row);
            }
            return $configs;
        }
        else
        {
            die($stmt->__toString());
            return null;
        }
    }

This is aggravating and I feel like I am missing something at either:

->from(array('rc' => 'RunConfigurations','c.name'))

or

->join(array('c' => 'Clients'),'rc.client_id = c.id')

and the ZF examples are not shedding any light on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无远思近则忧 2024-09-20 06:36:45

你太近了! join() 实际上有一个第三个参数,您可以在其中提供列名称,就像 from() 中的第二个参数一样。

这意味着 ->join(array('c' => 'Clients'),'rc.client_id = c.id',array('name')) 应该生成 SQL你正在寻找。

-- 引用自 Zend 框架手册

join() 的第三个参数是列名数组,与 from() 方法中使用的数组类似。它默认为“*”,支持关联名称、表达式和 Zend_Db_Expr,与 from() 方法中的列名称数组相同。

You are so close! join() actually has a 3rd parameter in which you can supply the column names just like the 2nd parameter from from().

This would mean that ->join(array('c' => 'Clients'),'rc.client_id = c.id',array('name')) should generate the SQL you are looking for.

-- Quote from the Zend Framework manual:

The third argument to join() is an array of column names, like that used in the from() method. It defaults to "*", supports correlation names, expressions, and Zend_Db_Expr in the same way as the array of column names in the from() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文