Download the PHP package peacq/picorm without Composer
On this page you can find all versions of the php package peacq/picorm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package picorm
Short Description Lightweight ORM
License LGPL
Homepage https://github.com/iNem0o/PicORM
Informations about the package picorm
a lightweight PHP ORM.
PicORM will help you to map your MySQL database rows into PHP object and create relations between them.
PicORM is an Active Record pattern implementation easy to install and use.
Some stuff you need to know
- You can't have multiple primary key yet
- You can't use Many To Many relations with data in relation table yet
- Fork and pull request are encouraged!
Install
From composer
Install composer in your www folder with curl -sS https://getcomposer.org/installer | php
Create a composer.json
file with
Install PicORM with php composer.phar install
From source
Clone https://github.com/iNem0o/PicORM
repository and include PicORM
autoload with
Load and configure PicORM
Before using PicORM
you have to configure it.
datasource
is the only required parameter and have to be a PDO instance
Model
Implements a Model
First you have to create a table, which your model will be mapped to
Next create a class which extends \PicORM\Model
You have to implements some static parameters to describe your MySQL table schema, if you forgot one of them, a \PicORM\Exception
will remind you
Required
protected static $_tableName
MySQL table name
protected static $_primaryKey
table primary key field name
protected static $_tableFields
array with all mysql table fields name without primary key
Optional
protected static $_databaseName
name of the database if different from datasource main DB
and then, add one public property by table field with public $fieldName
Brand model declaration
Create and save
Update and delete
find() and findOne()
Every subclass of Model
inherit of static methods find()
and findOne()
.
Theses methods are used to hydrate models from database rows.
How to use $where parameter
this parameter is data for building a WHERE mysql clause
How to use $order parameter
This parameter is data for building an ORDER mysql clause
Collections
Collections in PicORM are created by ::find()
method, accessible statically on each \PicORM\Model
subclass.
Once you have a fresh \PicORM\Collection instance, data is not fetched yet. Fetching is done only when you try to access data using one of these ways
Collections usage
An \PicORM\Collection instance can execute UPDATE and DELETE queries on the collection members before fetching data,
this way using update()
or delete()
method produce only one MySQL query based on find()
restriction parameters.
Collections pagination
Pagination in collection is based on MySQL FOUND_ROWS(). Remembering that collection is not fetched until you use it, when you have a Collection instance, its easy to activate pagination with
You have now access to 2 more methods.
Collection advanced query
You are able to alter the fetch query before it execution using the method on the collection. You will get an instance of which you can manipulate. Once you are done, just set the collection query helper with
That way you can load custom data inside a model instance.
Relations between models
Using relations will need you to add a property and a method to your model subclass.
protected static $_relations = array();
needed to be implemented to store model relations
protected static function defineRelations() { }
method to declare your relation
overriding defineRelations()
in your subclass allow you to declare your model specific relations
using one of the 3 next methods.
Using relation
This example will use the following MySQL schema
First you have to declare your 3 models and their relations
Now you can start to create and manipulates related models
As you declare a one to many relation from Brand to Car you can also using setter and getter on the other side
Many to many relations are easy to use too
Changelog
UNSTABLE 0.0.1
- Initial release
UNSTABLE 0.0.2
- Bug fixes
- Add namespace support and pdo fetch mode selection
- Add transaction support
BETA 0.0.3
- Bug fixes
- Refactoring MySQL
- Collections
- Tests
BETA 0.0.4
- Refactoring Entity to Model
- QueryBuilder : SQL hint and FoundRows
- Collection pagination
- Tests
BETA 0.0.5
- Customize collection query builder
- Moving from SQL_CALC_FOUND_ROWS to count(*)
- Fix bug with relation alias name and caps
- Fix bug in relation getter with LIMIT clause
- Fix bug with no where / order / limit in many to many getter
- Refactoring Code
- Refactoring PHPDoc
- Refactoring Tests
- Adding TravisCI
BETA 0.0.6
- Adding setter for database and table
- Bugfix:update query doesnt use tablename getter
- Bugfix : where value was not prefixed with table name in many to many getter
- Update readme