从 Zend_Db_Table_Select 获取表别名
我正在为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从逻辑上讲,我认为这应该是这样的。为了安全起见,请使用 Select() 构建一些虚拟查询,并使用
print_r
等转储部分数组。我刚刚执行了此测试,别名是数组键,它不是从零开始的数字数组:
输出:
因此您需要将其引用为
$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:
Output:
So you would need to reference it as
$part["c"]