phalcon框架中如何实现脱离模型进行sql查询?
比如,我有表A和表B,在models/下有A.php这个model类,而没有B的,现在在A中需要对表B进行一次SQL查询,在不创建B的model类的情况下如何拿到B的数据?
------解决方案--------------------$ar = $obj->fetchAll($sql);
或
$r = $obj->fetchOne($sql);
其实 Phalcon 的类都是可以单独使用的,并不一定非要在框架中使用
比如
$connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => "localhost",
"username" => "root",
"password" => "",
"dbname" => "test"
));
//print_r(get_class_methods($connection)); //从支持的方法上看,只不过是在PDO上扩展了一些功能
$connection->query('set names gbk');
$t = $connection->fetchAll('select * from `123`', 1);
print_r($t);
Array
(
[0] => Array
(
[id] => 1
[姓名] => 张三
[型号] => A8-300
[工位] => A
[数量] => 200
[时间] => 2013-06-05 08:46:00
)
[1] => Array
(
[id] => 2
[姓名] => 李四
[型号] => A8-300
[工位] => B
[数量] => 121