Download the PHP package roi/sql-clause-builder without Composer
On this page you can find all versions of the php package roi/sql-clause-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package sql-clause-builder
SqlClauseBuilder
Build SQL clause from an array.
Installation
To add the package to your dependencies, you should have composer installed on your machine. Run the command below in your project directory
composer require roi/sql-clause-builder
Namespace
You can access the class within this namespace \Roi\SqlClauseBuilder
Usage
In order to use the package you must include the auto-generated autoload.php
file by composer usually in the location {project-root}/vendor
at the top of your script.
If you are using a PHP framework like Laravel, Slim etc, you can skip the above because it would be automatically added.
SqlClauseBuilder
requires you to first define an associative array of parameters that is acceptable. See sample below
Notice that each entry in the array above is an associative array with 3 defined keys, which are
col
, a string which implies to the corresponding column in the tablefactors
, an array of SQL operations that can be performed on the specified columnbind_as
, the explicit data type for the parameter. See https://www.php.net/manual/en/pdo.constants.php for a list options.
Please note that the keys are required.
Next, create an instance of SqlClauseBuilder
with the defined searchable columns passed as the argument.
Note that the $searchable_params
is the only argument required by the constructor
Then you could easily fit this in a PDO prepared statement, binding the parameters ;
Next call the build
method, passing the array you want built into a SQL clause
The build method returns an array with structure like below
You can use the result in your query as demonstrated below
WHAT IS COVERED
The following operators are supported
- like,
- equalto
- notequalto
- greaterthan
- lesserthan
- greaterthanorequalto
- lesserthanorequalto
- between
- notbetween
- isnull
- notisnull
TODO
- [ ] add support for more operators
- [ ] provide a method that allows users add operators