Behaviors implementing this interface can participate in entity marshalling.
This enables behaviors to define behavior for how the properties they provide/manage should be marshalled.
An Association is a relationship established between two tables and is used to configure and customize the way interconnected records are retrieved.
A container/collection for association classes.
Contains methods for managing associations, and ordering operations around saving and deleting.
Base class for behaviors.
Behaviors allow you to simulate mixins, and create reusable blocks of application logic, that can be reused across several models. Behaviors also provide a way to hook into model callbacks and augment their behavior.
Behaviors can provide mixin like features by declaring public methods. These methods will be accessible on the tables the behavior has been added to.
function doSomething($arg1, $arg2) {
// do something
}
Would be called like $table->doSomething($arg1, $arg2);.
Behaviors can listen to any events fired on a Table. By default CakePHP provides a number of lifecycle events your behaviors can listen to:
beforeFind(Event $event, Query $query, ArrayObject $options, boolean $primary)
Fired before each find operation. By stopping the event and supplying a
return value you can bypass the find operation entirely. Any changes done
to the $query instance will be retained for the rest of the find. The
$primary parameter indicates whether or not this is the root query,
or an associated query.
buildValidator(Event $event, Validator $validator, string $name)
Fired when the validator object identified by $name is being built. You can use this
callback to add validation rules or add validation providers.
buildRules(Event $event, RulesChecker $rules)
Fired when the rules checking object for the table is being built. You can use this
callback to add more rules to the set.
beforeRules(Event $event, EntityInterface $entity, ArrayObject $options, $operation)
Fired before an entity is validated using by a rules checker. By stopping this event,
you can return the final value of the rules checking operation.
afterRules(Event $event, EntityInterface $entity, ArrayObject $options, bool $result, $operation)
Fired after the rules have been checked on the entity. By stopping this event,
you can return the final value of the rules checking operation.
beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)
Fired before each entity is saved. Stopping this event will abort the save
operation. When the event is stopped the result of the event will be returned.
afterSave(Event $event, EntityInterface $entity, ArrayObject $options)
Fired after an entity is saved.
beforeDelete(Event $event, EntityInterface $entity, ArrayObject $options)
Fired before an entity is deleted. By stopping this event you will abort
the delete operation.
afterDelete(Event $event, EntityInterface $entity, ArrayObject $options)
Fired after an entity has been deleted.
In addition to the core events, behaviors can respond to any event fired from your Table classes including custom application specific ones.
You can set the priority of a behaviors callbacks by using the
priority setting when attaching a behavior. This will set the
priority for all the callbacks a behavior provides.
Behaviors can provide finder methods that hook into a Table's
find() method. Custom finders are a great way to provide preset
queries that relate to your behavior. For example a SluggableBehavior
could provide a find('slugged') finder. Behavior finders
are implemented the same as other finders. Any method
starting with find will be setup as a finder. Your finder
methods should expect the following arguments:
findSlugged(Query $query, array $options)
BehaviorRegistry is used as a registry for loaded behaviors and handles loading and constructing behavior objects.
This class also provides method for checking and dispatching behavior methods.
Represents a single level in the associations tree to be eagerly loaded for a specific query. This contains all the information required to fetch the results from the database from an associations and all its children levels.
Exposes the methods for storing the associations that should be eager loaded for a table once a query is provided and delegates the job of creating the required joins and decorating the results so that those associations can be part of the result set.
An entity represents a single result row from a repository. It exposes the methods for retrieving and storing properties associated in this row.
Contains methods that are capable of injecting eagerly loaded associations into entities or lists of entities by using the same syntax as the EagerLoader.
Contains logic to convert array data into entities.
Useful when converting request data into entities.
Extends the base Query class to provide new methods related to association loading, automatic fields selection, automatic type casting and to wrap results into a specific iterator that will be responsible for hydrating results if required.
Represents the results obtained after executing a query for a specific table This object is responsible for correctly nesting result keys reported from the query, casting each field to the correct type and executing the extra queries required for eager loading external associations.
ORM flavoured rules checker.
Adds ORM related features to the RulesChecker class.
OOP style Save Option Builder.
This allows you to build options to save entities in a OOP style and helps you to avoid mistakes by validating the options as you build them.
Represents a single database table.
Exposes methods for retrieving data out of it, and manages the associations this table has to other tables. Multiple instances of this class can be created for the same database table with different aliases, this allows you to address your database structure in a richer and more expressive way.
The primary way to retrieve data is using Table::find(). See that method for more information.
In addition to the standard find($type) finder methods, CakePHP provides dynamic finder methods. These methods allow you to easily set basic conditions up. For example to filter users by username you would call
$query = $users->findByUsername('mark');
You can also combine conditions on multiple fields using either Or or And:
$query = $users->findByUsernameOrEmail('mark', '[email protected]');
You can use Table::updateAll() and Table::deleteAll() to do bulk updates/deletes. You should be aware that events will not be fired for bulk updates/deletes.
Table objects provide a few callbacks/events you can hook into to augment/replace find operations. Each event uses the standard event subsystem in CakePHP
beforeFind(Event $event, Query $query, ArrayObject $options, boolean $primary)
Fired before each find operation. By stopping the event and supplying a
return value you can bypass the find operation entirely. Any changes done
to the $query instance will be retained for the rest of the find. The
$primary parameter indicates whether or not this is the root query,
or an associated query.
buildValidator(Event $event, Validator $validator, string $name)
Allows listeners to modify validation rules for the provided named validator.
buildRules(Event $event, RulesChecker $rules)
Allows listeners to modify the rules checker by adding more rules.
beforeRules(Event $event, EntityInterface $entity, ArrayObject $options, string $operation)
Fired before an entity is validated using the rules checker. By stopping this event,
you can return the final value of the rules checking operation.
afterRules(Event $event, EntityInterface $entity, ArrayObject $options, bool $result, string $operation)
Fired after the rules have been checked on the entity. By stopping this event,
you can return the final value of the rules checking operation.
beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)
Fired before each entity is saved. Stopping this event will abort the save
operation. When the event is stopped the result of the event will be returned.
afterSave(Event $event, EntityInterface $entity, ArrayObject $options)
Fired after an entity is saved.
afterSaveCommit(Event $event, EntityInterface $entity, ArrayObject $options)
Fired after the transaction in which the save operation is wrapped has been committed.
It’s also triggered for non atomic saves where database operations are implicitly committed.
The event is triggered only for the primary table on which save() is directly called.
The event is not triggered if a transaction is started before calling save.
beforeDelete(Event $event, EntityInterface $entity, ArrayObject $options)
Fired before an entity is deleted. By stopping this event you will abort
the delete operation.
afterDelete(Event $event, EntityInterface $entity, ArrayObject $options)
Fired after an entity has been deleted.
Provides a registry/factory for Table objects.
This registry allows you to centralize the configuration for tables their connections and other meta-data.
You may need to configure your table objects. Using the TableLocator you can
centralize configuration. Any configuration set before instances are created
will be used when creating instances. If you modify configuration after
an instance is made, the instances will not be updated.
TableRegistry::getTableLocator()->setConfig('Users', ['table' => 'my_users']);
// Prior to 3.6.0
TableRegistry::config('Users', ['table' => 'my_users']);
Configuration data is stored per alias if you use the same table with multiple aliases you will need to set configuration multiple times.
You can fetch instances out of the registry through TableLocator::get().
One instance is stored per alias. Once an alias is populated the same
instance will always be returned. This reduces the ORM memory cost and
helps make cyclic references easier to solve.
$table = TableRegistry::getTableLocator()->get('Users', $config);
// Prior to 3.6.0
$table = TableRegistry::get('Users', $config);
Contains methods for parsing the associated tables array that is typically passed to a save operation