php-sql-query-builder: tutorial doesn't work

PHP 7.0

                    ->setTable('user')
                    ->setColumns(['userId' => 'user_id', 'username' => 'name', 'email' => 'email']);
echo $builder->write($query);
`

``` [Symfony\Component\Debug\Exception\FatalThrowableError]
  Type error: Argument 1 passed to NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder::write() must implement interface NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface, instance of NilPortu  
  gues\Sql\QueryBuilder\Manipulation\ColumnQuery given, `

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Comments: 15 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Can u fix this shit?

I researched a little and discovered that the core isn’t soo hackable, see an example: If i create the SQL Instruction directly by invoking the correspondent methods, as an example where() and setColumns() statement, they return respectively Where and ColumnQuery class instances.

Generating the following error:

Catchable fatal error: Argument 1 passed to NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder::write() must implement interface NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface, instance of NilPortugues\Sql\QueryBuilder\Syntax\Where given, called in C:\Users\NOTEBOOKRIC\Documents\GitHub\middleware_mc_srvcl_rest_raise\UIoT\Factories\InstructionFactory.php on line 102 and defined in C:\Users\NOTEBOOKRIC\Documents\GitHub\middleware_mc_srvcl_rest_raise\UIoT\Vendor\nilportugues\sql-query-builder\src\Builder\GenericBuilder.php on line 216

I simply used.

$query = $builder->select()->setTable('DEVICES')->setColumns($deviceArray)->where()->equals('ID', 7);
$builder->write($query);

as example, and the error ahead was returned to me.

BUT, if i do in this way:

$parteOne = $builder->select()->setTable('DEVICES');
$parteOne->setColumns($deviceArray);
$parteOne->where()->equals('ID', 7);
$builder->write($parteOne);

Everything works correctly. Maybe your Variable Injection isn’t working correctly.

After looking through the tests, all of which pass, there is a key difference in how the queries are built up in the tutorials. Rather than calling builder->select() to initialise the query, create a new instance of the Select class instead. The first example should actually read:

use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;

$builder = new GenericBuilder(); 

$query = new Select();
$query->setTable('user')->setColumns(['user_id','name','email']);
        
echo $builder->write($query);

Following this method all of the examples I’ve tried so far do indeed work.

I know this project is no longer maintained, but it is an awesome project and this might help someone else who wants to use it 😃

Why can this still be open? Would be cool to get it solved asap.

I had to add

/**
* @return QueryInterface
*/
public function end()
{
   return $this->query;
}

Then i can do something like this:

$query = $builder->select()->where()->equals('field', 'value')->end();
$builder->write($query);

It’s not the best thing but it’s the only workaround I found.

@nilportugues what you think can be? The tutorial is a demonstration? or is intended to wisely work? I didn’t give a look in the source code of this library, but i think that’s a bug?

Since also phpStorm is warning me that the return types of the variables aren’t the expected as the tutorial…

Confirmed here, too.