DB tools
Some useful database commands have been added to Setup plugin 3.8.0+:
bin/cake db init
This will create the (default) database, if it does not yet exist.
Setting up a fresh project or local version of it, the dev tool might not do this automatically.
Running this command avoids having to do this manually.
You can also do the same for the test DB using -c test
option.
bin/cake db reset
This will empty all tables of that (default) database, leaving all (phinx) migration tables in place (to avoid rerunning migrations).
bin/cake db wipe
This will clear out all tables of that (default) database.
I for example sometimes need that when running tests locally, having to quickly fix up the migration file and then rerunning the tests.
Since they use the already run migrations and there is no clean rollback for the tests, there is only the wipe option:
bin/cake db wipe -c test
Also sometimes the tests just got messed up somehow, then resetting quickly this way also often makes it work again.
PHP Templating
When working with templating, useful helper methods can make your life easier.
A yesNo() helper method can quickly switch between check/error font icons.
For e.g. green/red coloring for this yes/no on top, we can use a ok() helper method to wrap this.
echo $this->Templating->ok(
$this->IconSnippet->yesNo($subscription->enabled),
$subscription->enabled);
Details see cakephp-templating/releases/tag/0.2.7
Useful shortcut functions
I posted about the usefulness of wrapping HTML in a value object a while back.
Now, having to deal with use statements all over, and especially in templates can be cumbersome.
So maybe in those cases having shortcut functions can be useful.
You can define them in your bootstrap (files):
function html(string $string): \Templating\View\HtmlStringable {
return \Templating\View\Html::create($string);
}
Then in any template, you can use them (given that you use the Templating helpers/traits):
echo $this->Html->link(html(<b>Title<b>), ['action' => 'view', $entity->id]);
Note how you can prevent options (escape true/false) from being needed, which will also improve upgradability.
Complex search filters
Did you ever have to do a range filter (min/max), e.g. on prices in a table?
The Search plugin is really powerful and flexible.
Let’s give it a go:
Here we need to make one of the two a dummy one just so the value is parsed into the "args".
The other rule then does the actual filtering and then has also access to that other value.
->callback('price_max', [
'callback' => function (SelectQuery $query, array $args, $filter) {
return false;
},
])
->callback('price_min', [
'callback' => function (SelectQuery $query, array $args, $filter) {
$min = (int)$args['price_min'];
$max = (int)($args['price_max'] ?? null);
if (!$min && !$max || $max < $min) {
return false;
}
$query->where(['price >=' => $min, 'price <=' => $max]);
return true;
},
]);
I personally like to display this as a slider with two handles and two hidden form control inputs.
A demo of this can be found in the sandbox.
More filter examples here.