DISTINCT not working with hasMany cakephp - php

I want to apply DISTINCT query in cakephp 2+ it's working fine but when I BindModel with that model it's not working ?
Here is my code please check it..
$this->User->bindModel(
array(
'hasMany' => array(
'UserPreference' => array(
'className' => 'UserPreference',
'foreignKey' => 'user_id',
'fields' => 'UserPreference.notification_status',
'conditions' => array('UserPreference.notification_status' => 1),
)
)
)
);
$data = $this->User->find('all', array('conditions' => array('app_id NOT ' => '0', 'User.status' => 1), 'fields' => array('DISTINCT User.app_id')));
pr($data); die;

just because I once happened something like this I fix it changing this:
$this->User->bindModel(
array(
'hasMany' => array(
'UserPreference' => array(
'className' => 'UserPreference',
'foreignKey' => 'user_id',
'fields' => 'UserPreference.notification_status',
'conditions' => array('UserPreference.notification_status = 1'),
)
)
)
);

Related

Cakephp 2.8.x Query access violation when i try the CakePHP way. Normal query works

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

Join query in cakephp

Hello I am new in Cakephp and I am having a hard time converting mysql queries in cakephp.I have this query which I want to convert into Cakephp syntax
SELECT *
FROM trip
JOIN countries ON trip.departure_country_id = countries.id
WHERE countries.country_name LIKE "eng%"
This is what I have tried so far
class Trip extends AppModel
{
public $useTable = 'trip';
public $primaryKey = 'trip_id';
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'fields' => array('User.user_id','User.first_name','User.last_name','User.profile_image','User.country','User.phone_no')
),
'departure_country' => array(
'className' => 'Country',
'foreignKey' => 'departure_country_id',
),
'arrival_country' => array(
'className' => 'Country',
'foreignKey' => 'arrival_country_id',
)
);
public function getLocationBasedTrips($country){
return $this->find('all', array(
'conditions' => array(
'OR' => array('Country.country_name Like' => '%'.$country.'%')
),
));
}
}
Trip Model:
$this->find('all',
array(
'joins' => array(
array(
'table' => 'countries',
'alias' => 'Country',
'type' => 'INNER',
'conditions' => array(
'Trip.departure_country_id = Country.id'
)
),
'conditions' => array(
'Country.country_name Like' => "%$country%"
)
)
);
Moved some brackets around from the previous answer. Try this:
$this->find('all', array(
'joins' => array(
array(
'table' => 'countries',
'alias' => 'Country',
'conditions' => array(
'Trip.departure_country_id = Country.id'
)
)
),
'conditions' => array(
'Country.country_name LIKE' => "%$country%"
)
)
);

cant paginate with containable

In cakephp I can't get paginate to work with containable.
I followed the docs and I can't get it to work with just 2 tables. Instead I get every associated table.
I get this error as well:
Notice (8): Indirect modification of overloaded property StudentsController::$paginate has no effect
The code works fine with a find all.
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#using-containable
$this->Student->Behaviors->load('Containable');
$this->paginate['Student'] = array(
'conditions' => array( 'student_inactive' => false,'Student.student_enq' => true ),
'limit'=>10,
'fields' => array('Student.id,last_name,first_name') ,
'contain' => array('Guardian' => array(
'fields' => array('id', 'guardian_email', 'guardian_first_name', 'guardian_last_name' ))) ,
'order' => 'Student.id'
);
$st = $this->paginate('Student');
array(
(int) 0 => array(
'Student' => array(
'id' => '233',
'student_inactive' => false,
'student_enq' => false,
'student_unallocated' => false,
'first_name' => 'Aadil',
.....
),
'TutoringType' => array(
'id' => '1',
'value' => 'Individual Tuition'
),
'Referral' => array(
'id' => '1',
'title' => 'Google - Organic',
'details' => ''
),
Try with -
$this->Paginator->settings = array(
'conditions' => array( 'student_inactive' => false,'Student.student_enq' => true ),
'limit'=>10,
'fields' => array('Student.id,last_name,first_name') ,
'contain' => array('Guardian' => array(
'fields' => array('id', 'guardian_email', 'guardian_first_name', 'guardian_last_name' ))) ,
'order' => 'Student.id'
);
$st = $this->Paginator->paginate('Student');

HABTM Cake php find

I have tables below with HABTM associations.
user, group , groups_users
group , project, projects_groups
public $hasAndBelongsToMany = array(
'Group' =>
array(
'className' => 'Group',
'joinTable' => 'groups_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'group_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => ''
)
);
I am trying to get all users, as a list for checkboxes, that are on a project using project id using query below
$users = $this->User->Group->Project->find('list',
array(
'conditions' => array('Project.id' => $this->Session->read('Projectid'))
,'contain' => array(
'User' => array( 'fields' => 'User.id', 'User.email')
)
)
);
But error below
Model "Project" is not associated with model "User"
I have actually tried different ways to get this working but cant seem to get it right, how can i achieve what i want. cheers
I achieved what i wanted with joins , is there a way to do this by using model associations, instead of the manual joins option?
$options['joins'] = array(
array('table' => 'groups_users',
'alias' => 'gu',
'type' => 'LEFT',
'conditions' => array(
'gu.user_id = User.id',
)
),
array('table' => 'projectGroups',
'alias' => 'pg',
'type' => 'LEFT',
'conditions' => array(
'pg.group_id = gu.group_id',
)
)
);
$this->User->recursive = -1;
$options['conditions'] = array(
'pg.project_id = ' => $this->Session->read('Projectid'),
'User.Active = ' => true
);
$users = $this->User->find('list', $options);

Access related models/tables to inner join query in cakephp

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

Categories