CakePHP 2.0 - virtualFields are not used on find operation with options - php

I am using CakePHP 2.0 and I have a Model with this 'virtualFields':
Country.php:
var $virtualFields = array(
'path' => "CONCAT_WS('/', dirname, basename)"
);
If I do this in a Controller which uses "User"
$users = $this->User->find('all');
the virtual field path is set.
If I use this in my Controller, which also uses "User"
$options['fields'] = array(
'DISTINCT User.*'
);
$options['joins'] = array(
array(
'table' => 'courses',
'type' => 'inner',
'conditions' => array(
'User.id = courses.user_id'
)
),
array(
'table' => 'times',
'type' => 'inner',
'conditions' => array(
'courses.id = times.course_id'
)
)
);
$options['conditions'] = array(
'times.amount > ' => 0
);
$users = $this->User->find('all', $options);
With the options, the path field is not set, of course, the SQL query does not seem to have the "CONCAT_WS('/', dirname, basename)" field included, which is included if I do the find operation without the options.
What can I do having the options, so that the virtual field is automatically included? Of course I can write in the fields options the CONCAT, but that's not very nice, especially if I change it.
Best regards.

Use group by rather than distinct and remove the fields option completely.
$options['group'] = array(
'User.*'
);
$options['joins'] = array(
array(
'table' => 'courses',
'type' => 'inner',
'conditions' => array(
'User.id = courses.user_id'
)
),
array(
'table' => 'times',
'type' => 'inner',
'conditions' => array(
'courses.id = times.course_id'
)
)
);
$options['conditions'] = array(
'times.amount > ' => 0
);
$users = $this->User->find('all', $options);

Related

Retrieve total count of paginated records along side custom limit in Cake PHP 2 paginator

I'm working inside a legacy Cake PHP 2.10 application and am utilising both the Pagination component and PaginationHelper.
I've had to override the paginateCount method to set a custom option for my paginator settings to limit the maximum number of query results which are then paginated, limit and maxLimit are not sufficient on their own - I've added totalLimit.
The problem now is if there are less results than my totalLimit, such as when filtering, it continues to display 20,000 rather than the number of actual results. I could create another query specifically for this count, but wondered if there's a quick workaround that I'm missing, here's my my Application model method:
public function paginateCount($conditions = null, $recursive = 0, $extra = array())
{
if (isset($extra['totalLimit'])) {
return $extra['totalLimit'];
}
}
And my pagination set up:
// define pagination settings
$this->Paginator->settings = array(
'Application' => array(
'paramType' => 'querystring',
'totalLimit' => 20000,
'limit' => $filters['pagination']['perPage'],
'maxLimit' => $filters['pagination']['perPage'],
'fields' => array(
'Application.*',
'ApplicationPayday.*',
'ApplicationApiLink.*',
'ApplicationResponse.*',
'AffiliateId.*',
'Redirect.*'
),
'joins' => array(
array(
'table' => 'tlp_application_paydays',
'alias' => 'ApplicationPayday',
'type' => 'LEFT',
'conditions' => array(
'ApplicationPayday.application_id = Application.id'
)
),
array(
'table' => 'tlp_application_api_links',
'alias' => 'ApplicationApiLink',
'type' => 'LEFT',
'conditions' => array(
'ApplicationApiLink.application_id = Application.id'
)
),
array(
'table' => 'tlp_application_responses',
'alias' => 'ApplicationResponse',
'type' => 'LEFT',
'conditions' => array(
'ApplicationResponse.application_id = Application.id'
)
),
array(
'table' => 'tlp_affiliate_ids',
'alias' => 'AffiliateId',
'type' => 'LEFT',
'conditions' => array(
'AffiliateId.aff_id = Application.tlp_aff_id'
)
),
array(
'table' => 'tlp_redirects',
'alias' => 'Redirect',
'type' => 'LEFT',
'conditions' => array(
'Redirect.application_id = Application.id'
)
)
),
'conditions' => $queryConditions,
'group' => array(
'Application.id'
),
'order' => array(
'Application.id' => 'desc'
),
'recursive' => -1
)
);
// run query to get applications via paginated settings
try {
$applications = $this->Paginator->paginate('Application');
} catch (\NotFoundException $e) {
$this->Session->setFlash("Page doesn't exist. We've reset your search filters and taken you to the first page.");
return $this->redirect('/payday_admin/leads/');
}
The problem is that you replace the number of total results that paginateCounts returns with $extra['totalLimit'] that you set (20.000 in this case).
The pagineCount() function should be overridden with something like in the example below. This way you will insert your total limit in the ->find('count', []) parameters and also keep the original count if your $extra['totalLimit'] parameter is not sent.
public function paginateCount($conditions, $recursive, $extra)
{
if (isset($extra['totalLimit'])) {
$limit = $extra['totalLimit'];
unset($extra['totalLimit']);
$count = $this->find('count', compact($conditions, $recursive, $limit, $extra));
return (int)$count;
}
$count = $this->find('count', compact($conditions, $recursive, $extra));
return (int)$count;
}
Your count will be limited to the maximum value of totalLimit, but will return the true count if it's lower.
If you have millions of rows with many joins I recommend caching the count. The cache key can be created by hashing the conditions and other parameters.
This is how the conditions are passed to the overridden paginateCount() from the model Class.
$this->Paginator->settings = [
'limit' => 50,
'contain' => [
...
],
'conditions' => $conditions,
'joins' => $joins,
'group' => $group
];
$this->Paginator->paginate('Model');

