fyre / orm
An ORM library.
Installs: 242
Dependents: 6
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 1
Open Issues: 0
pkg:composer/fyre/orm
Requires
- fyre/collection: ^1.0
- fyre/container: ^1.0
- fyre/db: ^6.2.0
- fyre/entity: ^6.0
- fyre/event: ^4.1
- fyre/inflector: ^3.0
- fyre/lang: ^5.0
- fyre/macro: ^1.0
- fyre/schema: ^7.0
- fyre/validation: ^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^12
- dev-main
- v12.0.2
- v12.0.1
- v12.0
- v11.1.3
- v11.1.2
- v11.1.1
- v11.1.0
- v11.0.9
- v11.0.8
- v11.0.7
- v11.0.6
- v11.0.5
- v11.0.4
- v11.0.3
- v11.0.2
- v11.0.1
- v11.0
- v10.1.0
- v10.0.3
- v10.0.2
- v10.0.1
- v10.0
- v9.1.0
- v9.0.3
- v9.0.2
- v9.0.1
- v9.0
- v8.0.2
- v8.0.1
- v8.0
- v7.0.3
- v7.0.2
- v7.0.1
- v7.0
- v6.0.1
- v6.0
- v5.1.0
- v5.0.7
- v5.0.6
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0
- v3.1.1
- v3.1.0
- v3.0.2
- v3.0.1
- v3.0
- v2.0.2
- v2.0.1
- v2.0
- v1.2.1
- v1.2.0
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0
This package is auto-updated.
Last update: 2025-10-21 07:28:47 UTC
README
FyreORM is a free, open-source database ORM for PHP.
Table Of Contents
- Installation
- Basic Usage
- Methods
- Models
- Queries
- Results
- Relationships
- Behavior Registry
- Behaviors
- Rules
Installation
Using Composer
composer require fyre/orm
In PHP:
use Fyre\ORM\ModelRegistry;
Basic Usage
$containeris a Container.
$modelRegistry = new ModelRegistry($container);
Autoloading
It is recommended to bind the ModelRegistry to the Container as a singleton.
$container->singleton(ModelRegistry::class);
Any dependencies will be injected automatically when loading from the Container.
$modelRegistry = $container->use(ModelRegistry::class);
Methods
Add Namespace
Add a namespace for loading models.
$namespaceis a string representing the namespace.
$modelRegistry->addNamespace($namespace);
Build
Build a Model.
$classAliasis a string representing the model class alias.
$model = $modelRegistry->build($classAlias);
Model dependencies will be resolved automatically from the Container.
Clear
Clear all namespaces and models.
$modelRegistry->clear();
Create Default Model
Create a default Model.
$model = $modelRegistry->createDefaultModel();
Model dependencies will be resolved automatically from the Container.
Get Default Model Class
Get the default model class name.
$defaultModelClass = $modelRegistry->getDefaultModelClass();
Get Namespaces
Get the namespaces.
$namespaces = $modelRegistry->getNamespaces();
Has Namespace
Determine whether a namespace exists.
$namespaceis a string representing the namespace.
$hasNamespace = $modelRegistry->hasNamespace($namespace);
Is Loaded
Determine whether a model is loaded.
$aliasis a string representing the model alias.
$isLoaded = $modelRegistry->isLoaded($alias);
Remove Namespace
Remove a namespace.
$namespaceis a string representing the namespace.
$modelRegistry->removeNamespace($namespace);
Set Default Model Class
Set the default model class name.
$defaultModelClassis a string representing the default model class name.
$modelRegistry->setDefaultModelClass($defaultModelClass);
Unload
Unload a model.
$aliasis a string representing the model alias.
$modelRegistry->unload($alias);
Use
Load a shared Model instance.
$aliasis a string representing the model alias.$classAliasis a string representing the model class alias, and will default to the model alias.
$model = $modelRegistry->use($alias, $classAlias);
Model dependencies will be resolved automatically from the Container.
Models
Custom models can be created by extending the \Fyre\ORM\Model class, suffixing the class name with "Model".
To allow autoloading an instance of your model, add the the namespace to the ModelRegistry.
Delete Query
Create a new DeleteQuery.
$optionsis an array containing options for the query.aliasis a string representing the table alias, and will default to the model alias.
$query = $model->deleteQuery($options);
Get Connection
Get the Connection.
$typeis a string representing the connection type, and will default toself::WRITE.
$connection = $model->getConnection($type);
Models use ConnectionManager for database connections, and you can specify the connection to use by setting the connectionKeys property of your models, or using the setConnection method.
protected array $connectionKeys = [ self::WRITE => 'default', self::READ => 'read_replica' ];
If the self::READ key is omitted, it will fallback to the self::WRITE connection for database reads.
Insert Query
Create a new InsertQuery.
$query = $model->insertQuery();
Replace Query
Create a new ReplaceQuery.
$query = $model->replaceQuery();
Select Query
Create a new SelectQuery.
$optionsis an array containing options for the query.aliasis a string representing the table alias, and will default to the model alias.connectionTypeis a string representing the connection type, and will default toself::READ.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.
$query = $model->selectQuery($options);
Set Connection
Set the Connection.
$connectionis a Connection.$typeis a string representing the connection type, and will default toself::WRITE.
$model->setConnection($connection, $type);
Subquery
Create a new subquery SelectQuery.
$optionsis an array containing options for the query.aliasis a string representing the table alias, and will default to the model alias.connectionTypeis a string representing the connection type, and will default toself::READ.
$query = $model->subquery($options);
Update Query
Create a new UpdateQuery.
$optionsis an array containing options for the query.aliasis a string representing the table alias, and will default to the model alias.
$query = $model->updateQuery($options);
Update Batch Query
Create a new UpdateBatchQuery.
$optionsis an array containing options for the query.aliasis a string representing the table alias, and will default to the model alias.
$query = $model->updateBatchQuery($options);
Schema
Alias Field
Alias a field name.
$fieldis a string representing the field name.$aliasis a string representing the alias, and will default to the model alias.
$aliasField = $model->aliasField($field, $alias);
Get Alias
Get the model alias.
$alias = $model->getAlias();
By default, the alias will be the class alias.
Get Auto Increment Key
Get the table auto increment column.
$autoIncrementKey = $model->getAutoIncrementKey();
Get Class Alias
Get the model class alias.
$classAlias = $model->getClassAlias();
By default, the alias will be the class name or the alias used when loading the model with ModelRegistry.
Get Display Name
Get the display name.
$displayName = $model->getDisplayName();
By default, the display name will be the first column in the schema with the name of either "name", "title" or "label", or you can specify a column using the displayName property in your models.
protected string $displayName = 'display_name';
Get Primary Key
Get the primary key(s).
$primaryKeys = $model->getPrimaryKey();
Get Route Key
Get the route key.
$routeKey = $model->getRouteKey();
Get Schema
Get the schema Table.
$table = $model->getSchema();
Get Table
Get the table name.
$table = $model->getTable();
By default, the table name will be the snake case form of the model class alias, or you can specify a table name using the table property in your models.
protected string $table = 'my_table';
Set Alias
Set the model alias.
$aliasis a string representing the model alias.
$model->setAlias($alias);
Set Class Alias
Set the model class alias.
$classAliasis a string representing the model class alias.
$model->setClassAlias($classAlias);
Set Display Name
Set the display name.
$displayNameis a string representing the display name.
$model->setDisplayName($displayName);
Set Table
Set the table name.
$tableis a string representing the table name.
$model->setTable($table);
Entities
Models will use the EntityLocator to find an entity class using the model class alias.
You can map a model alias to a specific entity class using the EntityLocator.
$entityLocator->map($alias, $className);
Load Into
Load contained data into entity.
$entityis an Entity.$containis an array containing the relationships to contain.
$model->loadInto($entity, $contain);
New Empty Entity
Build a new empty Entity.
$entity = $model->newEmptyEntity();
New Entity
Build a new Entity using data.
$datais an array containing the data.$optionsis an array containing entity options.associatedis an array containing the relationships to parse, and will default to null.accessibleis an array containing accessible fields, and will default to null.guardis a boolean indicating whether to check whether the field is accessible, and will default to true.mutateis a boolean indicating whether to mutate the value, and will default to true.parseis a boolean indicating whether to parse user data, and will default to true.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.validateis a boolean indicating whether to validate user data, and will default to true.cleanis a boolean indicating whether to clean the entity, and will default to false.newis a boolean indicating whether to mark the entity as new, and will default to null.
$entity = $model->newEntity($data, $options);
New Entities
Build multiple new entities using user data.
$datais an array containing the data.$optionsis an array containing entity options.associatedis an array containing the relationships to parse, and will default to null.accessibleis an array containing accessible fields, and will default to null.guardis a boolean indicating whether to check whether the field is accessible, and will default to true.mutateis a boolean indicating whether to mutate the value, and will default to true.parseis a boolean indicating whether to parse user data, and will default to true.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.validateis a boolean indicating whether to validate user data, and will default to true.cleanis a boolean indicating whether to clean the entity, and will default to false.newis a boolean indicating whether to mark the entity as new, and will default to null.
$entities = $model->newEntities($data, $options);
Patch Entity
Update an Entity using user data.
$entityis an Entity.$datais an array containing the data.$optionsis an array containing entity options.associatedis an array containing the relationships to parse, and will default to null.accessibleis an array containing accessible fields, and will default to null.guardis a boolean indicating whether to check whether the field is accessible, and will default to true.mutateis a boolean indicating whether to mutate the value, and will default to true.parseis a boolean indicating whether to parse user data, and will default to true.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.validateis a boolean indicating whether to validate user data, and will default to true.cleanis a boolean indicating whether to clean the entity, and will default to false.newis a boolean indicating whether to mark the entity as new, and will default to null.
$model->patchEntity($entity, $data, $options);
Patch Entities
Update multiple entities using user data.
$entitiesis an array or Traversable containing the entities.$datais an array containing the data.$optionsis an array containing entity options.associatedis an array containing the relationships to parse, and will default to null.accessibleis an array containing accessible fields, and will default to null.guardis a boolean indicating whether to check whether the field is accessible, and will default to true.mutateis a boolean indicating whether to mutate the value, and will default to true.parseis a boolean indicating whether to parse user data, and will default to true.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.validateis a boolean indicating whether to validate user data, and will default to true.cleanis a boolean indicating whether to clean the entity, and will default to false.newis a boolean indicating whether to mark the entity as new, and will default to null.
$model->patchEntities($entities, $data, $options);
Query Methods
Delete
Delete an Entity.
$entityis an Entity.$optionsis an array containing delete options.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.cascadeis a boolean indicating whether to cascade deletes, and will default to true.
$result = $model->delete($entity, $options);
Delete All
Delete all rows matching conditions.
$conditionsis an array or string representing the where conditions.
$affectedRows = $model->deleteAll($conditions);
This method will not use callbacks or cascade to related data.
Delete Many
Delete multiple entities.
$entitiesis an array or Traversable containing the entities.$optionsis an array containing delete options.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.cascadeis a boolean indicating whether to cascade deletes, and will default to true.
$result = $model->deleteMany($entities, $options);
Exists
Determine whether matching rows exist.
$conditionsis an array or string representing the where conditions.
$exists = $model->exists($conditions);
Find
Create a new SelectQuery.
$datais an array containing the query data.connectionTypeis a string representing the connection type, and will default toself::READ.fieldsis an array or string representing the fields to select.containis a string or array containing the relationships to contain.joinis an array containing the tables to join.conditionsis an array or string representing the where conditions.orderByis an array or string representing the fields to order by.groupByis an array or string representing the fields to group by.havingis an array or string representing the having conditions.limitis a number indicating the query limit.offsetis a number indicating the query offset.epilogis a string representing the epilog for the query.autoFieldsis a boolean indicating whether to enable auto fields.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.
$query = $model->find($data);
Get
Retrieve a single entity.
$primaryValuesis a string, integer or array containing the primary key value(s).$datais an array containing the query data.connectionTypeis a string representing the connection type, and will default toself::READ.fieldsis an array or string representing the fields to select.containis a string or array containing the relationships to contain.joinis an array containing the tables to join.epilogis a string representing the epilog for the query.autoFieldsis a boolean indicating whether to enable auto fields.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.
$entity = $model->get($primaryValues, $data);
Resolve Route Binding
Resolve an entity from a route.
$valueis a string or integer containing the route key value.$fieldis a string representing the route key.$parentis an Entity representing the parent entity, and will default to null.
$entity = $model->resolveRouteBinding($value, $field, $parent);
Save
Save an Entity.
$entityis an Entity.$optionsis an array containing save options.checkExistsis a boolean indicating whether to check if new entities exist, and will default to true.checkRulesis a boolean indicating whether to validate the RuleSet, and will default to true.saveRelatedis a boolean indicating whether to save related data, and will default to true.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.cleanis a boolean indicating whether to clean the entity after saving, and will default to true.
$result = $model->save($entity, $options);
Save Many
Save multiple entities.
$entitiesis an array or Traversable containing the entities.$optionsis an array containing save options.checkExistsis a boolean indicating whether to check if new entities exist, and will default to true.checkRulesis a boolean indicating whether to validate the RuleSet, and will default to true.saveRelatedis a boolean indicating whether to save related data, and will default to true.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.cleanis a boolean indicating whether to clean the entity after saving, and will default to true.
$result = $model->saveMany($entities, $options);
Update All
Update all rows matching conditions.
$datais an array of values to update.$conditionsis an array or string representing the where conditions.
$affectedRows = $model->updateAll($data, $conditions);
This method will not use callbacks.
Relationship Methods
Add Relationship
Add a Relationship.
$relationshipis a Relationship.
$model->addRelationship($relationship);
Get Relationship
Get a Relationship.
$nameis a string representing the relationship name.
$relationship = $model->getRelationship($name);
Get Relationships
Get all relationships.
$relationships = $model->getRelationships();
Has Relationship
Determine whether a Relationship exists.
$nameis a string representing the relationship name.
$hasRelationship = $model->hasRelationship($name);
Remove Relationship
Remove an existing Relationship.
$nameis a string representing the relationship name.
$model->removeRelationship($name);
Behavior Methods
Add Behavior
Add a Behavior to the Model.
$nameis a string representing the behavior name.$optionsis an array containing behavior options.
$model->addBehavior($name, $options);
Get Behavior
Get a loaded Behavior.
$nameis a string representing the behavior name.
$behavior = $model->getBehavior($name);
Has Behavior
Determine whether the Model has a Behavior.
$nameis a string representing the behavior name.
$hasBehavior = $model->hasBehavior($name);
Remove Behavior
Remove a Behavior from the Model.
$nameis a string representing the behavior name.
$model->removeBehavior($name);
Validation
Get Rules
Get the model RuleSet.
$rules = $model->getRules();
You can build custom rules by specifying a buildRules method in your models.
Get Validator
Get the model Validator.
$validator = $model->getValidator();
You can build a custom validator by specifying a buildValidation method in your models.
Set Rules
Set the model RuleSet.
$rulesis a RuleSet.
$model->setRules($rules);
Set Validator
Set the model Validator.
$validatoris a Validator.
$model->setValidator($validator);
Callbacks
Callbacks can be defined in your models, allowing custom code to run or revert changes at various points during model operations.
After Delete
Execute a callback after entities are deleted.
use Fyre\Entity\Entity; use Fyre\Event\Event; public function afterDelete(Event $event, Entity $entity, array $options) {}
If the $event is stopped, the delete will not be performed and the transaction will be rolled back.
After Delete Commit
Execute a callback after entities are deleted and transaction is committed.
use Fyre\Entity\Entity; use Fyre\Event\Event; public function afterDeleteCommit(Event $event, Entity $entity, array $options) {}
After Find
Execute a callback after performing a find query.
use Fyre\ORM\Result; use Fyre\Event\Event; public function afterFind(Event $event, Result $result, array $options): Result {}
After Rules
Execute a callback after model rules are processed.
use Fyre\Entity\Entity; use Fyre\Event\Event; public function afterRules(Event $event, Entity $entity, array $options) {}
If the $event is stopped, the save will not be performed.
After Parse
Execute a callback after parsing user data into an entity.
use Fyre\Entity\Entity; use Fyre\Event\Event; public function afterParse(Event $event, Entity $entity, array $options) {}
After Save
Execute a callback after entities are saved to the database.
use Fyre\Entity\Entity; use Fyre\Event\Event; public function afterSave(Event $event, Entity $entity, array $options) {}
If the $event is stopped, the save will not be performed and the transaction will be rolled back.
After Save Commit
Execute a callback after entities are saved to the database and transaction is committed.
use Fyre\Entity\Entity; use Fyre\Event\Event; public function afterSaveCommit(Event $event, Entity $entity, array $options) {}
Before Delete
Execute a callback before entities are deleted.
use Fyre\Entity\Entity; use Fyre\Event\Event; public function beforeDelete(Event $event, Entity $entity, array $options) {}
If the $event is stopped, the delete will not be performed.
Before Find
Execute a callback before performing a find query.
use Fyre\Event\Event; use Fyre\ORM\Query; public function beforeFind(Event $event, Query $query, array $options): Query {}
Before Parse
Execute a callback before parsing user data into an entity.
use ArrayObject; use Fyre\Event\Event; public function beforeParse(Event $event, ArrayObject $data, array $options) {}
Before Rules
Before rules callback.
use Fyre\Entity\Entity; use Fyre\Event\Event; public function beforeRules(Event $event, Entity $entity, array $options) {}
If the $event is stopped, the save will not be performed.
Before Save
Execute a callback before entities are saved to the database.
use Fyre\Entity\Entity; use Fyre\Event\Event; public function beforeSave(Event $event, Entity $entity, array $options) {}
If the $event is stopped, the save will not be performed and the transaction will be rolled back.
Initialize
Initialize the model.
$model->initialize();
This method is called automatically when the model is loaded. You can define this method on your custom models to attach relationships or behaviors.
Queries
Get Model
Get the Model.
$model = $query->getModel();
Delete
The \Fyre\ORM\Queries\DeleteQuery class extends the DeleteQuery class. The table and alias will be automatically set from the Model.
$model->deleteQuery() ->where($conditions) ->execute();
Insert
The \Fyre\ORM\Queries\InsertQuery class extends the InsertQuery class. The table will be automatically set from the Model.
$model->insertQuery() ->values($values) ->execute();
Replace
The \Fyre\ORM\Queries\ReplaceQuery class extends the ReplaceQuery class. The table will be automatically set from the Model.
$model->replaceQuery() ->values($values) ->execute();
Select
The \Fyre\ORM\Queries\SelectQuery class extends the SelectQuery class, while providing several additional methods and wrappers for relationship and entity mapping. The table and alias will be automatically set from the Model, and field names will be automatically aliased.
$model->selectQuery() ->select($fields) ->where($conditions) ->execute();
All
Get the Result.
$results = $query->all();
Clear Result
Clear the buffered result.
$query->clearResult();
Contain
Set the contain relationships.
$containis a string or array containing the relationships to contain.$overwriteis a boolean indicating whether to overwrite existing contains, and will default to false.
$query->contain($contain, $overwrite);
Count
Get the result count.
$count = $query->count();
Disable Auto Fields
Disable auto fields.
$query->disableAutoFields();
Disable Buffering
Disable result buffering.
$query->disableBuffering();
Enable Auto Fields
Enable auto fields.
$query->enableAutoFields();
Enable Buffering
Enable result buffering.
$query->enableBuffering();
First
Get the first result.
$entity = $query->first();
Get Alias
Get the alias.
$alias = $query->getAlias();
Get Connection Type
Get the connection type.
$connectionType = $query->getConnectionType();
Get Contain
Get the contain array.
$contain = $query->getContain();
Get Matching
Get the matching array.
$matching = $query->getMatching();
Get Result
Get the Result.
$result = $query->getResult();
Inner Join With
INNER JOIN a relationship table.
$containis a string representing the relationships to contain.$conditionsis an array containing additional join conditions.
$query->innerJoinWith($contain, $conditions);
Left Join With
LEFT JOIN a relationship table.
$containis a string representing the relationships to contain.$conditionsis an array containing additional join conditions.
$query->leftJoinWith($contain, $conditions);
Matching
INNER JOIN a relationship table and load matching data.
$containis a string representing the relationships to contain.$conditionsis an array containing additional join conditions.
$query->matching($contain, $conditions);
The matching data will be accessible via the _matchingData property.
Not Matching
LEFT JOIN a relationship table and exclude matching rows.
$containis a string representing the relationships to contain.$conditionsis an array containing additional join conditions.
$query->notMatching($contain, $conditions);
To Array
Get the results as an array.
$array = $query->toArray();
Update
The \Fyre\ORM\Queries\UpdateQuery class extends the UpdateQuery class. The table will be automatically set from the Model.
$model->updateQuery() ->set($values) ->where($conditions) ->execute();
Get Alias
Get the alias.
$alias = $query->getAlias();
Update Batch
The \Fyre\ORM\Queries\UpdateBatchQuery class extends the UpdateBatchQuery class. The table and alias will be automatically set from the Model, and field names will be automatically aliased.
$model->updateBatchQuery() ->set($values, $keys) ->execute();
Get Alias
Get the alias.
$alias = $query->getAlias();
Results
The \Fyre\ORM\Result class wraps the ResultSet class, and acts as a proxy for the Collection class, providing additional handling for entity mapping and eager loading contained results.
Relationships
Relationships can be accessed directly as a property on the model, using the relationship name. Target model methods and properties can also be accessed directly from the relationship.
Build Joins
Build join data.
$optionsis an array containing the join options, and will default to [].
$joins = $relationship->buildJoins($options);
Find Related
Find related data for entities.
$entitiesis an array or Traversable containing the entities.$datais an array containing the query data.
$related = $relationship->findRelated($entities, $data);
Get Binding Key
Get the binding key.
$bindingKey = $relationship->getBindingKey();
Get Conditions
Get the conditions.
$conditions = $relationship->getConditions();
Get Foreign Key
Get the foreign key.
$foreignKey = $relationship->getForeignKey();
Get Join Type
Get the join type.
$joinType = $relationship->getJoinType();
Get Name
Get the relationship name.
$name = $relationship->getName();
Get Property
Get the relationship property name.
$propertyName = $relationship->getProperty();
Get Source
Get the source Model.
$source = $relationship->getSource();
Get Strategy
Get the select strategy.
$strategy = $relationship->getStrategy();
Get Target
Get the target Model.
$target = $relationship->getTarget();
Has Multiple
Determine whether the relationship has multiple related items.
$hasMultiple = $relationship->hasMultiple();
Is Dependent
Determine whether the target is dependent.
$isDependent = $relationship->isDependent();
Is Owning Side
Determine whether the source is the owning side of the relationship.
$isOwningSide = $relationship->isOwningSide();
Load Related
Load related data for entities.
$entitiesis an array or Traversable containing the entities.$datais an array containing the query data.$queryis a SelectQuery (that produced the entities), and will default to null.
$relationship->loadRelated($entities, $data);
Save Related
Save related data for an entity.
$entityis an Entity.
$result = $relationship->saveRelated($entity);
Set Binding Key
Set the binding key.
$bindingKeyis a string representing the binding key.
$relationship->setBindingKey($bindingKey);
Set Conditions
Set the conditions.
$conditionsis an array containing additional conditions.
$relationship->setConditions($conditions);
Set Dependent
Set whether the target is dependent.
$dependentis a boolean indicating whether to recursively delete related data.
$relationship->setDependent($dependent);
Set Foreign Key
Set the foreign key.
$foreignKeyis a string representing the foreign key key.
$relationship->setForeignKey($foreignKey);
Set Join Type
Set the join type.
$joinTypeis a string representing the type of join.
$relationship->setJoinType($joinType);
Set Property
Set the property name.
$propertyNameis a string representing the entity property name.
$relationship->setProperty($propertyName);
Set Source
Set the source Model.
$sourceis a Model.
$relationship->setSource($source);
Set Strategy
Set the select strategy.
$strategyis a string representing the select strategy.
$relationship->setStrategy($strategy);
Set Target
Set the target Model.
$targetis a Model.
$relationship->setTarget($target);
Unlink All
Remove related data from entities.
$entitiesis an array or Traversable containing the entities.$optionsis an array containing delete options.
$result = $relationship->unlinkAll($entities, $options);
Belongs To
$nameis a string representing the relationship name.$datais an array containing relationship data.classAliasis a string representing the target class alias, and will default to the relationship name.propertyNameis a string representing the entity property name, and will default to the snake case form of the singular relationship name.foreignKeyis a string representing the foreign key column in the current table, and will default to the snake case singular name of the target alias (suffixed with "_id").bindingKeyis a string representing the matching column in the target table, and will default to the primary key.strategymust be either "join" or "select", and will default to "join".conditionsis an array containing additional conditions.joinTypeis a string representing the type of join, and will default to "LEFT".
$model->belongsTo($name, $data);
Has Many
$nameis a string representing the relationship name.$datais an array containing relationship data.classAliasis a string representing the target class alias, and will default to the relationship name.propertyNameis a string representing the entity property name, and will default to the snake case form of the relationship name.foreignKeyis a string representing the foreign key column in the target table, and will default to the snake case singular name of the current alias (suffixed with "_id").bindingKeyis a string representing the matching column in the current table, and will default to the primary key.strategymust be either "select" or "subquery", and will default to "select".saveStrategymust be either "append" or "replace", and will default to "append".conditionsis an array containing additional conditions.sortis an array or string representing the fields to order by, and will default to null.dependentis a boolean indicating whether to recursively delete related data, and will default to false.
$model->hasMany($name, $data);
Get Save Strategy
Get the save strategy.
$saveStrategy = $relationship->getSaveStrategy();
Get Sort
Get the sort order.
$sort = $relationship->getSort();
Set Save Strategy
Set the save strategy.
$saveStrategyis a string representing the save strategy.
$relationship->setSaveStrategy($saveStrategy);
Set Sort
Set the sort order.
$sortis an array or string representing the fields to order by.
$relationship->setSort($sort);
Has One
$nameis a string representing the relationship name.$datais an array containing relationship data.classAliasis a string representing the target class alias, and will default to the relationship name.propertyNameis a string representing the entity property name, and will default to the snake case form of the singular relationship name.foreignKeyis a string representing the foreign key column in the target table, and will default to the snake case singular name of the current alias (suffixed with "_id").bindingKeyis a string representing the matching column in the current table, and will default to the primary key.strategymust be either "join" or "select", and will default to "join".conditionsis an array containing additional conditions.joinTypeis a string representing the type of join, and will default to "LEFT".dependentis a boolean indicating whether to recursively delete related data, and will default to false.
$model->hasOne($name, $data);
Many To Many
When loading results, the join table data will be accessible via the _joinData property.
$nameis a string representing the relationship name.$datais an array containing relationship data.classAliasis a string representing the target class alias, and will default to the relationship name.throughis a string representing the join alias, and will default to the concatenated form of the current alias and relationship name (sorted alphabetically).propertyNameis a string representing the entity property name, and will default to the snake case form of the relationship name.foreignKeyis a string representing the foreign key column in the join table, and will default to the snake case singular name of the current alias (suffixed with "_id").targetForeignKeyis a string representing the target foreign key column in the join table, and will default to the snake case singular name of the relationship name (suffixed with "_id").bindingKeyis a string representing the matching column in the current table, and will default to the primary key.strategymust be either "select" or "subquery", and will default to "select".saveStrategymust be either "append" or "replace", and will default to "replace".sortis an array or string representing the fields to order by, and will default to null.conditionsis an array containing additional conditions.
$model->manyToMany($name, $data);
Get Junction
Get the junction Model.
$junction = $relationship->getJunction();
Get Save Strategy
Get the save strategy.
$saveStrategy = $relationship->getSaveStrategy();
Get Sort
Get the sort order.
$sort = $relationship->getSort();
Get Source Relationship
Get the source relationship.
$sourceRelationship = $relationship->getSourceRelationship();
Get Target Foreign Key
Get the target foreign key.
$targetForeignKey = $relationship->getTargetForeignKey();
Get Target Relationship
Get the target relationship.
$targetRelationship = $relationship->getTargetRelationship();
Set Junction
Set the junction Model.
$junctionis a Model.
$relationship->setJunction($junction);
Set Save Strategy
Set the save strategy.
$saveStrategyis a string representing the save strategy.
$relationship->setSaveStrategy($saveStrategy);
Set Sort
Set the sort order.
$sortis an array or string representing the fields to order by.
$relationship->setSort($sort);
Set Target Foreign Key
Set the target foreign key.
$targetForeignKeyis a string representing the target foreign key column in the join table.
$relationship->setTargetForeignKey($targetForeignKey);
Behavior Registry
use Fyre\ORM\BehaviorRegistry;
$containeris a Container.
$behaviorRegistry = new BehaviorRegistry($container);
Autoloading
It is recommended to bind the BehaviorRegistry to the Container as a singleton.
$container->singleton(BehaviorRegistry::class);
Any dependencies will be injected automatically when loading from the Container.
$behaviorRegistry = $container->use(BehaviorRegistry::class);
Behavior Registry Methods
Add Namespace
Add a namespace for automatically loading behaviors.
$namespaceis a string representing the namespace.
$behaviorRegistry->addNamespace($namespace);
Build
Build a behavior.
$nameis a string representing the behavior name.$modelis a Model.$optionsis an array containing the behavior options, and will default to [].
$behavior = $behaviorRegistry->build($name, $model, $options);
Clear
Clear all namespaces and behaviors.
$behaviorRegistry->clear();
Find
Find a behavior class.
$nameis a string representing the behavior name.
$className = $behaviorRegistry->find($name);
Get Namespaces
Get the namespaces.
$namespaces = $behaviorRegistry->getNamespaces();
Has Namespace
Determine whether a namespace exists.
$namespaceis a string representing the namespace.
$hasNamespace = $behaviorRegistry->hasNamespace($namespace);
Remove Namespace
Remove a namespace.
$namespaceis a string representing the namespace.
$behaviorRegistry->removeNamespace($namespace);
Behaviors
Behaviors must be attached to a Model using the addBehavior method. Behavior methods can then be called directly on the model.
Custom behaviors can be created by extending \Fyre\ORM\Behavior, suffixing the class name with "Behavior", and ensuring the __construct method accepts Model as the argument (and optionally an $options array as the second parameter).
Behaviors can also include callbacks that will be executed during model operations.
Get Config
Get the configuration options.
$config = $behavior->getConfig();
Get Model
Get the Model.
$model = $behavior->getModel();
Soft Delete
$optionsis an array containing behavior options.fieldis a string representing the deleted field name, and will default to "deleted".
$model->addBehavior('SoftDelete', $options);
Find Only Deleted
Find only soft deleted records.
$datais an array containing the query data.connectionTypeis a string representing the connection type, and will default toself::READ.fieldsis an array or string representing the fields to select.containis a string or array containing the relationships to contain.joinis an array containing the tables to join.conditionsis an array or string representing the where conditions.orderByis an array or string representing the fields to order by.groupByis an array or string representing the fields to group by.havingis an array or string representing the having conditions.limitis a number indicating the query limit.offsetis a number indicating the query offset.epilogis a string representing the epilog for the query.autoFieldsis a boolean indicating whether to enable auto fields.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.
$query = $model->findOnlyDeleted($data);
Find With Deleted
Find all records including soft deleted.
$datais an array containing the query data.connectionTypeis a string representing the connection type, and will default toself::READ.fieldsis an array or string representing the fields to select.containis a string or array containing the relationships to contain.joinis an array containing the tables to join.conditionsis an array or string representing the where conditions.orderByis an array or string representing the fields to order by.groupByis an array or string representing the fields to group by.havingis an array or string representing the having conditions.limitis a number indicating the query limit.offsetis a number indicating the query offset.epilogis a string representing the epilog for the query.autoFieldsis a boolean indicating whether to enable auto fields.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.
$query = $model->findWithDeleted($data);
Purge
Delete an Entity (permanently).
$entityis an Entity.$optionsis an array containing delete options.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.cascadeis a boolean indicating whether to cascade deletes, and will default to true.
$result = $model->purge($entity, $options);
Purge Many
Delete multiple entities (permanently).
$entitiesis an array or Traversable containing the entities.$optionsis an array containing delete options.eventsis a boolean indicating whether to trigger model/behavior events, and will default to true.cascadeis a boolean indicating whether to cascade deletes, and will default to true.
$result = $model->purgeMany($entities, $options);
Restore
Restore an Entity.
$entityis an Entity.$optionsis an array containing save options.dependentsis a boolean indicating whether to restore dependents, and will default to true.
$result = $model->restore($entity, $options);
Restore Many
Restore multiple entities.
$entitiesis an array or Traversable containing the entities.$optionsis an array containing save options.dependentsis a boolean indicating whether to restore dependents, and will default to true.
$result = $model->restoreMany($entities, $options);
Timestamp
The timestamp behavior provides a simple way to automatically update created/modified timestamps when saving data via models.
$optionsis an array containing behavior options.createdFieldis a string representing the created field name, and will default to "created".modifiedFieldis a string representing the modified field name, and will default to "modified".
$model->addBehavior('Timestamp', $options);
Rules
Add
Add a rule.
$ruleis a Closure, that accepts an Entity as the first argument, and should return false if the validation failed.
$rules->add($rule);
Exists In
Create an "exists in" rule.
$fieldsis an array containing the fields.$nameis the name of the relationship.$optionsis an array containing the rule options.targetFieldsis an array containing fields to match in the target table, and will default to the primary key(s).callbackis a Closure, that accepts the SelectQuery as an argument.allowNullableNullsis a boolean indicating whether to allow nullable nulls, and will default to null.messageis a string representing the error message, and will default to the Lang value for "RuleSet.existsIn".
$rules->existsIn($fields, $name, $options);
Is Clean
Create an "is clean" rule.
$optionsis an array containing the rule options.$fieldsis an array containing the fields.messageis a string representing the error message, and will default to the Lang value for "RuleSet.isClean".
$rules->isClean($fields, $options);
Is Unique
Create an "is unique" rule.
$fieldsis an array containing the fields.$optionsis an array containing the rule options.callbackis a Closure, that accepts the SelectQuery as an argument.allowMultipleNullsis a boolean indicating whether to allow multiple nulls, and will default to true.messageis a string representing the error message, and will default to the Lang value for "RuleSet.isUnique".
$rules->isUnique($fields, $options);
Validate
Validate an Entity.
$entityis an Entity.
$rules->validate($entity);