I have this inner join query in a paginated model called Application
Application has many AssignedExploration
AssignedExploration belongs to Exploration
Exploration belongs to ExplorationCategory
ExplorationCategory belongs to Season
My query so far is:
$session_condition = array(
'table' => 'assigned_explorations',
'alias' => 'AssignedExploration',
'type' => 'INNER',
'conditions' => array(
'AssignedExploration.application_id = Application.id',
'OR' => array(
array('AssignedExploration.label' => $this->request->named['filter_session'])
)
)
);
I would like to add another query inside the OR that will get applications which has a Season.name = fall
I've tried adding array('Season.name' => 'fall') :
$session_condition = array(
'table' => 'assigned_explorations',
'alias' => 'AssignedExploration',
'type' => 'INNER',
'conditions' => array(
'AssignedExploration.application_id = Application.id',
'OR' => array(
array('AssignedExploration.label' => $this->request->named['filter_session']),
array('Season.name' => 'fall')
)
)
);
But no luck. Seems like Season is not recognized by AssignedExploration.
I would like to nest from AssignedExploration to Exploration to ExplorationCategory and to Season
And I don't have any idea on how to make it thru inner join query via cakephp.
Thanks in advanced.
EDIT: query as of now:
INNER JOIN
`ntc_development`.`assigned_explorations` AS `AssignedExploration` ON (
`AssignedExploration`.`application_id` = `Application`.`id`
AND
`AssignedExploration`.`label` = 'fall'
)
$this->paginate['Application'] = array(
'recursive' => -1,
'joins' => array(
array(
'table' => 'assigned_explorations',
'alias' => 'AssignedExploration',
'type' => 'inner',
'foreignKey' => true,
'conditions' => array('AssignedExploration.application_id = Application.id')
),
array(
'table' => 'seasons',
'alias' => 'Season',
'type' => 'inner',
'foreignKey' => true,
'conditions' => array('Season.id = Application.season_id') # whats your foregin key
),
),
'fields' => array(),
'conditions' => array('Season.name' => 'fail'),
);
Related
I created a query, but now i need to refactor it to the CakePHP standard. Only i can't seem to get it working.
This is my working query:
$query = $this->Transfer->query( "SELECT DISTINCT emailaddresses.emailaddress
FROM transfers
JOIN emailaddresses
ON (emailaddresses.transfer_id = transfers.transfer_id AND emailaddresses.type <> 'SENDER' AND emailaddresses.received_state = 'DELIVERED')
WHERE transfers.created_user_id = $created_user_id " );
This query is working, but when i try to Cakeify it i get access denied for table emailaddress. This is the CakePHP query:
$query = $this->Transfer->find('all', array(
'fields' => 'DISTINCT Emailaddress.emailaddress ',
'conditions' => array(
'transfers.created_user_id' => $created_user_id
),
'joins' => array(
array(
'table' => 'Emailaddress.emailaddress',
'type' => 'INNER',
'conditions' => array(
'Emailaddress.transfer_id' => 'Transfer.transfer_id',
'Emailaddress.type' => 'SENDER',
'Emailaddress.received_state' => 'DELIVERED'
)
)
)
));
What do i need to change to get this query working with the Cake standards?
Your query would be like:
$query = $this->Transfer->find('all', array(
//'fields' => 'DISTINCT Emailaddress.emailaddress ',
'fields' => array('DISTINCT Emailaddress.emailaddress '),
'conditions' => array(
//'transfers.created_user_id' => $created_user_id
'Transfer.created_user_id' => $created_user_id
),
'joins' => array(
array(
//'table' => 'Emailaddress.emailaddress',
'table' => 'emailaddresses',
'alias' => 'Emailaddress', // add alias
'type' => 'INNER',
'conditions' => array(
'Emailaddress.transfer_id' => 'Transfer.transfer_id',
//'Emailaddress.type' => 'SENDER',
'Emailaddress.type <>' => 'SENDER',
'Emailaddress.received_state' => 'DELIVERED'
)
)
)
));
Read more:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables
I am new in cakephp and I have searched a lot but couldn't get the solution. My query is not returning any error but it doesn't join the zone table with the disposeTemp table.
public function disposePreview(){
$this->loadModel('DisposeTemp');
$joins = array(
array(
'table' => 'zones',
'alias' => 'Zone',
'type' => 'inner',
'conditions' => array('DisposeTemp.zone = Zone.id')
)
);
$options = array(
'conditions' => array('DisposeTemp.is_delete' => 0,'DisposeTemp.status'=>2),
'order' => array('Item.item_category_id' => 'asc'),
'joins' => $joins
);
$getDT = $this->DisposeTemp->find('all', $options);
debug($getDT);
exit;
}
Set foreign key false , as you are not having proper naming convention for foreign key in DisposeTemp table , it was supposed to be zone_id to auto form relationship.
Now in your situation Use.
$joins = array(
array(
'table' => 'zones',
'alias' => 'Zone',
'type' => 'inner',
'foreignKey' => FALSE,
'conditions' => array('DisposeTemp.zone = Zone.id')
)
);
When below code below it generate query with single quote in join query
$this->Paginator->settings = array(
'joins' => array(
array(
'table' => 'businesses_categories',
'alias' => 'BusinessesCategory',
'type' => 'LEFT',
'conditions' => array('Business.id' => 'BusinessesCategory`.`business_id'),
),
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'LEFT',
'conditions' => array('BusinessesCategory.category_id' => 'Category.id'),
),
),
'conditions' => array(
'Category.id' => 24),
'limit' => 10
);
$businesses = $this->Paginator->paginate('Business');
I have added query that's generated by above paginator query. query works fine, when i use ON (Business.id = BusinessesCategory.business_id) instead of ON (Business.id = 'BusinessesCategory.business_id')
how do i fix this. so, it does not include single quote on values
SELECT `Business`.`id`,
`Business`.`state`,
`Business`.`slug`,
`Business`.`city`,
`Business`.`suburb`,
`Business`.`user_id`,
`Business`.`business_name`,
`Business`.`business_address`,
`Business`.`business_postal`,
`Business`.`business_postal_id`,
`Business`.`business_phone`,
`Business`.`business_phone1`,
`Business`.`business_phone2`,
`Business`.`business_email`,
`Business`.`business_website`,
`Business`.`business_details`,
`Business`.`business_openinghours`,
`Business`.`business_service`,
`Business`.`business_addtionalinfo`,
`Business`.`business_lat`,
`Business`.`business_lng`,
`Business`.`identity`,
`Business`.`status`
FROM `yuldicom`.`businesses` AS `Business`
LEFT JOIN `yuldicom`.`businesses_categories` AS `BusinessesCategory` ON (`Business`.`id` = 'BusinessesCategory`.`business_id')
LEFT JOIN `yuldicom`.`categories` AS `Category` ON (`BusinessesCategory`.`category_id` = 'Category.id')
WHERE `Category`.`id` = 24 LIMIT 10
Here you go :--
$this->Paginator->settings = array(
'joins' => array(
array(
'table' => 'businesses_categories',
'alias' => 'BusinessesCategory',
'type' => 'LEFT',
'conditions' => array('Business.id=BusinessesCategory.business_id'),
),
array(
'table' => 'categories',
'alias' => 'Category',
'type' => 'LEFT',
'conditions' => array('BusinessesCategory.category_id=Category.id'),
),
),
'conditions' => array(
'Category.id' => 24),
'limit' => 10
);
$businesses = $this->Paginator->paginate('Business');
New to CakePHP etc and I'm massively confused with following problem so any guidance would be greatly appreciated. Essentially I'm having problems with hasAndBelongsToMany relationships and I don’t know if I’m going about it correctly as I’m doing most of the work inside one controller and one model.
I have a client’s pages, clients have many jobs (this works), clients belong to a client type (this works), clients also have many case studies (also works) and clients have jobs (fine).
Jobs have and belong to many disciplines – this doesn’t work, however, it appears as though the queries are being run (the SQL output in debug mode shows this, so I ran the SQL directly into MySQL - it queries fine) but Cake is not providing me the data into the clients array.
Here is the code for my Client Model and Controller.
Client.php (Model)
public $belongsTo = array(
'ClientType' => array(
'className' => 'ClientType',
'foreignKey' => 'type_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'CaseStudy' => array(
'className' => 'CaseStudy',
'foreignKey' => 'main_contractor',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Job' => array(
'className' => 'Job',
'foreignKey' => 'client_id',
'conditions' => '',
'fields' => '',
'order' => 'Job.id desc'
)
);
ClientsController.php (Controller)
$options['joins'] = array(
array('table' => 'case_studies',
'alias' => 'CaseStudy',
'type' => 'LEFT',
'conditions' => array(
'CaseStudy.client_id = Client.id',
)
),
array('table' => 'jobs',
'alias' => 'Job',
'type' => 'LEFT',
'conditions' => array(
'Job.client_id = Client.id',
)
),
array('table' => 'sectors',
'alias' => 'Sector',
'type' => 'LEFT',
'conditions' => array(
'Job.sector_id = Sector.id',
)
),
array('table' => 'disciplines_jobs',
'alias' => 'DisciplinesJobs',
'type' => 'LEFT',
'conditions' => array(
'Job.id = DisciplinesJobs.job_id',
)
),
array(
'table' => 'disciplines',
'alias' => 'Discipline',
'type' => 'LEFT',
'conditions' => array(
'DisciplinesJobs.discipline_id = Discipline.id'
)
)
);
$options['conditions'] = array('Client.id' => $client_id);
$clients = $this->Client->find('all', $options);
Output of $clients array above:
array(
(int) 0 => array(
'Client' => array(
'id' => '47',
'type_id' => '2',
'name' => 'Balfour Beatty',
'logo' => '1361786198_thumbnail_balfour beatty.jpg',
'website_url' => 'http://www.google.com',
'date_added' => '2013-02-25 10:56:38',
'date_modified' => '2013-02-25 10:56:38'
),
'ClientType' => array(
'id' => '2',
'name' => 'Constructors'
),
'CaseStudy' => array(
(int) 0 => array(
'id' => '23',
'client_id' => '47',
'sector_id' => '1',
'name' => 'Shoreham Academy',
'header_image' => '1365088787_thumbnail_1365088787_header copy.jpg',
'main_contractor' => '47',
'architect' => 'Architecture PLB',
'project_value' => '565000',
'scope_of_works' => '<table></table>',
'text' => '<p><</p>',
'type' => 'flooring',
'date_added' => '2013-04-04 11:19:47',
'date_modified' => '2013-04-04 11:19:47'
)
),
'Job' => array(
(int) 0 => array(
'id' => '1',
'client_id' => '47',
'sector_id' => '2',
'project' => 'Shoreham Academy (Project not case study)',
'date' => '2012-10-19',
'cost' => '£416k',
'quantity_of_flooring' => '7000m',
'date_added' => '2013-08-06 21:46:59',
'date_modified' => '2013-08-06 21:47:01'
)
)
)
Notice the above $clients array doesn’t have any disciplines data from the discipline_jobs table but the SQL output is which runs successfully:
SELECT `Client`.`id`, `Client`.`type_id`, `Client`.`name`, `Client`.`logo`, `Client`.`website_url`, `Client`.`date_added`, `Client`.`date_modified`, `ClientType`.`id`, `ClientType`.`name`
FROM `ar_flooring`.`clients` AS `Client`
LEFT JOIN `ar_flooring`.`case_studies` AS `CaseStudy` ON (`CaseStudy`.`client_id` = `Client`.`id`)
LEFT JOIN `ar_flooring`.`jobs` AS `Job` ON (`Job`.`client_id` = `Client`.`id`)
LEFT JOIN `ar_flooring`.`sectors` AS `Sector` ON (`Job`.`sector_id` = `Sector`.`id`)
LEFT JOIN `ar_flooring`.`disciplines_jobs` AS `DisciplinesJobs` ON (`Job`.`id` = `DisciplinesJobs`.`job_id`) LEFT JOIN `ar_flooring`.`disciplines` AS `Discipline` ON (`DisciplinesJobs`.`discipline_id` = `Discipline`.`id`)
LEFT JOIN `ar_flooring`.`client_types` AS `ClientType` ON (`Client`.`type_id` = `ClientType`.`id`)
WHERE `Client`.`id` = 47
I don’t have a discipline model or controller. I don’t have a jobs model or controller because I’m trying to do everything with the clients model and controller – is this correct?
Does anyone know why this is happening?
Hope this makes sense.
Cheers!
It appears that you want to run a
$this->Client->find('all')
at your ClientsController.
In that find all, you want to retrieve a list of Clients, the ClientType each Client belongsTo, the CaseStudy list each Client has, the Job list each Client has, and the Discipline each Job hasAndBelongsTo.
First, as a practitioner of CakePHP, I almost always avoid using the habtm relationship.
The reason is that it is almost always the case that I have extra fields in the join table. In this case, jobs_disciplines is likely to have more than just job_id and discipline_id. Maybe it will also have another field called status.
What I would do is that I will bake the JobsDiscipline model and the Discipline Model.
JobsDiscipline will belong to both Discipline and Job.
Both Discipline and Job will have many JobsDiscipline.
After this, the query to construct is basically just
$this->Client->find('all', array(
'contain' => array('ClientType', 'CaseStudy', 'Job' => array('JobsDiscipline'=>array('Discipline')))
));
This should work even if you set the recursive at your AppModel to be -1.
If this does not work, let me know again.
How to create this query on cakePHP 1.3
SELECT "Redeem_log"."benefit_id", count("Redeem_log"."benefit_id") as JUMLAH FROM "redeem_logs" "Redeem_log"
LEFT JOIN "benefits" "Benefit" ON ("Redeem_log"."benefit_id" = "Benefit"."id")
LEFT JOIN "merchants" "Merchant" ON ("Benefit"."merchant_id" = "Merchant"."id")
LEFT JOIN "merchant_types" "Merchant_type" ON ("Merchant"."merchant_type_id" = "Merchant_type"."id")
WHERE "Redeem_log"."benefit_id" IS NOT NULL AND ("Merchant_type"."merchant_type"='lokal' OR "Merchant_type"."merchant_type"='nasional') GROUP BY "Redeem_log"."benefit_id" ORDER BY "JUMLAH" DESC
I don't want to use belongsTO, hasMany or ect
If I use
var $belongsTo = array(
'Benefit' => array('className' => 'Benefit', 'foreignKey' => 'benefit_id'),
'Merchant' => array('className' => 'Merchant', 'foreignKey' => 'merchant_id')
);
left join is like :
SELECT "Redeem_log"."benefit_id", count("Redeem_log"."benefit_id") as JUMLAH
FROM "redeem_logs" "Redeem_log"
LEFT JOIN "benefits" "Benefit" ON ("Redeem_log"."benefit_id" = "Benefit"."id")
LEFT JOIN "merchants" "Merchant" ON ("Redeem_log"."merchant_id" = "Merchant"."id")
WHERE "Redeem_log"."benefit_id" IS NOT NULL GROUP BY "Redeem_log"."benefit_id"
ORDER BY "JUMLAH" DESC
I would do it this way, the params for the query looks bit scary, but when you look on it in details it makes sense:
$params = array(
'recursive' => -1,
'fields' => array('RedeemLog.benefit_id', 'COUNT(RedeemLog.benefit_id) as JUMLAH'),
'conditions' => array(
array('NOT' => array('RedeemLog.benefit_id' => null)),
array('Merchanttype.merchant_type' => array('lokal', 'nasional')),
),
'joins' => array(
array(
'table' => 'benefits',
'alias' => 'Benefit',
'type' => 'LEFT',
'conditions' => array('RedeemLog.benefit_id = Benefit.id')
),
array(
'table' => 'merchants',
'alias' => 'Merchant',
'type' => 'LEFT',
'conditions' => array('Benefit.merchant_id = Merchant.id')
),
array(
'table' => 'merchant_types',
'alias' => 'Merchanttype',
'type' => 'LEFT',
'conditions' => array('Merchant.merchant_type_id = Merchanttype.id')
),
),
'order' => 'JUMLAH DESC',
'group' => 'RedeemLog.benefit_id',
);
and finaly the cake find
$result = $this->RedeemLog->find('all', $params);
hope it works for you.
Add joins field in params parameter of MOdel::find();
like this
$params = array(
'joins'=>array(
array(
'table'=>'users',
'alias'=>'User',
'type'=>'LEFT/RIGHT/INNER',
'conditions'=>array("$this->Alias.foreignKey=User.pk")
)
)
);