从 Zend_Db_Table_Select 获取表别名

发布于 2024-08-02 07:46:23 字数 605 浏览 8 评论 0原文

我正在为我的 Zend Framework 库开发 Active Record 模式(类似于 RoR/Cake)。我的问题是:如何确定选择对象是否使用表的别名?

$select->from(array("c" => "categories"));

vs.

$select->from("categories");

,我将其传递给“fetch”函数,该函数添加额外的联接等以自动获取行关系...我想添加一些自定义sql;根据用户如何使用“from”方法,“c.id”或“categories.id”。

我知道我可以用来

$parts = $select->getPart(Zend_Db_Select::FROM); 

获取数组形式的数据,并且表名称或别名似乎位于所述数组的“槽”0 中。表名或别名是否始终位于槽零中?即我可以可靠地使用:

$tableNameOrAlias = $parts[0];

抱歉,如果这很复杂,但希望你能提供帮助! :)

I'm working on an Active Record pattern (similar to RoR/Cake) for my Zend Framework library. My question is this: How do I figure out whether a select object is using an alias for a table or not?

$select->from(array("c" => "categories"));

vs.

$select->from("categories");

and I pass this to a "fetch" function which adds additional joins and whatnot to get the row relationships automatically...I want to add some custom sql; either "c.id" or "categories.id" based on how the user used the "from" method.

I know I can use

$parts = $select->getPart(Zend_Db_Select::FROM); 

to get the from data as an array, and the table name or alias seems to be in "slot" 0 of said array. Will the table name or alias always be in slot zero? i.e. can I reliably use:

$tableNameOrAlias = $parts[0];

Sorry if this is convolute but hope you can help! :)

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

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

发布评论

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

评论(1

水晶透心 2024-08-09 07:46:23

从逻辑上讲,我认为这应该是这样的。为了安全起见,请使用 Select() 构建一些虚拟查询,并使用 print_r 等转储部分数组。

我刚刚执行了此测试,别名是数组键,它不是从零开始的数字数组:

   $select = $this->db->select()->from(array("c" => "categories","d" => "dummies"));
   $parts = $select->getPart(Zend_Db_Select::FROM);
   echo '<pre>';
   print_r($parts);
   echo '</pre>';

输出:

Array
(
    [c] => Array
        (
            [joinType] => inner join
            [schema] => 
            [tableName] => categories
            [joinCondition] => 
        )

)

因此您需要将其引用为 $part["c"]

Logically, I would think that's how it should work. To be on the safe side, build a few dummy queries using a Select() and dump the part array using print_r or such.

I just performed this test, the alias is the array key, it is not a zero-based numeric array:

   $select = $this->db->select()->from(array("c" => "categories","d" => "dummies"));
   $parts = $select->getPart(Zend_Db_Select::FROM);
   echo '<pre>';
   print_r($parts);
   echo '</pre>';

Output:

Array
(
    [c] => Array
        (
            [joinType] => inner join
            [schema] => 
            [tableName] => categories
            [joinCondition] => 
        )

)

So you would need to reference it as $part["c"]

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