php-sql-query-builder icon indicating copy to clipboard operation
php-sql-query-builder copied to clipboard

tutorial doesn't work

Open collmomo opened this issue 8 years ago • 15 comments

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, `

collmomo avatar Mar 23 '16 17:03 collmomo

Confirmed here, too.

ghost avatar Apr 26 '16 23:04 ghost

If i use where() Here gives error too. What the hell are those erros.

ovflowd avatar Jul 21 '16 14:07 ovflowd

@sant0ro provide the code of the query and I'll look into it :)

nilportugues avatar Jul 21 '16 14:07 nilportugues

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.

ovflowd avatar Jul 21 '16 14:07 ovflowd

@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...

ovflowd avatar Jul 22 '16 17:07 ovflowd

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.

smeeckaert avatar Jan 12 '17 16:01 smeeckaert

Can u fix this shit?

assada avatar Mar 02 '17 14:03 assada

Would be great to fix it, same error here! This simple code just doesn't work:

$query = $builder->select()
    ->setTable('user')
    ->setColumns(['id','name']);

echo $builder->write($query);

Error:

PHP Fatal error: Uncaught TypeError: 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 /var/www/site/includes/search.inc.php on line 26 and defined in /var/www/site/vendor/nilportugues/sql-query-builder/src/Builder/GenericBuilder.php:216

Removing '->setColumns(['id','name'])' everything works fine. And yes, the columns id and name exist ;-)

g-giani avatar Jul 05 '17 15:07 g-giani

@g-giani I've got the same problem, the tutorial has error true. you can fix this - do not resign variable $query; try to use this $query = $builder->select(); // here you get the new Select instance $query->setTable('user') ->setColumns(['id','name']); echo $builder->write($query); //here is the place where you send new Select instance to builder

volodymyrtym avatar Nov 01 '17 12:11 volodymyrtym

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

SmattyCore avatar Feb 02 '18 11:02 SmattyCore

I believe it will never be solved.

belaarany avatar Feb 17 '18 10:02 belaarany

I do not think the author is taking care of this nice library any more. For those who are fetching this problem can change the dependency to my fork of this library at https://github.com/mmuhasan/php-sql-query-builder/ with the following composer.json configuration:

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/mmuhasan/php-sql-query-builder"
        }
    ],
    "require":{
        "nilportugues/sql-query-builder" : "dev-where-bug-fix"
    },
    "prefer-stable":"true"

In my repo the tutorial is also fixed.

Note: the fixed I made is proposed by @smeeckaert earlier in this thread. A pull request is also created with the changes.

mmuhasan avatar Mar 27 '18 17:03 mmuhasan

Appending ->where()->end(); works for me:

$query = $builder->select()
    ->setTable('user')
    ->setColumns(['user_id','name','email'])
    ->where()->end();

This will convert a ColumnQuery back to a QueryInterface without side effects.

dcrouch79 avatar Nov 06 '18 12:11 dcrouch79

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 :)

dcrouch79 avatar Nov 09 '18 14:11 dcrouch79

@dcrouch79 Indeed this is a great project and like you and everyone else I was also thinking the project is not maintained any further, but actually, it is. As you figure out some of it, would you please update the right files and create a pull request to have it in the main repository. It will help all of us in the coming days. Btw, the reason it is currently working the way you said because the author merges one of the pull request proposed by me.

mmuhasan avatar Nov 10 '18 10:11 mmuhasan