Paginator->Sort() using related virtualField

Hi, is it possible to use the paginator to sort using a virtualField in a related model?

Here is a simplified example of what I'm trying to do using a fresh baked app with 1.3.1:

Database:

CREATE TABLE `customers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(45) DEFAULT NULL,
  `last_name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `customers` VALUES (1,'Alisha','Clayburn'),(2,'Jane','Smith'),(3,'Jane','Doe'),(4,'Shayne','Shover');

CREATE TABLE `assets` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `asset_no` varchar(45) DEFAULT NULL,
  `customer_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `assets` VALUES (1,'10-224',1),(2,'12-744',4),(3,'24-113',2),(4,'45-057',3),(5,'17-246',4),(6,'34-136',1);

Asset Model:

<?php
class Asset extends AppModel {
	var $name = 'Asset';
	var $displayField = 'asset_no';

	var $belongsTo = array(
		'Customer' => array(
			'className' => 'Customer',
			'foreignKey' => 'customer_id'
		)
	);
}
?>

Customer Model:

<?php
class Customer extends AppModel {
	var $name = 'Customer';
	var $displayField = 'full_name';
	var $virtualFields = array(
		'full_name' => 'CONCAT(Customer.first_name, " ", Customer.last_name)'
	);

	var $hasMany = array(
		'Asset' => array(
			'className' => 'Asset',
			'foreignKey' => 'customer_id',
			'dependent' => false
		)
	);

}
?>

Asset Controller:

<?php
class AssetsController extends AppController {

	var $name = 'Assets';

	function index() {
		$this->Asset->recursive = 0;
		$this->set('assets', $this->paginate());
	}
}
?>

Asset index.ctp:

<div class="assets index">
	<h2><?php __('Assets');?></h2>
	<table>
	<tr>
		<th><?php echo $this->Paginator->sort('id');?></th>
		<th><?php echo $this->Paginator->sort('asset_no');?></th>
		<th><?php echo $this->Paginator->sort('Customer.id');?></th>
		<th><?php echo $this->Paginator->sort('Customer.first_name');?></th>
		<th><?php echo $this->Paginator->sort('Customer.last_name');?></th>
		<th><?php echo $this->Paginator->sort('Customer.full_name');?></th>
	</tr>
	<?php foreach ($assets as $asset): ?>
	<tr>
		<td><?php echo $asset['Asset']['id']; ?></td>
		<td><?php echo $asset['Asset']['asset_no']; ?></td>
		<td><?php echo $asset['Customer']['id']; ?></td>
		<td><?php echo $asset['Customer']['first_name']; ?></td>
		<td><?php echo $asset['Customer']['last_name']; ?></td>
		<td><?php echo $asset['Customer']['full_name']; ?></td>
	</tr>
	<?php endforeach; ?>
	</table>
	<div class="paging">
		<?php echo $this->Paginator->prev();?>
	  	<?php echo $this->Paginator->numbers();?>
		<?php echo $this->Paginator->next();?>
	</div>
</div>

Sorting by real Customer fields (id, first_name, last_name) works as expected and virtual field 'full_name' is displaying properly in the table. However, sorting by Customer.full_name does not seem to generate an ORDER BY clause in the SQL. Is this not possible in 1.3.1 or am I leaving something out?

Thanks :)

Asked by LaneO, on 29/5/10

I have submitted a ticket and fix here.

LaneO - on 30/5/10

<< comments | 1 | 2

2 Answers

I've not yet tested this, but based on these two commits in release 1.3.1, I think it should work in 1.3.1.

See http:github.com/cakephp/cakephp/commit/29f2223 and http:github.com/cakephp/cakephp/commit/4915645 or the changelog at http://cakephp.lighthouseapp.com/projects/42648/changelog-1-3-1

Answered by sytzelooron 30/5/10

Confirmed. this works in 1.3.2

Answered by Ryderson 26/6/10

<< previous next >>

Your Answer

You can use Creole Wiki Syntax to format your text.

Tagged with

Rating

0

Viewed

270 times

Last Activity

on 26/6/10