📜 ⬆️ ⬇️

Tips & tricks CakePHP

For those who have already managed to get acquainted with the framework.


Flexible link management


By default, when searching for all representatives of a model, Cake searches for all its associated submodels. This is often inconvenient, since the number of requests increases dramatically, like the number of useless information. This, of course, can be solved by standard means, such as $ this-> recursive in the amount of the required search sublevels (up to 3 by default), but this often does not help, because It happens that some submodels are needed, but not all. In this case, the convenient expects function, which works directly in the model, will help.
Instruction
It is very convenient to use.
For example:
$ this-> Post-> Author-> expects ();
$ this-> Post-> Category-> expects ();
$ this-> Post-> PostDetail-> expects (array ('PostExtendedDetail', 'PostAttachment'));


And you can immediately:
$ this-> Post-> expects ('Author.Author', 'Category.Category',
'PostDetail.PostExtendedDetail', 'PostDetail.PostAttachment');

Tag Cloud


It would seem a trivial task. Let's try to do it in CakePHP terms.
It is clear that a tag cloud (for example, tagcloud) should exist, as the Tags controller action, the latter will perform other tasks in the future. On the other hand, this action has no standard view, since This is the layout of the entire page. Those. global view.
Here the concept of the Element will help us. An element is a view that is called from page to page, most often in layout. What we need. Therefore, in the elements folder we create a tagcloud.ctp template with the text:
$ tags = $ this-> requestAction ('tags / cloud');

So we got an array of tags and in the same template now we can somehow equip the cloud;)
')
Now go to layouts / default.thtml and in the required place on the page insert the following line:
echo $ this-> renderElement ('tagcloud');

That's all. Now our template will be printed with a tag cloud;)

Cache in CakePHP 1.2


Yes, it's just unbelievable! In the latest version of CakePHP, working with caching was not easy, but very simple! Currently supported: APC, File, Memcache, Model and XCache. The simplest and most convenient is File. It's simple, in the tmp folder a file with the specified name is created. In addition, the creation of its own caching system is supported.
So, activate Cache. For this in the file with the core.php settings we will write:
$ cakeCache = array ('File');

or for example
$ cakeCache = array ('Memcache');

Remember that you must have Memcached installed! For experiments in Windows, you can install this version .
Further is still easier! Regardless of the name of the caching system, we use the following commands in the controller:
Cache :: write ($ key, $ data, '+1 week');

For reading
Cache :: read ($ key);

If there is no such key in the cache, then read returns false:
$ users = Cache :: read ($ key);
if ($ users === false) {
$ users = $ this-> User-> findAll ();
Cache :: write ($ key, $ users);
}

Removal or complete cleaning:
Cache :: delete ($ key);
Cache :: clear ();

Validation in CakePHP 1.2


Validation has also become easier. For some typical cases, you no longer need to use regular expressions.
A general view of the validation rules is as follows:
var $ validate = array ('field' => array ('rule' => array ('validationmethod', 'param1', 'param2')));

For example, numbers and numbers:
var $ validate = array ('username' => array ('rule' => array ('alphaNumeric')));

Field length
var $ validate = array ('username' => array ('rule' => array ('between', 3, 10)));

Comparison with number or word:
var $ validate = array ('age' => array ('rule' => array ('comparison', 'greater or equal', 18)));

Date:
var $ validate = array ('startdate' => array ('rule' => array ('date', 'ymd')));

rule can be turned into an array of similar rules to extend the check.
Read more about the rules here.

Ajax Pagination 1.2


1.2 has built-in support for paging using Ajax. Consider an example for the action of a controller:
$ this-> paginate ['User'] = array ('limit' => 10);
$ records = $ this-> paginate ('User');
$ this-> set ('records', $ records);
$ params ['url'] = $ id;
$ this-> set ('paginator_params', $ params);

Everything is very simple. Create a paginate object with a User element that we want to paginate. Then we call the paginate function. If we want to pass some additional parameters via an Ajax request, then we do this in paginator_params.
In view we do this:
<? php $ paginator-> options (array ('update' => 'comment-content', 'indicator' =>
'notification')); ?>
Navigation :
<? php echo $ paginator-> prev ('<< Back', (isset ($ paginator_params)
? $ paginator_params: null), null, array ('class' => 'disabled')); ?>

<? php echo $ paginator-> next ('Forward >>', (isset ($ paginator_params)?
$ paginator_params: null), null, array ('class' => 'disabled')); ?>
Pages:
<? php echo $ paginator-> counter (array ('separator' => 'from'));

Actually, everything. You only need to create the HTML element comment-content, which will be updated with the new Pagination page.

Months in Russian form


$ form-> month ('Model') by default displays months in English. Months are taken from the locale, because in order to display the Russian version you need to do something like this:
setlocale (LC_TIME, 'rus');

Attention: in Windows, this option will give CP1251 encoding. Therefore, if you have a UTF-8 site, then you must either have a locale on the ru_RU.UTF8 server (users Linux, hello!), Or convert this $ form-> month using iconv. Or create your $ form-> select;)

PS In the near future it is planned to open a social network written on the basis of CakePHP, therefore it is possible to open a blog that would write about everything related to the development on this framework.

Source: https://habr.com/ru/post/13146/


All Articles