php-sql-query-builder
php-sql-query-builder copied to clipboard
Help other guys, about left join with orderBy bug
I love this library, because it Works without establishing a connection to the database. but realy it have many bug wait to fix,
I just want help other guys, maybe you need this help, maybe you will found the left join can't work with orderBy, I saw the code, and find the solution:
<?php
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
$builder = new GenericBuilder();
$query = $builder->select()
->setTable('user');
$query->setColumns([
'userId' => 'user_id',
'username' => 'name',
'email' => 'email'
]);
$query->orderBy('user_id', OrderBy::DESC);
$query->leftJoin(
'news', //join table
'user_id', //origin table field used to join
'author_id', //join column
['newsTitle' => 'title', 'body', 'created_at', 'updated_at']
)
->on()
->equals('author_id', 1); //enforcing a condition on the join column
$query
->where()
->greaterThan('user_id', 5)
->notLike('username', 'John');
$query
->orderBy('created_at', OrderBy::DESC, 'news');
echo $builder->writeFormatted($query);
yes, if you use orderBy after leftJoin , you need pass the third table parms, otherwise, it will output
ORDER BY
user.user_id DESC,
user.created_at DESC;
Thanks this library!
Thanks for your post @aviggngyv , its helpful