Single Pagination on multiple models in cakephp

My requirement is to search on 2 different tables : Vets and Clinics. There might be relation between them i.e result should fetch clinics have 'a' in name and vets having 'a' in them. Vets might be related to clinics or might not. Currently I'm doing the following. Is there any method to avoid running 2 queries which can also help me use the cakephp pagination helper?
$this->paginate = array(
'Vet' => array(
'conditions' => $conditions,
'fields' => array('Vet.id', 'Vet.name', 'Vet.professionnal_address', 'phone_number', 'Vet.email', 'Vet.type', 'Vet.latitude', 'Vet.longitude','Vet.city','Vet.clinic_id','Vet.zipcode'),
'joins' => array(
array(
'table' => 'vet_appointment_types',
'alias' => 'VetAppointmentType',
'type' => 'LEFT',
'conditions' => array(
'Vet.id = VetAppointmentType.vet_id',
)
)
),
'limit' => $limit,
'group' => array(
'Vet.id'
),
'order' => array(
'Vet.name' => 'ASC'
)
),
'Clinic' => array(
'conditions' => $conditions1,
'fields' => array('Clinic.*'),
'limit' => $limit,
'order' => array(
'Clinic.name' => 'ASC'
)
)
);
$results = $this->paginate('Vet');
$results2 = $this->paginate('Clinic');
I tried by extending the default pagination component in a non database model and using union but the database structure in a bit complex so can't use union. Also, i think implementing a temporary table based model would be an option, but since it'll be used for searching, so how exactly to go about implementing it, I'm unable to think. Any help would be +1'd ;)
As per the suggestion, I accomplished it by using database views. Below is the code. Any improvement suggestions is highly appreciated.
// \app\Model\Search.php
App::uses('ConnectionManager', 'Model');
App::uses('AppModel', 'Model');
App::uses('CakeLog', 'Log');
class Search extends AppModel {
public function __construct() {
parent::__construct();
$connection = ConnectionManager::getDataSource('default');
$return = $connection->execute("CREATE OR REPLACE VIEW `search` AS
SELECT Vet.id, (CONCAT( `Vet`.`fname` , ' ', `Vet`.`lname` )) AS name, 'vet' AS TYPE , Vet.latitude, Vet.longitude, Vet.zipcode, Vet.speciality FROM `db`.`vets` AS `Vet`
UNION
SELECT Clinic.id, Clinic.name, 'clinic' AS TYPE , Clinic.lat, Clinic.long, Clinic.zipcode, Clinic.address FROM `db`.`clinics` AS `Clinic`");
$return = ($return == true)?'Search View created successfully on '.date('Y-m-d H:i:s'):$return;
CakeLog::write('search', $return);
}
public $useTable = 'search';// This model does not use a database table
public $primaryKey = 'id'; // Define primary key
public $useDbConfig = 'default'; // Define db
}
This is the model.Everytime user searches anything, when the model is loaded in controller, view is created/replaced so that updated values can be fetched. It can be used as any other model in cakephp. It also supports virtualFields as well. To build your query for the database view you can also use the query builder which i used as follows.
$joins = array(
array(
'table' => 'vet_appointment_types',
'alias' => 'VetAppointmentType',
'type' => 'LEFT',
'conditions' => array(
'Vet.id = VetAppointmentType.vet_id',
)
)
);
$dbo = $this->Vet->getDataSource();
$subQuery = $dbo->buildStatement(
array(
'fields' => array('Vet.id', 'Vet.name'),
'table' => $dbo->fullTableName($this->Vet),
'alias' => 'Vet',
'limit' => $limit,
'group' => array(
'Vet.id'
),
'order' => array(
'Vet.name' => 'ASC'
),
'offset' => null,
'joins' => $joins,
'conditions' => $conditions
),
$this->Vet
);
$query = $subQuery;
$query .= ' UNION ';
$dbo = $this->Clinic->getDataSource();
$subQuery = $dbo->buildStatement(
array(
'fields' => array('Clinic.id', 'Clinic.name'),
'table' => $dbo->fullTableName($this->Clinic),
'alias' => 'Clinic',
'limit' => $limit,
'conditions' => $conditions1,
'limit' => $limit,
'order' => array(
'Clinic.name' => 'ASC'
),
'offset' => null
),
$this->Clinic
);
$query .= $subQuery;
print_r($query);

Join single item from related table based on min of field

I have an Item model which has the following associations:
public $hasOne = array(
'Project' => array(
'className' => 'Project',
'foreignKey' => 'item_id'
)
);
public $hasMany = array(
'ItemPic' => array(
'className' => 'ItemPic',
'foreignKey' => 'item_id',
'dependent' => false
)
);
I am wanting custom data for different views of Item. It seems like CakePHP automatically includes Project data (maybe because it is hasOne?) and does not include the ItemPic data. In the index I really don't even want the Project data... however, I do want the ItemPic data. For each Item record pulled, I want a single ItemPic record joined to it. This ItemPic should be basically ItemPic.item_id = Item.id and ORDER BY ItemPic.rank LIMIT 1.
The purpose of this is basically so that in the index I can show a list of Items and a picture associated with each item. I would like all of the images along with the Project data in the view for a single Item, but not in the list/index.
I was told I could use containable like this:
// In the model
public $actsAs = array('Containable');
// In the controller
$this->paginate = array(
'conditions' => $conditions,
'contain' => array(
'ItemPic' => array(
'fields' => array('file_name'),
'order' => 'rank',
'limit' => 1
)
)
);
The above actually works how I want... however, I was also told that doing this would cause an extra query to be ran for every single Item... which I feel I should avoid.
I tried doing this, but I get duplicate data and it doesn't attach any ItemPic data:
$this->paginate = array(
'conditions' => $conditions,
'joins' => array(
array(
'table' => 'item_pics',
'alias' => 'ItemPic',
'type' => 'LEFT',
'conditions' => array(
'ItemPic.item_id = Item.id'
),
'order' => 'rank ASC',
'limit' => 1
)
)
);
$paginated = $this->Paginator->paginate();
Can you please try this:
$this->paginate = array(
'conditions' => $conditions,
'joins' => array(
array(
'table' => 'item_pics',
'alias' => 'ItemPic',
'type' => 'LEFT',
'conditions' => array(
'ItemPic.item_id = Item.id'
),
'order' => 'rank ASC',
'limit' => 1
)
),
'fields' => array('Item.*','Project.*','ItemPic.*')
);
In the fileds section you may or may not assign "Item" , "Project" according to your requirment.
Thanks

CakePHP paginator not paginating

I have a paginator as follows:
var $paginate = array(
'order'=>array('ReleaseServer.server_environment'=>'ASC',
'ReleaseServer.server_name'=>'ASC'),
'joins'=>array(
array(
'table' => 'release_server_to_components',
'alias' => 'ReleaseServerToComponent',
'type' => 'LEFT',
'foreignKey' => false,
'conditions'=> array('ReleaseServer.id = ReleaseServerToComponent.release_server_id')
),
array(
'table' => 'release_components',
'alias' => 'ReleaseComponent',
'type' => 'LEFT',
'foreignKey' => false,
'conditions'=> array('ReleaseServerToComponent.release_component_id = ReleaseComponent.id')
)
),
'group'=>array('ReleaseServer.id'),
'contain' => array(
'ReleaseServerToComponent' => array(
'ReleaseComponent' => array(
'Release'
)
)
),
'limit' => 25,
);
Then in my controller function I do the following:
$this->set('allServers', $this->paginate('ReleaseServer', $conditions));
Where $conditions are some extra conditions for the query.
As you see above I set the limit at 25.
There are 29 records in the database however, but the page only shows 25 and the page says there is only one page.
but when a person clicks on one of the column headers to order them, some rows that were not there before magically appear, and others disappear. Why would this be?
If you need any other info please let me know
UPDATE
Now i see that the problem resides in the group part of the paginate variable, but i need it in order to make it so I do not get multiple rows of the same thing.
How do I fix that?
I solved it by adding a new paginateCount() function to my model:
function paginateCount($conditions = null, $recursive = 0, $extra = array())
{
$count = $this->find('count', array(
'fields' => 'DISTINCT ReleaseServer.id',
'conditions' => $conditions
));
return $count;
}

CakePHP find method with JOIN

Hi,
I need to do the following query using the CakePHP find method:
SELECT *
FROM `messages`
INNER JOIN users ON messages.from = users.id
WHERE messages.to = 4
ORDER BY messages.datetime DESC
Basically I have:
messages table with a Message model
users table with User model
and want to retrieve information from both tables in one query. The users.id field is the same as the messages.from field, so that's what the join is on.
I am doing it in my MessagesController so it would need to be something like:
$this->Message->find();
Thanks
There are two main ways that you can do this. One of them is the standard CakePHP way, and the other is using a custom join.
It's worth pointing out that this advice is for CakePHP 2.x, not 3.x.
The CakePHP Way
You would create a relationship with your User model and Messages Model, and use the containable behavior:
class User extends AppModel {
public $actsAs = array('Containable');
public $hasMany = array('Message');
}
class Message extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array('User');
}
You need to change the messages.from column to be messages.user_id so that cake can automagically associate the records for you.
Then you can do this from the messages controller:
$this->Message->find('all', array(
'contain' => array('User')
'conditions' => array(
'Message.to' => 4
),
'order' => 'Message.datetime DESC'
));
The (other) CakePHP way
I recommend using the first method, because it will save you a lot of time and work. The first method also does the groundwork of setting up a relationship which can be used for any number of other find calls and conditions besides the one you need now. However, cakePHP does support a syntax for defining your own joins. It would be done like this, from the MessagesController:
$this->Message->find('all', array(
'joins' => array(
array(
'table' => 'users',
'alias' => 'UserJoin',
'type' => 'INNER',
'conditions' => array(
'UserJoin.id = Message.from'
)
)
),
'conditions' => array(
'Message.to' => 4
),
'fields' => array('UserJoin.*', 'Message.*'),
'order' => 'Message.datetime DESC'
));
Note, I've left the field name messages.from the same as your current table in this example.
Using two relationships to the same model
Here is how you can do the first example using two relationships to the same model:
class User extends AppModel {
public $actsAs = array('Containable');
public $hasMany = array(
'MessagesSent' => array(
'className' => 'Message',
'foreignKey' => 'from'
)
);
public $belongsTo = array(
'MessagesReceived' => array(
'className' => 'Message',
'foreignKey' => 'to'
)
);
}
class Message extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array(
'UserFrom' => array(
'className' => 'User',
'foreignKey' => 'from'
)
);
public $hasMany = array(
'UserTo' => array(
'className' => 'User',
'foreignKey' => 'to'
)
);
}
Now you can do your find call like this:
$this->Message->find('all', array(
'contain' => array('UserFrom')
'conditions' => array(
'Message.to' => 4
),
'order' => 'Message.datetime DESC'
));
Otro example, custom Data Pagination for JOIN
CODE in Controller CakePHP 2.6 is OK:
$this->SenasaPedidosFacturadosSds->recursive = -1;
// Filtro
$where = array(
'joins' => array(
array(
'table' => 'usuarios',
'alias' => 'Usuarios',
'type' => 'INNER',
'conditions' => array(
'Usuarios.usuario_id = SenasaPedidosFacturadosSds.usuarios_id'
)
),
array(
'table' => 'senasa_pedidos',
'alias' => 'SenasaPedidos',
'type' => 'INNER',
'conditions' => array(
'SenasaPedidos.id = SenasaPedidosFacturadosSds.senasa_pedidos_id'
)
),
array(
'table' => 'clientes',
'alias' => 'Clientes',
'type' => 'INNER',
'conditions' => array(
'Clientes.id_cliente = SenasaPedidos.clientes_id'
)
),
),
'fields'=>array(
'SenasaPedidosFacturadosSds.*',
'Usuarios.usuario_id',
'Usuarios.apellido_nombre',
'Usuarios.senasa_establecimientos_id',
'Clientes.id_cliente',
'Clientes.consolida_doc_sanitaria',
'Clientes.requiere_senasa',
'Clientes.razon_social',
'SenasaPedidos.id',
'SenasaPedidos.domicilio_entrega',
'SenasaPedidos.sds',
'SenasaPedidos.pt_ptr'
),
'conditions'=>array(
'Clientes.requiere_senasa'=>1
),
'order' => 'SenasaPedidosFacturadosSds.created DESC',
'limit'=>100
);
$this->paginate = $where;
// Get datos
$data = $this->Paginator->paginate();
exit(debug($data));
OR Example 2, NOT active conditions:
$this->SenasaPedidosFacturadosSds->recursive = -1;
// Filtro
$where = array(
'joins' => array(
array(
'table' => 'usuarios',
'alias' => 'Usuarios',
'type' => 'INNER',
'conditions' => array(
'Usuarios.usuario_id = SenasaPedidosFacturadosSds.usuarios_id'
)
),
array(
'table' => 'senasa_pedidos',
'alias' => 'SenasaPedidos',
'type' => 'INNER',
'conditions' => array(
'SenasaPedidos.id = SenasaPedidosFacturadosSds.senasa_pedidos_id'
)
),
array(
'table' => 'clientes',
'alias' => 'Clientes',
'type' => 'INNER',
'conditions' => array(
'Clientes.id_cliente = SenasaPedidos.clientes_id',
'Clientes.requiere_senasa = 1'
)
),
),
'fields'=>array(
'SenasaPedidosFacturadosSds.*',
'Usuarios.usuario_id',
'Usuarios.apellido_nombre',
'Usuarios.senasa_establecimientos_id',
'Clientes.id_cliente',
'Clientes.consolida_doc_sanitaria',
'Clientes.requiere_senasa',
'Clientes.razon_social',
'SenasaPedidos.id',
'SenasaPedidos.domicilio_entrega',
'SenasaPedidos.sds',
'SenasaPedidos.pt_ptr'
),
//'conditions'=>array(
// 'Clientes.requiere_senasa'=>1
//),
'order' => 'SenasaPedidosFacturadosSds.created DESC',
'limit'=>100
);
$this->paginate = $where;
// Get datos
$data = $this->Paginator->paginate();
exit(debug($data));
$services = $this->Service->find('all', array(
'limit' =>4,
'fields' => array('Service.*','ServiceImage.*'),
'joins' => array(
array(
'table' => 'services_images',
'alias' => 'ServiceImage',
'type' => 'INNER',
'conditions' => array(
'ServiceImage.service_id' =>'Service.id'
)
),
),
)
);
It goges to array is null.

Categories