Professional Documents
Culture Documents
Advanced Querying With CakePHP 3
Advanced Querying With CakePHP 3
CakePHP 3
1 / 28
Agenda
1 . A short story
4 . Simple Querying
6 . Subqueries
8 . Associations Strategies
9 . Filtering by Associations
12 . Intelligent Counters
2 / 28
A Short Story
Once upon a time there was a framework that worked like ...
$this->Post->recursive = 3;
$this->Post->find('all');
,
And there was much rejoice and then much sadness .
3 / 28
The Philosophy Behind the
New ORM
,
Understanding the ideas that brought us here will help you use effectively the ORM
4 / 28
The R in ORM
,
SQL is declarative language even non programmers can get good at it
5 / 28
No intentions of being
This means :
Not going to connect to stuff that is not a relational database
No incomplete abstractions
fixed Interface.
6 / 28
Goals we had in mind
7 / 28
Clean layers
// Ok, now I am
$table = TableRegistry::get('users');
$table->find();
8 / 28
Extensible column types
system
9 / 28
Lazy (almost) all the things.
10 / 28
Should be fun to work with
$premium = $users->find('active')->find('premium')->each(function($user) {
echo $user->name;
});
$subscribers = $users->find('active')->find('subscribedToNewsletter');
$recipients = $premium->append($free)->extract('email');
11 / 28
The Setup
12 / 28
Simple Querying
{
"name": "Japan",
"population": 126714000
},
{
"name": "Thailand",
"population": 61399000
},
{
"name": "United Kingdom",
"population": 59623400
},
13 / 28
Simple Querying
14 / 28
SQL Functions
{
"average_exp": 66.48604
}
15 / 28
Subqueries
$countries->find('republics')->find('withHighLifeExp');
16 / 28
Working with associations
$this->hasOne('OfficialLanguages', [
'className' => LanguagesTable::class,
'foreignKey' => 'country_id',
'conditions' => ['OfficialLanguages.is_official' => 'T']
]);
Official Languages
17 / 28
Association strategies
18 / 28
Filtering by associations
Denmark
19 / 28
Raw SQL and Deep Assocs
,
I want to learn a new language so I need to go to a city where that language is
20 / 28
Post processing
,
Therefore you cannot return arrays or any other value
The Solution
Use formatResults()
Use mapReduce()
21 / 28
Grouping by a Property
"Africa": [
{
"name": "Angola"
},
{
"name": "Burundi"
},
{
"name": "Benin"
},
{
"name": "Burkina Faso"
}
"America": [...
22 / 28
Getting Key - Value Lists
{
"Aruba": "Dutch",
"Afghanistan": "Pashto",
"Albania": "Albaniana",
"Andorra": "Catalan",
"Netherlands Antilles": "Papiamento",
"United Arab Emirates": "Arabic",
"Argentina": "Spanish",
"Armenia": "Armenian",
...
23 / 28
Multiple Formatters
"North America": {
"Caribbean": [
{
"name": "Aruba"
},
{
"name": "Anguilla"
},
{
"name": "Netherlands Antilles"
}
...
24 / 28
Intelligent Counts
$countries->find()
->select(function($query) {
return [
'average_life_expectancy' => $query->func()->avg('life_expectancy'),
'continent'
});
->group(['continent'])
->count(); // 7
25 / 28
I have 99 problems...
'
Don t care about actual results counting in a pagination query ?
Prefer using estimates or a different logic ?
Use custom counters !
$query = $youtubeVideos->find('superComplexStuff')->counter(function() {
return Cache::read('estimated_results');
});
$query->count(); // 10000000
26 / 28
There's Plenty More!
, ...
But unfortunately little time
Result streaming
Query caching
Finder callbacks
27 / 28
Thanks for your time
Questions?
://
https . / / -
github com lorenzo cakephp3 examples
28 / 28