Download the PHP package thomasjbradley/micromodel without Composer
On this page you can find all versions of the php package thomasjbradley/micromodel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download thomasjbradley/micromodel
More information about thomasjbradley/micromodel
Files in thomasjbradley/micromodel
Package micromodel
Short Description MicroModel: a really basic ORM-like form and table mapper, for working with Silex, Symfony Forms, and Doctrine DBAL; supports single tables without relationships.
License BSD-3-Clause
Homepage http://github.com/thomasjbradley/micromodel
Informations about the package micromodel
MicroModel
A really basic ORM-like form and table mapper, for working with Silex, Symfony Forms, and Doctrine DBAL; supports single tables without relationships.
Example table
Here’s a table we’ll use for the rest of the code samples.
`planets`
| id | name | orbital_period | last_updated |
| (PK, AI) | (text) | (number) | (text) |
------------------------------------------------------
| 1 | Mercury | 87.97 | 1982-10-28 |
| 2 | Venus | 224.70 | 1980-05-21 |
| 3 | Earth | 365.25 | 1981-06-04 |
How to use
-
Install with Composer.
-
Create a PHP class in your Silex application that extends MicroModel. The class must be named identically to the table. Capitalization doesn’t matter, the class name/table name will be converted to lowercase.
- Then make a new instance of your model, passing the Silex\Application.
Defining fields
When defining fields, the options array inherits everything from Symfony\Form options arrays.
Always define the primary key field first.
Field definition extra options
MicroModel adds a few extra options to the array.
set
(function) — An optional setter function. Allows for data type conversion and sanitization.display
(boolean) — Optional; whether the field should be shown in forms or not.
Methods
☛ __construct( Silex\Application $app [, mixed $clauses = null ] )
Set up the model and optionally read a single item immediately by specifying $clauses
.
$app
— the Silex\Application object.$clauses
— passed directly toread()
. Seeread()
for more details.
☛ defineField( $name [, string $type = 'text' [, array $options = array() ]] )
Define a new field on the model to match a field in the table. Usually called from within the defineSchema()
method. Refer to defining fields.
$name
— The name of the field, spelled identically to the table’s field name.$type
— One of the Symfony\Form field types. They match very well to the Doctrine\DBAL field types.$options
— Any of the Symfony\Form field options. Refer to field registration extra options for more options.
@return — $this
☛ find( [ string|array $order = null [, array $where = array() ]] )
Get a bunch of results from the table, optionally sorting them.
Without any arguments find()
will return every entry in the database.
Will return an array of the model objects.
$order
— the field names & direction for the order clause.$where
— arrays of WHERE clause conditions, following this format:array('field', 'comparison', 'value')
.
@return — an array of objects, each object is an instance of your model.
☛ create()
INSERT
the current object into the table using the property values.
After insertion, the primary key field is populated with lastInsertId
.
The data is not validated before creation is attempted.
@return — $this
☛ read( mixed $clauses )
Read a single entry from the table, converting all the fields to properties of the object.
$clauses
— conditions for reading a single entry in the table.int|string
— the value for the individual item’s primary key.array
— arrays of WHERE clause conditions, following this format:array('field', 'comparison', 'value')
.
@return — $this
☛ update()
UPDATE
the current object in the table using the property values.
Uses the field marked as primary key for the WHERE
clause.
The data is not validated before updating is attempted.
@return — $this
☛ delete()
DELETE
the current object from the table.
Uses the field marked as primary key for the WHERE
clause.
@return — $this
☛ getForm( [ boolean $csrfProtection = true ] )
Returns a Symfony\Form object for the model. All constraints and options from the field registration are used.
$csrfProtection
— flag for enabling/disabling CSRF proection; helpful primarily for APIs
@return — Symfony\Form
☛ jsonSerialize()
Double duty: returns a simplified version of all the fields’ values in an associative array, and in PHP/5.4 it is the JsonSerializer implementation.
@return — an array
containing all the fields and their values
☛ isValid()
Validates the information in the object against the field constraints.
@return — boolean
☛ getErrors()
Return an array of all the validation error messages produced by Symfony\Form.
Generally called after isValid()
if you need to see the error messages.
If there are no error messages the array is empty.
@return — array
License
MicroModel is licensed under the BSD 3-Clause license.