Cakephp 3.x CounterCache on the same model 

I have a model in a CakePHP app where I refer to the parents.
The tree behaviour seems to be an overkill for this purpose, but I needed the children's count for each random node. (For feeding this to a jstree widget through AJAX.)

My model was called ItemTypes and until I have not added the className => __CLASS__ to the belongsTo association the generated SQL query tried to refer to the parent_item_types which does not exists.


public function initialize(array $config)
{
parent::initialize($config);

$this->table('item_types';);
$this->displayField('name';);
$this->primaryKey('id';);

$this->addBehavior('Timestamp';);

$this->belongsTo('ParentItemTypes', [
'foreignKey' => 'parent_item_type_id',
'class' => 'ItemTypes',
'className' => __CLASS__
]);

$this->addBehavior(
'CounterCache',
[
'ParentItemTypes' => [
'child_item_type_count' => function ($event, $entity, $table) {
return $this->find('all', ['conditions' => ['ItemTypes.parent_item_type_id' => $entity->parent_item_type_id]])->count();
}
],
]
);
}


Many kudos to jose_zap!
[ hozzászólás ] ( 3 megtekintés ) [ 0 trackbackek ] permalink ( 3 / 57 )
i18n generator script for CakePHP 

Are you tired of running cake i18n extract and clicking over the Poedit?


#!/bin/sh
rm -rf Locale/*.pot

langs=( hun )
./Console/cake i18n extract --output ./Locale --paths ./ --merge no
for lang in "${langs[@]}"
do
:
msgmerge -U Locale/$lang/LC_MESSAGES/default.po Locale/default.pot
done


Place and run this script in your app directory of your cakephp project, and you will only need to do your translatioon with Poedit/whatever.
Enjoy.
[ hozzászólás ] [ 0 trackbackek ] permalink ( 3.1 / 160 )
When the CakePHP session does not works 

I have migrated one of my CakePHP based webapplication to an another webhosting provider, and my Session stopeed working. The setFlash messages was not displayed, and the authorization does not worked.

The solution was simple but not obvious.
The Sessionhelper sends a cookie to you browser (CAKEPHP by default) containing your session id and store your session data on the server. This storage method can be selected with the Session.defaults.

At first I have suspected the php builting session which in the default case does the session data storage on the server. After creating some minimal tests the php built in session handling seemed to be working. Later I have realized that my CAKEPHP cookie had been never sent to my browser. I have created a small test to the php's setcookie function. It worked. I have realized the following section of the manual:
Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.


After I have added a newline before my php start tag the test stopped working in the new environment, but worked on my dev box.

And yes after some code ditching I have found the malicious code:
I have left a tab in my UsersController before the php start tag...
[ hozzászólás ] ( 2 megtekintés ) [ 0 trackbackek ] permalink ( 2.9 / 128 )
CakePHP: Undefined property: View::$Session after adding helper to the AppController 

I have got the error above after I have added a helper to the AppController. If I added the similar helper to all of my Controllers, everything had worked fine. Of course it is a workaround, so I have did some investigation, and I have figured out that the Session also acts as a helper not just as a component. After adding the Session to the helpers of the AppController the problem gone away.
[ hozzászólás ] ( 4 megtekintés ) [ 0 trackbackek ] permalink ( 3 / 96 )

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |

 
számláló