Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0 dynamic finders #2409

Merged
merged 11 commits into from Dec 3, 2013
Merged

3.0 dynamic finders #2409

merged 11 commits into from Dec 3, 2013

Conversation

markstory
Copy link
Member

As per #2212 I've implemented dynamic finder methods. They are mostly backwards compatible with previous versions. They are intentionally less powerful, in hopes of directing people to use custom finders instead. I've also taken Andy's suggestion and made it possible to combine dynamic finders + custom finders which is kind of nice.

* Support generating conditions based on method.
* Allow first and all to be used as find types.
* Allow 'and' and 'or' operators.
Previous implementations of magic finders would just do the wrong thing.
Throwing an exceptino feels safer and more explicit.
foreach ($fields as $field) {
$conditions[$field] = array_shift($args);
}
$order = array_shift($args);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think supporting order in dynamic finders is a good idea. It makes for some really unreadable code, findByName('jose')->order(['name' => 'DESC]) is a better alternative

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll take it out then 😄

$this->assertInstanceOf('Cake\ORM\Query', $result);
$this->assertEquals(1, $result->clause('limit'));
$expected = new QueryExpression(['username' => 'garrett'], ['username' => 'string']);
$this->assertEquals($expected, $result->clause('where'));
Copy link
Member

Choose a reason for hiding this comment

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

I'm really happy to see the unexecuted query being asserted in these tests =)

Copy link
Member Author

Choose a reason for hiding this comment

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

Me too, I would feel dirty executing the query here. There are numerous integration tests elsewhere that check that queries do the right thing.

@lorenzo
Copy link
Member

lorenzo commented Dec 2, 2013

I think we should drop All from findAllBy, find() is always by default a findAll and if you actually want the first result you will need to do ->first().

@markstory
Copy link
Member Author

@lorenzo So only have findBy and findPublishedBy? We would have `findAllBy implicitly then. The special case of first would be removed though.

@lorenzo
Copy link
Member

lorenzo commented Dec 2, 2013

@markstory yeah, that is how I'd like it to work

@markstory
Copy link
Member Author

Ok, I'll get that working tonight.

lorenzo added a commit that referenced this pull request Dec 3, 2013
@lorenzo lorenzo merged commit 176758c into cakephp:3.0 Dec 3, 2013
@lorenzo lorenzo deleted the 3.0-magic-finder branch December 3, 2013 09:55
$fields = substr($method, 8);
$findType = 'all';
} else {
$fields = substr($method, strlen($matches[0]));
Copy link
Member

Choose a reason for hiding this comment

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

Late to the party, but there's no test showing that findTrollsBy works - using findAll to test this chunk where all is the default type anyway could hide a bug in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, I'll add a test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants