nilportugues / php-sql-query-builder

An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation.
http://nilportugues.com
MIT License
417 stars 114 forks source link

Exception when trying to build a simple query #89

Open diogocavilha opened 7 years ago

diogocavilha commented 7 years ago

Hi, I'm following the docs in order to test the package and I've got that exception:

TypeError: Argument 1 passed to NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder::write() must implement interface NilPortugues\Sql\QueryBuilder\Manipulation\QueryInterface, instance of NilPortugues\Sql\QueryBuilder\Manipulation\ColumnQuery given, called in /www/operand_api_vuejs/public/index.php on line 33 in /www/operand_api_vuejs/vendor/nilportugues/sql-query-builder/src/Builder/GenericBuilder.php on line 216

The same code as in the docs... here

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

$builder = new GenericBuilder(); 

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

echo $builder->write($query);

By the way, I'm using php 7.1.

sctt commented 7 years ago

that problem is affecting everybody. i don't know why the maintainers seem to ignore such a severe bug, i couldn't build almost any of the queries i needed, and those were very basic ones -.-

anyway i'll post my workaround:

  1. edit this file src/Manipulation/ColumnQuery.php
  2. paste this function at the end:
    public function end()
    {
        return $this->select;
    }
  3. change your query like this:
    $query = $builder->select()
    ->setTable('user')
    ->setColumns(['user_id','name','email'])
    ->end();

it should work now.

diogocavilha commented 7 years ago

@sctt Great!

Thanks ever so much.

aviggngyv commented 7 years ago

Maybe should be change like this:

public function setColumns(array $columns) { $this->columns = $columns;

    return $this->select;
}

but I found another bug : leftJoin not work.

marcus-hiles commented 5 years ago

Great this post was helpful @diogocavilha @sctt