Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions en/appendices/4-1-migration-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Http
ORM
---

* ``Cake\ORM\TableRegistry`` has been deprecated. Use ``Cake\ORM\Locator\LocatorAwareTrait::getTableLocator()``
or ``Cake\Datasource\FactoryLocator::get('Table')`` to get the table locator instance instead.
* BelongsToMany associations now respect the bindingKey set in the junction table's BelongsTo association.
Previously, the target table's primary key was always used instead.
* ``Cake\ORM\AssociationCollection`` no longer lower cases association names
Expand Down
9 changes: 4 additions & 5 deletions en/console-commands/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ Modify your test case to the following snippet of code::

use Cake\Console\Command;
use Cake\I18n\FrozenTime;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\ConsoleIntegrationTestTrait;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -369,7 +368,7 @@ Modify your test case to the following snippet of code::
$this->exec('update_table Users');
$this->assertExitCode(Command::CODE_SUCCESS);

$user = TableRegistry::getTableLocator()->get('Users')->get(1);
$user = $this->getTableLocator()->get('Users')->get(1);
$this->assertSame($user->modified->timestamp, $now->timestamp);

FrozenTime::setTestNow(null);
Expand Down Expand Up @@ -451,22 +450,22 @@ incorrect response. Remove the ``testUpdateModified`` method and, add the follow
$this->exec('update_table Users', ['y']);
$this->assertExitCode(Command::CODE_SUCCESS);

$user = TableRegistry::getTableLocator()->get('Users')->get(1);
$user = $this->getTableLocator()->get('Users')->get(1);
$this->assertSame($user->modified->timestamp, $now->timestamp);

FrozenTime::setTestNow(null);
}

public function testUpdateModifiedUnsure()
{
$user = TableRegistry::getTableLocator()->get('Users')->get(1);
$user = $this->getTableLocator()->get('Users')->get(1);
$original = $user->modified->timestamp;

$this->exec('my_console best_framework', ['n']);
$this->assertExitCode(Command::CODE_ERROR);
$this->assertErrorContains('You need to be sure.');

$user = TableRegistry::getTableLocator()->get('Users')->get(1);
$user = $this->getTableLocator()->get('Users')->get(1);
$this->assertSame($original, $user->timestamp);
}

Expand Down
2 changes: 1 addition & 1 deletion en/console-commands/repl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ application's models:

bin/cake console

>>> $articles = Cake\ORM\TableRegistry::getTableLocator()->get('Articles');
>>> $articles = Cake\Datasource\FactoryLocator::get('Table')->get('Articles');
// object(Cake\ORM\Table)(
//
// )
Expand Down
7 changes: 2 additions & 5 deletions en/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -366,17 +366,14 @@ factory method::
['ElasticIndexes', 'factory']
);

The factory can be a callable or instance of ``\Cake\Datasource\Locator\LocatorInterface``.

After registering a table factory, you can use ``loadModel`` to load
instances::

// In a controller method.
$this->loadModel('Locations', 'ElasticIndex');

.. note::

The built-in ORM's TableRegistry is connected by default as the 'Table'
provider.

Paginating a Model
==================

Expand Down
6 changes: 3 additions & 3 deletions en/controllers/components/pagination.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ additional details on how to use the table registry::
],
];

