Get table alias from Zend_Db_Table_Select
I am working on an Active Record pattern (similar to RoR / Cake) for my Zend Framework library. My question is this: how do I determine if the select object is using an alias for the table or not?
$select->from(array("c" => "categories"));
against.
$select->from("categories");
and I pass this to the "fetch" function that adds extra joins and whatnot to get the row relationships automatically ... I want to add some custom sql; or "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 data from an array, and the table name or alias appears to be in "slot" 0 of the specified array. Will the table name or alias always be in slot zero? that is, I can reliably use:
$tableNameOrAlias = $parts[0];
Sorry if this is collapsed, but hope you can help! :)
source to share
Logically, I would think about how it should work. To be on the safe side, create some dummy queries with Select () and dump the part array with print_r
or such.
I just did this test, the alias is the key of the array, 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] =>
)
)
Thus, you will need to refer to it as $part["c"]
source to share