Class TableRegistry
Provides a registry/factory for Table objects.
This registry allows you to centralize the configuration for tables their connections and other meta-data.
Configuring instances
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.
Getting instances
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);
Properties summary
-
$_defaultLocatorClass protected static
string
Default LocatorInterface implementation class.
-
$_locator protected static
\Cake\ORM\Locator\LocatorInterface
LocatorInterface implementation instance.
Method Summary
-
clear() public static
Clears the registry of configuration and instances.
-
exists() public static
Check to see if an instance exists in the registry.
-
get() public static
Get a table instance from the registry.
-
getTableLocator() public static
Returns a singleton instance of LocatorInterface implementation.
-
remove() public static
Removes an instance from the registry.
-
set() public static
Set an instance.
-
setTableLocator() public static
Sets singleton instance of LocatorInterface implementation.
Method Detail
exists() public static ¶
exists(string $alias)
Check to see if an instance exists in the registry.
Parameters
-
string
$alias The alias to check for.
Returns
bool
get() public static ¶
get(string $alias, array $options)
Get a table instance from the registry.
See options specification in {@link TableLocator::get()}.
Parameters
-
string
$alias The alias name you want to get.
-
array
$options optional The options you want to build the table with.
Returns
\Cake\ORM\Table
getTableLocator() public static ¶
getTableLocator()
Returns a singleton instance of LocatorInterface implementation.
Returns
\Cake\ORM\Locator\LocatorInterface
remove() public static ¶
remove(string $alias)
Removes an instance from the registry.
Parameters
-
string
$alias The alias to remove.
set() public static ¶
set(string $alias, \Cake\ORM\Table $object)
Set an instance.
Parameters
-
string
$alias The alias to set.
-
\Cake\ORM\Table
$object The table to set.
Returns
\Cake\ORM\Table
setTableLocator() public static ¶
setTableLocator(\Cake\ORM\Locator\LocatorInterface $tableLocator)
Sets singleton instance of LocatorInterface implementation.
Parameters
-
\Cake\ORM\Locator\LocatorInterface
$tableLocator Instance of a locator to use.