// Register an additional table object to allow differentiating in pagination component
TableRegistry::getTableLocator()->setConfig('UnpublishedArticles', [
// Load an additional table object to allow differentiating in pagination component
$this->loadModel('UnpublishedArticles', [
'className' => 'App\Model\Table\ArticlesTable',
'table' => 'articles',
'entityClass' => 'App\Model\Entity\Article',
Expand All @@ -240,7 +240,7 @@ additional details on how to use the table registry::
);

$unpublishedArticles = $this->paginate(
TableRegistry::getTableLocator()->get('UnpublishedArticles')->find('all', [
$this->UnpublishedArticles->find('all', [
'scope' => 'unpublished_articles'
])->where(['published' => false])
);
Expand Down
4 changes: 2 additions & 2 deletions en/core-libraries/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ a more direct approach and only listen to the event you really need::

// You can create the following before the
// save operation, ie. config/bootstrap.php
use Cake\ORM\TableRegistry;
use Cake\Datasource\FactoryLocator;
// If sending emails
use Cake\Mailer\Email;

TableRegistry::getTableLocator()->get('ThirdPartyPlugin.Feedbacks')
FactoryLocator::get('Table')->get('ThirdPartyPlugin.Feedbacks')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you thinking that we are early enough in 4.x that we don't need the 'before x do y' type language?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally liked the method call, instead of the additional magic string.

Copy link
Member Author

@ADmad ADmad Jun 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markstory Usage like FactoryLocator::get('Table')->get('ThirdPartyPlugin.Feedbacks') works even in 3.x. So we don't need any before x do y type language.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe he means "Prior to 4.1, use ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@othercorey Yes I get that, but it's not necessary since the new usage shown actually works since 3.x.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think those should be kept, as the get('Table') part should not be leaking to the developer, mainly for usability.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Practically everywhere else the examples have been updated to use $this->getTableLocator(). This particular example shows setting an event listener in bootstrap.php for global event manager using a closure, hence using FactoryLocator directly is the only way.

->getEventManager()
->on('Model.afterSave', function($event, $entity)
{
Expand Down
15 changes: 6 additions & 9 deletions en/development/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ use that to retrieve the table name::
public $import = ['model' => 'Articles'];
}

Since this uses ``TableRegistry::getTableLocator()->get()``, it also supports plugin syntax.
It also supports plugin syntax.

You can naturally import your table definition from an existing model/table, but
have your records defined directly on the fixture as it was shown on previous
Expand Down Expand Up @@ -749,7 +749,7 @@ with the following contents::
namespace App\Test\TestCase\Model\Table;

use App\Model\Table\ArticlesTable;
use Cake\ORM\TableRegistry;
use Cake\Datasource\FactoryLocator;
use Cake\TestSuite\TestCase;

class ArticlesTableTest extends TestCase
Expand All @@ -771,7 +771,6 @@ now looks like this::
namespace App\Test\TestCase\Model\Table;

use App\Model\Table\ArticlesTable;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

class ArticlesTableTest extends TestCase
Expand All @@ -781,7 +780,7 @@ now looks like this::
public function setUp(): void
{
parent::setUp();
$this->Articles = TableRegistry::getTableLocator()->get('Articles');
$this->Articles = $this->getTableLocator()->get('Articles');
}

public function testFindPublished(): void
Expand Down Expand Up @@ -826,7 +825,7 @@ avoids issues with reflected properties that normal mocks have::

In your ``tearDown()`` method be sure to remove the mock with::

TableRegistry::clear();
$this->getTableLocator()->clear();

.. _integration-testing:

Expand Down Expand Up @@ -885,7 +884,6 @@ Create a file named **ArticlesControllerTest.php** in your

namespace App\Test\TestCase\Controller;

use Cake\ORM\TableRegistry;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -932,7 +930,7 @@ Create a file named **ArticlesControllerTest.php** in your
$this->post('/articles', $data);

$this->assertResponseSuccess();
$articles = TableRegistry::getTableLocator()->get('Articles');
$articles = $this->getTableLocator()->get('Articles');
$query = $articles->find()->where(['title' => $data['title']]);
$this->assertEquals(1, $query->count());
}
Expand Down Expand Up @@ -1617,7 +1615,6 @@ the event data::

use App\Model\Table\OrdersTable;
use Cake\Event\EventList;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

class OrdersTableTest extends TestCase
Expand All @@ -1627,7 +1624,7 @@ the event data::
public function setUp(): void
{
parent::setUp();
$this->Orders = TableRegistry::getTableLocator()->get('Orders');
$this->Orders = $this->getTableLocator()->get('Orders');
// enable event tracking
$this->Orders->getEventManager()->setEventList(new EventList());
}
Expand Down
8 changes: 4 additions & 4 deletions en/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ and retrieving user photos, finding suggestions for new friends, etc.
The model objects can be thought of as "Friend", "User", "Comment", or
"Photo". If we wanted to load some data from our ``users`` table we could do::

use Cake\ORM\TableRegistry;
use Cake\ORM\Locator\LocatorAwareTrait;

$users = TableRegistry::getTableLocator()->get('Users');
$users = $this->getTableLocator()->get('Users');
$query = $users->find();
foreach ($query as $row) {
echo $row->username;
Expand All @@ -50,9 +50,9 @@ for table and entity classes that have not yet been defined.
If we wanted to make a new user and save it (with validation) we would do
something like::

use Cake\ORM\TableRegistry;
use Cake\ORM\Locator\LocatorAwareTrait;

$users = TableRegistry::getTableLocator()->get('Users');
$users = $this->getTableLocator()->get('Users');
$user = $users->newEntity(['email' => 'mark@example.com']);
$users->save($user);

Expand Down
34 changes: 20 additions & 14 deletions en/orm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,26 @@ Quick Example

To get started you don't have to write any code. If you've followed the :ref:`CakePHP
conventions for your database tables <model-and-database-conventions>`
you can just start using the ORM. For example if we wanted to load some data from our ``articles``
table we could do::
you can just start using the ORM. For example if we wanted to load some data
from our ``articles`` table we could do::

use Cake\ORM\TableRegistry;
use Cake\ORM\Locator\LocatorAwareTrait;

$articles = TableRegistry::getTableLocator()->get('Articles');
public function someMethod()
{
$articles = $this->getTableLocator->get('Articles');

$query = $articles->find();
$query = $articles->find();

foreach ($query as $row) {
echo $row->title;
foreach ($query as $row) {
echo $row->title;
}
}

Within a static method you can use the :php:class:`~Cake\\Datasource\\FactoryLocator`
to get the table locator::

$articles = FactoryLocator::get('Table')->get('Articles');

Note that we didn't have to create any code or wire any configuration up.
The conventions in CakePHP allow us to skip some boilerplate code and allow the
Expand All @@ -57,12 +65,11 @@ associations or defining some additional methods we would add the following to

Table classes use the CamelCased version of the table name with the ``Table``
suffix as the class name. Once your class has been created you get a reference
to it using the :php:class:`~Cake\\ORM\\Locator\\TableLocator` through :php:class:`~Cake\\ORM\\TableRegistry` as before::
to it using the :php:class:`~Cake\\ORM\\Locator\\TableLocator` as before::

use Cake\ORM\TableRegistry;
use Cake\ORM\Locator\LocatorAwareTrait;

// $articles is an instance of our ArticlesTable class.
$articles = TableRegistry::getTableLocator()->get('Articles');
$articles = $this->getTableLocator()->get('Articles');

Now that we have a concrete table class, we'll probably want to use a concrete
entity class. Entity classes let you define accessor and mutator methods, define
Expand All @@ -81,10 +88,9 @@ Entities use the singular CamelCase version of the table name as their class
name by default. Now that we have created our entity class, when we
load entities from the database we'll get instances of our new Article class::

use Cake\ORM\TableRegistry;
use Cake\ORM\Locator\LocatorAwareTrait;

// $articles is an instance of ArticlesTable.
$articles = TableRegistry::getTableLocator()->get('Articles');
$articles = $this->getTableLocator()->get('Articles');
$query = $articles->find();

foreach ($query as $row) {
Expand Down
6 changes: 4 additions & 2 deletions en/orm/behaviors/tree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ hierarchical data in::

Once added, you can let CakePHP build the internal structure if the table is
already holding some rows::

$categories = TableRegistry::getTableLocator()->get('Categories');

// In a controller

$categories = $this->getTableLocator()->get('Categories');
$categories->recover();

You can verify it works by getting any row from the table and asking for the
Expand Down
8 changes: 4 additions & 4 deletions en/orm/entities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ to store in them::

The preferred way of getting new entities is using the ``newEmptyEntity()`` method from the
``Table`` objects::

use \Cake\ORM\Locator\LocatorAwareTrait;

use Cake\ORM\TableRegistry;
$article = $this->getTableLocator()->newEmptyEntity();

$article = TableRegistry::getTableLocator()->get('Articles')->newEmptyEntity();

$article = TableRegistry::getTableLocator()->get('Articles')->newEntity([
$article = $this->getTableLocator()->newEntity([
'id' => 1,
'title' => 'New Article',
'created' => new DateTime('now')
Expand Down
8 changes: 4 additions & 4 deletions en/orm/query-builder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ modified. You can also use a table's connection object to access the lower level
Query builder that does not include ORM features, if necessary. See the
:ref:`database-queries` section for more information::

use Cake\ORM\TableRegistry;
use Cake\ORM\Locator\LocatorAwareTrait;

$articles = TableRegistry::getTableLocator()->get('Articles');
$articles = $this->getTableLocator()->get('Articles');

// Start a new query.
$query = $articles->find();
Expand All @@ -40,9 +40,9 @@ Selecting Rows From A Table

::

use Cake\ORM\TableRegistry;
use Cake\ORM\Locator\LocatorAwareTrait;

$query = TableRegistry::getTableLocator()->get('Articles')->find();
$query = $this->getTableLocator()->get('Articles')->find();

foreach ($query as $article) {
debug($article->title);
Expand Down
Loading