CakePHP Model with Multiple JOINS - mysql

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')
));

Related

Yii2 update with multiple conditions

$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']
]);

Getting associated data in CakePHP 3.x

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.

CakePHP 3 CounterCache not updating on delete with belongsToMany SelfJoinTable

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.

Linking two models with CakeDC Search Plugin

I have a small problem that is me consuming days.
I need made a search in two diferent models in CakePHP, i'm using CakeDC Search Plugin.The models are 'Desordens' and 'Sinonimos', the association is hasMany. One 'Desorden' hasMany 'Sinonymous':
public $hasMany = array(
'Sinonimo' => array(
'className' => 'Sinonimo',
'foreignKey' => 'desorden_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
So, i follow the example in CakeDC Docs an made this :
public $filterArgs = array(
'busca' => array(
'type' => 'like',
'encode' => true,
'before' => false,
'after' => false,
'field' => array(
'Desorden.name',
'Desorden.orphanumber',
'Sinonimo.sinonimo')
)
);
What i want to do is a search where the user can insert the name,orphanumber or the synonymous.In SQL would be:
SELECT `Desorden`.`id`, `Desorden`.`name`, `Desorden`.`orphanumber`, `Desorden`.`expertlink`, `Desorden`.`tipodesordem`, `Desorden`.`descricao`, `Sinonimo`.`id`, `Sinonimo`.`sinonimo`, `Sinonimo`.`desorden_id` FROM `rederaras`.`desordens` AS `Desorden` LEFT JOIN `rederaras`.`sinonimos` AS `Sinonimo` ON (`Sinonimo`.`desorden_id` = `Desorden`.`id`) WHERE ((`Desorden`.`name` LIKE 'mult') OR (`Desorden`.`orphanumber` LIKE 'mult') OR (`Sinonimo`.`sinonimo` LIKE 'mult'))
But the table 'Sinonimo' its not being joined and i have the 1054 error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Sinonimo.sinonimo' in 'where clause'
I don't know what i missing, the controller i have configured like this:
$this->Prg->commonProcess();
$this->Desorden->recursive = 0;
$this->Paginator->settings['conditions'] = $this->Desorden->parseCriteria($this->Prg->parsedParams());
$this->set('desordens', $this->Paginator->paginate());
I think its no problem in the model or cakedc filterArg configuration, because the query is constructed in the right way.I try to use bindModel to made a join between the tables 'Desordens' and 'Sinonimos' but no success....
$this->Desorden->bindModel(array(
'hasMany' => array(
'Sinonimo' => array(
'foreignKey' => false,
'conditions' => array('Desorden.id = Sinonimo.desorden_id')
)
)
), false);
Anyone can help me to made this join ?
Thanks
I solved the problem using $belongsTo in the two Models. When i set $belongsTo the CakeDC create a Join between the tables. I don't know if this a best solution, but works for me. If this help someone.

Complex CakePHP query refactoring

I have just inherited a project and I need to re-work a bit of code so that the pagination of an infinite scroller still work properly.
Right now the code is grabbing all of the categories and their products, and listings. I need to edit it so that only categories that have, products, which have active listings are returned.
Here is the code that eventually worked:
$catData = $this->find('all',array(
'conditions' => array(
'Indexer.type' => 'Category',
'Listing.listing_id IS NOT NULL'
),
'joins' => array(
array('table' => 'peeka_product_category_link',
'alias' => 'Link',
'type' => 'LEFT',
'conditions' => array(
'Link.category_id = Category.category_id'
)
),
array('table' => 'peeka_products',
'alias' => 'Product',
'type' => 'LEFT',
'conditions' => array(
'Product.product_id = Link.product_id'
)
),
array('table' => 'peeka_listings',
'alias' => 'Listing',
'type' => 'LEFT',
'conditions' => array(
'Listing.product_id = Product.product_id',
'Listing.listing_end_date >=' => $date,
'Listing.listing_start_date <=' => $date,
"Listing.listing_status = 'Active'"
)
),
),
'order' => 'Category.category_name ASC',
'limit' => $set_limit,
'fields' => array('Category.category_id, Category.category_name, Indexer.url'),
'group' => 'Category.category_id',
'recursive' => 0
));
EDIT: Thanks to Dave this is working now and I just wanted to post it for future reference. Maybe it will help someone else.
"...only categories that have, products, which have active listings are
returned."
"...a way to combine these three queries into one, so that the first
Category->find() function retrieves all the valid data."
To retrieve data and restrict it based on fields of associated models, you'll want to use CakePHP's JOIN.
It's hard to answer your question further than that without just writing the code for you, so - give JOINs a try, then come back and ask another more specific question if you have any issues with it.