Download the PHP package n0nag0n/super-model without Composer
On this page you can find all versions of the php package n0nag0n/super-model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download n0nag0n/super-model
More information about n0nag0n/super-model
Files in n0nag0n/super-model
Package super-model
Short Description A simple base model you can extend to reduce your hard coded SQL queries and get 90% of your db requests done quick.
License MIT
Informations about the package super-model
Update
It is highly recommended that you use flightphp/active-record instead of this library. No further development will be done with this library, but it does work as it is right now.
Super Model
Super model is a very simple ORM type php class to easily interact with tables in a database without writing a ton of SQL code all over the place.
To prove it, here are the lines of code...
This is written with performance in mind. So while it will not satisfy every single requirement in every project that's ever been built, it will work in the majority of cases, and do a kick butt job at it too!
Basic Usage
Getting started with Super Model is easy, simply extend the super model class and define a table name. That's about it.
Now what about some simple examples of how she works?
First, lets assume the following table:
Easy peasy, lemon squeezy right?
Docs
getBy*(mixed $value): array [result]
This is a method that returns one row back from the value specified. The *
part of the method refers to a field in the database. The field name is case-sensitive to whatever your field name is on your database table.
getAllBy*(mixed $value): array [ [result], [result] ]
This is a shortcut filter to return all rows by a given value. The *
part of the method refers to a field in the database. The field name is case-sensitive to whatever your field name is on your database table.
getAll(array $filters, bool $return_one_row = false): array [ [result], [result] ] or [result]
This is the filter where you can add a bunch of customization to filter the data from your table. There are a few unique keys to be aware of and some operators to help you pull your specific data.
There are also some basic config options with the model properties.
$disallow_wide_open_queries
If you have a model that you know will always return a small result set and want to be able to query the entire table, set this property. Otherwise it is a protection so that if no sql params were supplied, you wouldn't retrieve back the entire result set (which could crash and burn many things).
create(array $data): int [insert id]
This will create a single row on the table, but if you supply a multi-dimensional array, it will insert multiple rows. A primary key of id
is assumed.
update(array $data, string $update_field = 'id'): int (number of rows updated)
This will create a single row on the table, but if you supply a multi-dimensional array, it will insert multiple rows. A primary key of id
is assumed.
FAQ (Advanced Usage)
What if you want an automated way to alter your result if a specific flag is fired?
Easy peasy. There is a method called processResult()
that will run through every result you pull back. You inject special filters for this method in the $filters['processResults']
key.
What if you need to do a crazy complex SQL query that doesn't fall in the realm of this class or the getAll()
filters?
Remember the point of this class is NOT to satisfy every requirement from every project that ever has or will exist, but it will get you 90% the way there. In light of that, there is a simple way to execute the above question. Just use RAW SQL for your one off.
Testing
Simply run composer test
to run phpunit
and phpstan
. Currently at 100% coverage and that's where I'd like to keep it.
A note about 100% coverage: While the code may have 100% coverage, actual coverage is different. The goal is to test many different scenarios against the code to think through the code and anticipate unexpected results. I code to "real" coverage, not "did the code run" coverage.