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

Example didn't work #116

Open
Kal-Aster opened this issue Jun 18, 2020 · 1 comment
Open

Example didn't work #116

Kal-Aster opened this issue Jun 18, 2020 · 1 comment

Comments

@Kal-Aster
Copy link

Hello, I just installed this library, but I encountered a problem with the examples in the documentation.

I tested the example of the LEFT JOIN, but this didn't work:

<?php
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;

$builder = new GenericBuilder(); 

$query = $builder->select()
    ->setTable('user')
    ->setColumns([
            'userId'   => 'user_id',
            'username' => 'name',
            'email'    => 'email',
            'created_at'
    ])
    ->orderBy('user_id', OrderBy::DESC)
    ->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')
	->end();

$query
    ->orderBy('created_at', OrderBy::DESC);

echo $builder->writeFormatted($query);

With some tweaks I managed to make it work, and it appears like so:

<?php
use NilPortugues\Sql\QueryBuilder\Builder\MySqlBuilder;
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
use NilPortugues\Sql\QueryBuilder\Syntax\Where;

$builder = new MySqlBuilder(); 

$query = $builder->select();
$query->setTable('user')
    ->setColumns([
            'userId'   => 'user_id',
            'username' => 'name',
            'email'    => 'email',
            'created_at'
    ])
	->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
	->end();

$query
	->where()
    ->greaterThan('user_id', 5)
    ->notLike('username', 'John')
	->end()
	->orderBy('created_at', OrderBy::DESC);

echo $builder->writeFormatted($query);

Is it different because I used MySqlBuilder or is there something wrong I can't understand?

@hlpeter
Copy link

hlpeter commented Jul 3, 2020

I have same problem with example.
Fatal error: Uncaught Error: Call to undefined method NilPortugues\Sql\QueryBuilder\Syntax\Where::where()

edit: I fixed my problem

    $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)->end(); //enforcing a condition on the join column

    $query
    ->where()
    ->greaterThan('user_id', 5)
    ->notLike('username', 'John')
    ->end();

line: ->equals('author_id', 1)->end();

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