-
Notifications
You must be signed in to change notification settings - Fork 13
Alias in FROM clause. #12
Comments
First idea for the second point: Create public function _as ( $alias ) {
$this->_from[key($this->_from)] = current($this->_from) .
' AS ' . $alias;
return $this;
} It should be used like: $req = new \Hoa\Database\Query\Select();
$req->from('Foo')
->_as('F')
->innerJoin(
(new \Hoa\Database\Query\Select())
->from('Bar')
)
->_as('B')
->on('F.Foo_ID = B.Bar_ForeignKey'); |
And for the first point: Change protected function _join ( $type, $source ) {
if(empty($this->_from))
return $this;
end($this->_from);
$key = key($this->_from);
$value = array_pop($this->_from);
if(!is_int($key))
$value .= ' AS ' . $key;
if($source instanceof self)
$source = '(' . $source . ')';
$this->_from[] = $value . ' ' . $type . ' ' . $source;
return new Join($this, $this->_from);
} The changes are:
Note: |
Hello :-), Maybe @camael24 could help you on this issue? |
Hello |
Finnaly i look now :p I think its more logical to write
So the AS f is not well placed :) when we have jonction, i look this after |
No, ->innerJoin(
(new \Hoa\Database\Query\Select())
->from('Bar')
->_as('B')
) Will produce: (it works) SELECT *
FROM Foo
INNER JOIN (
SELECT *
FROM Bar AS B
); And ->innerJoin(
(new \Hoa\Database\Query\Select())
->from('Bar')
)->_as('B') Will produce: (What we have to do) SELECT *
FROM Foo
INNER JOIN (
SELECT *
FROM Bar
) AS B; We could also imagine a more complex request like: $req = new \Hoa\Database\Query\Select();
$req->from('Foo')
->_as('F') // Basic table alias.
->innerJoin(
(new \Hoa\Database\Query\Select())
->from('Bar')
->_as('Z') // Table alias inside sub query.
->where('Z.Bar_ID = ?')
)
->_as('B') // Sub query alias.
->on('F.Foo_ID = B.Bar_ForeignKey'); |
/ping |
Pong, sorry for the answer extremly late :s So there is a bug, what did you think about : $req = new \Hoa\Database\Query\Select();
$req->from('Foo', 'aaa')
->select('A as B', 'C', 'D')
->innerJoin(
(new \Hoa\Database\Query\Select())->from('Bar'),
'B'
)
->on('F.Foo_ID = B.Bar_ForeignKey'); Will generate Camael |
update my previous comment |
Assuming The BC is it a problem ? |
Database are not released yet, so I don't consider it as real issue. Thoughts Hywan? |
Well, the first comments brough a solution, why introducing something new? |
So to prevent the BC, we can hardcoded the alias in |
@camael24 But what about: #12 (comment). This is a good solution that does not introduce any BC isn't it? |
👍 for #12 (comment) |
👍 |
ping ? |
What is the status @Jir4? |
Hello,
I'm trying to use a
Hoa\Database\Query\Select
object in anINNER JOIN
clause.My goal is to get the following query:
But I wasn't able to alias correctly my two sources.
Foo
table I can make an alias without the keywordAS
likefrom('Foo F')
.ON
clause (it's work well without).There are my test code and the output.
PS: Moreover the fact to don't alias an
Hoa\Database\Query\Select
object in anINNER JOIN
clause throw a SQL error :#1248 - Every derived table must have its own alias
.Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: