将MySQL行透视到视图中的列
我有以下表格:天数、时段、价格、品牌、车辆 价格模型分为日期和期间 数据库模式:将MySQL行透视到视图中的列,mysql,cakephp,Mysql,Cakephp,我有以下表格:天数、时段、价格、品牌、车辆 价格模型分为日期和期间 数据库模式: CREATE TABLE `days` ( `id` int(3) NOT NULL AUTO_INCREMENT, `low` int(11) NOT NULL DEFAULT '0', `high` int(11) DEFAULT '0', `brand_id` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ); CREATE TABLE
CREATE TABLE `days` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`low` int(11) NOT NULL DEFAULT '0',
`high` int(11) DEFAULT '0',
`brand_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
CREATE TABLE `periods` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`start` date NOT NULL DEFAULT '0000-00-00',
`finish` date NOT NULL DEFAULT '0000-00-00',
`brand_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
CREATE TABLE `brands` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`brand` varchar(255) NOT NULL DEFAULT '',
`slug` varchar(20) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
);
CREATE TABLE `vehicles` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`size_id` int(11) DEFAULT NULL,
`brand_id` int(11) DEFAULT NULL,
`slug` varchar(50) NOT NULL,
`model` varchar(50) DEFAULT NULL,
`image` varchar(50) DEFAULT NULL,
`short_specs` text,
`specs` text,
`publish` tinyint(1) DEFAULT '1',
PRIMARY KEY (`id`)
);
CREATE TABLE `prices` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`vehicle_id` int(3) NOT NULL DEFAULT '0',
`day_id` int(3) NOT NULL DEFAULT '0',
`period_id` int(3) NOT NULL DEFAULT '0',
`price` decimal(10,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`id`)
);
我将virtualFields用于日和时段模型:
// day.php
var $virtualFields = array(
'name' => "CONCAT(Day.low,IF(Day.high IS NULL, '+', ' to '),IF(Day.high IS NULL, '', Day.high))"
);
// period.php dates are localized using setLocale and strftime in afterFind()
var $virtualFields = array(
'name' => "CONCAT(Period.start,'|', Period.finish)"
);
function afterFind($results) {
foreach ($results as $key => $val) {
if (isset($val['Period']['name'])) {
$dates = explode('|',$val['Period']['name']);
$results[$key]['Period']['name'] = strftime('%d %b %Y',strtotime($dates[0])). ' – ' . strftime('%d %b %Y',strtotime($dates[1]));
}
}
return $results;
}
在价格模型中,如果我这样做:
function price($id) {
$this->set('prices', $this->find('all',array('conditions' => ('Price.vehicle_id' => $id))));
}
这将显示在视图中
Day Period Price
5 - 20 01 May 2011 to 31 Aug 2011 $87.00
21 to 27 01 May 2011 to 31 Aug 2011 $66.00
28+ 01 May 2011 to 31 Aug 2011 $63.00
5 - 20 01 Sep 2011 to 30 Sep 2011 $177.00
21 to 27 01 Sep 2011 to 30 Sep 2011 $165.00
28+ 01 Sep 2011 to 30 Sep 2011 $155.00
5 - 20 01 Oct 2011 to 31 Oct 2011 $322.00
21 to 27 01 Oct 2011 to 31 Oct 2011 $310.00
28+ 01 Oct 2011 to 31 Oct 2011 $300.00
日期价格
2011年5月5日至20日至2011年8月31日87.00美元
2011年5月1日至27日至2011年8月31日$66.00
2011年5月28日+1日至2011年8月31日$63.00
2011年9月5日至20日至2011年9月30日177.00美元
2011年9月21日至27日2011年9月1日至30日165.00美元
2011年9月28日+1日至2011年9月30日$155.00
2011年10月1日至2011年10月31日$322.00
2011年10月1日21日至27日至2011年10月31日310.00美元
2011年10月28日+1日至2011年10月31日$300.00
我怎样才能这样呈现数据
5 - 20 days 21 - 27 days 28+ days
01 May 2011 to 31 Aug 2011 $87.00 $66.00 $63.00
01 Sep 2011 to 30 Sep 2011 $177.00 $165.00 $155.00
01 Oct 2011 to 31 Oct 2011 $322.00 $310.00 $300.00
5-20天21-27天28天以上
2011年5月1日至2011年8月31日$87.00$66.00$63.00
2011年9月1日至2011年9月30日177.00美元165.00美元155.00美元
2011年10月1日至2011年10月31日322.00美元310.00美元300.00美元
我一直在努力学习,但在CakePHP中我很难做到这一点。如有任何建议,将不胜感激 Mark Story写了一篇题为的博客文章,很可能会对你有所帮助。关键是在查询中为所需的每一列创建一个单独的字段,然后使用带有
IF
的字段表达式,仅选择该列中应使用的行。设法解决了这个问题。这很有帮助:
$prices看起来像:
array(
array(
array(
'prices' => 'days:price,days:price,days:price,days:price...',
'period_name' => 'yyy-mm-dd|yyyy-mm-dd'
),
...
array(
array(
array(
'prices' => 'days:price,days:price,days:price,days:price...',
'period_name' => 'yyy-mm-dd|yyyy-mm-dd'
),
...