Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help other guys, about left join with orderBy bug #90

Open
aviggngyv opened this issue Oct 31, 2017 · 1 comment
Open

Help other guys, about left join with orderBy bug #90

aviggngyv opened this issue Oct 31, 2017 · 1 comment

Comments

@aviggngyv
Copy link
Contributor

aviggngyv commented Oct 31, 2017

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!

@marcus-hiles
Copy link

Thanks for your post @aviggngyv , its helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants