欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

zend_db_table_abstract 中使用 zend_db_select 和join, Join Le

程序员文章站 2023-12-28 14:03:22
...

今天我们来讲一下关于zend_db_table_abstract中怎么使用一些查询语句,有需要的朋友可以参考一下。

代码如下 复制代码


--
-- 表的结构 `charge_logs`
--

CREATE TABLE IF NOT EXISTS `charge_logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`charge_id` int(11) NOT NULL,
`title` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

--
-- 转存表中的数据 `charge_logs`
--

INSERT INTO `charge_logs` (`id`, `charge_id`, `title`) VALUES
(1, 1, 'XXXXXXX');

--
-- 表的结构 `user_charges`
--

CREATE TABLE IF NOT EXISTS `user_charges` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) NOT NULL,
`charge_type` int(3) NOT NULL,
`charge_subtype` int(3) NOT NULL,
`charge_credits` int(3) NOT NULL,
`buy_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`valid_to` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`next_charge_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`payment` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

--
-- 转存表中的数据 `user_charges`
--

INSERT INTO `user_charges` (`id`, `user_id`, `charge_type`, `charge_subtype`, `charge_credits`, `buy_date`, `valid_to`, `next_charge_date`, `payment`) VALUES
(1, 1, 1, 1, 100, '2011-09-09 00:00:00', '2011-09-16 00:00:00', '0000-00-00 00:00:00', 'paypal');
在zend_db_table_abstract中使用 查询

$select = $this->getAdapter()->select();
$select->from(array('uc' => 'user_charges'))
->joinLeft(array('cl' => 'charge_logs'), 'uc.id = cl.charge_id', array('title'))
->where('uc.user_id=?', $id);
return $this->getAdapter()->fetchAll($select);
如果是用了Mapper,可以这样使用

$select = $this->getDbTable()->getAdapter()->select();
$select->from(array('uc' => 'user_charges'))
->joinLeft(array('cl' => 'charge_logs'), 'uc.id = cl.charge_id', array('title'))
->where('uc.user_id=?', $id);

return $this->getDbTable()->getAdapter()->fetchAll($select);
输出结果

array(1) {
[0] => array(10) {
["id"] => string(1) "1"
["user_id"] => string(1) "1"
["charge_type"] => string(1) "1"
["charge_subtype"] => string(1) "1"
["charge_credits"] => string(3) "100"
["buy_date"] => string(19) "2011-09-09 00:00:00"
["valid_to"] => string(19) "2011-09-16 00:00:00"
["next_charge_date"] => string(19) "0000-00-00 00:00:00"
["payment"] => string(6) "paypal"
["title"] => string(7) "XXXXXXX"
}
}

上一篇:

下一篇: