sanovskiy / simple-object
Simple ORM for MySQL and MSSQL
Requires
- php: ^8.2
- ext-pdo: *
- league/climate: ^3.8
- nette/php-generator: ^4.1
- sanovskiy/traitlib: ^2.0
- sanovskiy/utility: ^1.4
Suggests
- robmorgan/phinx: Allows creating migrations
- dev-master
- 7.0.6-beta
- 7.0.5-beta
- 7.0.4-beta
- 7.0.3-beta
- 7.0.2-beta
- 7.0.1-beta
- 7.0.0-alpha
- 6.4.1
- 6.3.1
- 6.3.0
- 6.2.5
- 6.2.4
- 6.2.3
- 6.2.2
- 6.2.1
- 6.2.0
- 6.1.5
- 6.1.4
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.6
- 6.0.5
- 6.0.4
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 5.3.4
- 5.3.3
- 5.3.2
- 5.3.1
- 5.3.0
- 5.2.7
- 5.2.6
- 5.2.5
- 5.2.4
- 5.2.3
- 5.2.2
- 5.2.1
- 5.2.0
- 5.1.5
- 5.1.4
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.3.7
- 3.3.6
- 3.3.5
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.1
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.3
- 2.3.2
- 2.3.1
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.1
- 2.2.0
- 2.1.5
- 1.9
- 1.8.13
- 1.8.12
- 1.8.11
- dev-version-7
- dev-dev
- dev-master-backup
- dev-php-5_6-rollback
- dev-dev-php8
- dev-version1
- dev-version5
- dev-version4_php7
This package is auto-updated.
Last update: 2024-03-28 11:41:15 UTC
README
More detailed documentation - https://simpleobject.readthedocs.io
Installation
composer require sanovskiy/simple-object
How to generate models
See cli/generate-models-example.php
About PKs
Don't forget that table PK must be named id
Model properties naming
All models and model properties properties are named in CamelCase by splitting table name or column by underscores. I.e: field model_id become property ModelId. Column SomeLongFieldName become Somelongfieldname.
Use robmorgan/phinx (https://github.com/cakephp/phinx) to make fully supported tables
Using models
Finding entities
$users = User::find(['role' => 'admin','is_active > ?' => 0]);
Getting entity
$user = User::one(['email' => 'user@example.org']);
OR by PK
$user = new User(1);
Populating models
You can use Collection to form self-controlled collection
$sql = 'Complex query which selects all columns from user table'; $bind = ['some'=>'bind','params'=>'for select']; $users = User::factory($sql,$bind);
Accessing properties
Model propertias are accessible by column name and CamelCase variant
$user = new User(); $user->UserName = 'johnsmith'; // table column is user_name $user->email = 'johnsmith@example.org'; // also works as Email $user->is_active = false; $user->save();
Data transforming
All data can be transformed after loading and before writing.
Rules are stores in static::$dataTransformRules
You can specify you own transformations for read and write.
class User extends Base_User { const ROLE_ADMIN = 'admin'; const ROLE_USER = 'user'; /** * @param $value * * @return string */ public static function roleTransformToAvailable($value) { if (!in_array($value, [static::ROLE_ADMIN, static::ROLE_USER])) { $value = static::ROLE_USER; } return $value; } } User::setReadTransform('role', 'custom_role', ['callback' => [User::class, 'roleTransformToAvailable']]); User::setWriteTransform('role', 'custom_role', ['callback' => [User::class, 'roleTransformToAvailable']]);
This will make any value of role
field to 'user' if it is not 'user' or 'admin'.
Custom transformations must start from 'custom_' and must contain proper callback.
Callback function first parameter must be $value.
There are some built in transformations like tinyint(1) to bool, datetime to unix timestamp and others. See Transform class.