CakePHP vertion: 3.3.11
CounterCache working on add method but not working on delete method.
SentenceTable
$this->belongsToMany('Sentences', [
'foreignKey' => 'second_sentence_id',
'targetForeignKey' => 'sentence_id',
'joinTable' => 'sentences_sentences'
]);
$this->belongsToMany('SecondSentences', [
'className' => 'Sentences',
'foreignKey' => 'sentence_id',
'targetForeignKey' => 'second_sentence_id',
'joinTable' => 'sentences_sentences'
]);
SentencesSentencesTable
$this->belongsTo('Sentences', [
'foreignKey' => 'sentence_id',
'joinType' => 'INNER'
]);
$this->belongsTo('SecondSentences', [
'className'=>'Sentences',
'foreignKey' => 'second_sentence_id',
'joinType' => 'INNER'
]);
$this->addBehavior('CounterCache', ['Sentences' => ['ver_count']]);
SentencesController Add method updating ver_count
$sentence = $this->Sentences->get($this->request->data['id']);
$sentence = $this->Sentences->patchEntity($sentence, $this->request->data);
$this->Sentences->SecondSentences->saveStrategy('append');
$this->Sentences->save($sentence);
SentencesController delete method not updating ver_count
$sentence = $this->Sentences->SecondSentences->get($this->request->data['id'],['contain'=>['Sentences']]);
if ($sentence->user_id == $this->Auth->user('id')) {
$this->Sentences->SecondSentences->delete($sentence);
$sentences = $this->Sentences->get($sentence->sentences[0]->id,['contain'=>['SecondSentences']]);
// NOW I AM USING BELOW CODE FOR UPDATING VER_COUNT.
$this->Sentences->updateAll(['ver_count'=>count($sentences->second_sentences)], ['id'=>$sentence->sentences[0]->id]);
}
How are your records deleted. Just as mentioned in cakephp documentation (CounterCache):
The counter will not be updated when you use deleteAll(), or execute SQL you have written.
And:
The CounterCache behavior works for belongsTo associations only.
Just confirm about that first.
Related
$this->localdb->createCommand()
->update(
$this->MYTable,
[
'name' => $el['new'],
'data' => $el['data'],
],
[
'userId', $this->user,
'product_id', $this->productId,
'name', $el['old'],
'created', $el['date'],
'category', $el['cat'],
]
);
I tried to use the following command to update a row using multiple where conditions, but it doesn't work for some reason. The documentation doesn't seem to cover this case and doesn't seem to be updated, because the update() method seems to match the updateAll method instead of the update method for some reason (maybe it was updated?). So I was wondering what was the correct way to do this.
You have a syntax error. Try following:
$this->localdb->createCommand()
->update(
$this->MYTable,
[
'name' => $el['new'],
'data' => $el['data'],
],
[
'userId' => $this->user,
'product_id' => $this->productId,
'name' => $el['old'],
'created' => $el['date'],
'category' => $el['cat'],
]
);
Try This updateAll query :
MYTable::updateAll([ // field to be updated in first array
'name' => $el['new'],
'data' => $el['data']
],
[ // conditions in second array
'userId' => $this->user,
'product_id' => $this->productId,
'name' => $el['old'],
'created' => $el['date'],
'category' => $el['cat']
]);
I have this setup in my Table classes
Icases table
$this->table('icases');
$this->displayField('name');
$this->primaryKey('id');
$this->belongsTo('Clients', [
'foreignKey' => 'client_id'
]);
$this->hasMany('Documents', [
'foreignKey' => 'icase_id'
]);
$this->belongsToMany('Users', [
'foreignKey' => 'icase_id',
'targetForeignKey' => 'user_id',
'joinTable' => 'icases_users'
]);
Clients Table
$this->table('clients');
$this->displayField('name');
$this->primaryKey('id');
$this->hasMany('Icases', [
'foreignKey' => 'client_id'
]);
$this->belongsToMany('Users', [
'foreignKey' => 'client_id',
'targetForeignKey' => 'user_id',
'joinTable' => 'clients_users'
]);
IcasesUsers Table
$this->table('icases_users');
$this->displayField('icase_id');
$this->primaryKey(['icase_id', 'user_id']);
$this->belongsTo('Icases', [
'foreignKey' => 'icase_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
In Icases table I have this SQL, which retrieves active and pending case data for related client and the user who is logged in. This works fine until user sets filter to include archived cases as well as active and pending which returns 107077 rows and I am getting a memory error.
Is there anyway to write this sql using contain() and get data in chunks before serving it to user, so that it cross the memory limit?
$cases_data = TableRegistry::get('icases')->find('all')
->select(['icases.id', 'icases.state', 'icases.client_id', 'icases.name', 'icases.age', 'clients.name'])
->innerJoin('icases_users', 'icases_users.icase_id = icases.id')
->where($conditions)
->innerJoin('clients', 'clients.id = icases.client_id')
->group(['icases.id'])
->order(['clients.name' => 'ASC', 'icases.name' => 'ASC'])
->execute()
->fetchAll('assoc');
You could paginate your result.
See the Paginator Helper and Pagination Component documentation.
How to cache database translations on yii2
I tried the following but not worked
'i18n' => [
'class' => Zelenin\yii\modules\I18n\components\I18N::className(),
'languages' => ['en', 'ar', 'fr'],
'sourceMessageTable' => 'source_message',
'messageTable' => 'message',
'cache' => 'cache'
],
The problem is in Zelenin i18n module. If you look at Module.php file, you can see:
$this->translations['*'] = [
'class' => DbMessageSource::className(),
'sourceMessageTable' => $this->sourceMessageTable,
'messageTable' => $this->messageTable,
'on missingTranslation' => $this->missingTranslationHandler
];
in init() method. This Code set DbMessageSource options and there are no any options about caching. Module have no any caching options too.
If you change this code to:
$this->translations['*'] = [
'class' => DbMessageSource::className(),
'sourceMessageTable' => $this->sourceMessageTable,
'messageTable' => $this->messageTable,
'enableCaching' => true,
'cachingDuration' => 3600,
'on missingTranslation' => $this->missingTranslationHandler
];
Cache will work. Some SELECT messages will gone from debug list.
The Yii documentation for i18n db messages says that the property cache only has meaning when the property cacheDuration is not zero. I suggest you set this value, so;
'i18n' => [
'class' => Zelenin\yii\modules\I18n\components\I18N::className(),
'languages' => ['en', 'ar', 'fr'],
'sourceMessageTable' => 'source_message',
'messageTable' => 'message',
'cache' => 'cache',
'cacheDuration' => 3600
],
I have a model which makes use of a table however does not really represent the table. This model shall use this table and perform multiple joins with different models, say 5 other models. Which is the best way to do this? Would you define joins separately and then merge them into one join array as seen below? Is this the best approach ?
$PlansJoin = array(
"table" => "plans_master",
"alias" => "Plan",
"type" => "INNER",
"conditions" => array(
"Plan.plan_id = KpiReporter.plan_id"
)
);
$BrandJoin = array(
"table" => "brands",
"alias" => "Brand",
"type" => "INNER",
"conditions" => array(
"Brand.brand_id = Plan.brand_id",
"OR" => array(
"Brand.brand_id" => $options["brand"],
"'all'" => $options["brand"]
)
)
);
$UserJoin = array(
"table" => "users",
"alias" => "User",
"type" => "INNER",
"conditions" => array(
"User.user_id = KpiReporter.user_id"
)
);
return $this->find("all", array(
"fields" => array_keys($this->virtualFields),
"joins" => $joins,
"group" => $group,
"conditions" => $conditions,
"order" => $order
));
Could associations be used for these complex queries ?
Why don't you use bindModel() method
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
or use containable behavior.. http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
I've used both, and I found that containable is much more easier to implement.
it will change your life, when you are using cakephp => http://book.cakephp.org/2.0/fr/core-libraries/behaviors/containable.html
When you active this "behaviors" , in your query, juste chose your join with an array "contain"
like this :
return $this->find("all", array(
"fields" => array_keys($this->virtualFields),
"joins" => $joins,
"group" => $group,
"conditions" => $conditions,
"order" => $order,
"contain"=> array('NameOfLinkedModel')
));
I am writing an application in Zend Framework 2 which is going to run from a few different subdomain, and I want to have a different module for each sub domain, to keep things tidy.
My problem is when I add more than 1 sub domain to the routing, it loses one of the sub domains.
eg: This setup works
testbed.localhost (module/Application)
a.testbed.localhost (module/A)
If I add an extra one it will the route all requests for a to the Application Index Controller
eg
testbed.localhost (module/Application), a.testbed.localhost (module/A), b.testbed.localhost (module/B)
This is the module.config.php for module/A
'router' => array(
'routes' => array(
'ads' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'a.testbed.localhost',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'A\Controller',
'controller' => 'A\Controller\A',
'action' => 'index',
),
),
And this is the route in module.config.php in module/B
'router' => array(
'routes' => array(
'ads' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'b.testbed.localhost',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'B\Controller',
'controller' => 'B\Controller\B',
'action' => 'index',
),
),
Now the namespaces are correct in both the module.config.php files, what I have noticed is the sub domain a.testbed.localhost will work if I remove the reference to it from config/application.config.php
<?php
return array(
'modules' => array(
'Application',
'A',
'B', <--- A doesn't work if B is here
),
And if I swap A & B around in the modules array above, then B will get forwarded to the Application Module and A will work. So it seems to have problems with more than 1 sub domain. Has anyone got any ideas / come across the same thing?
This happens because your route names are the same. I would try a-ads and b-ads for route names and that should resolve your situation.
In the end the configuration is getting merged together. So it's like an array, when the last array is merged it overwrites anything before it.