cakephp 同时挂两个数据库
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'eschool_user',
'password' => 'password',
'database' => 'eschool',
'prefix' => '',
);
var $common = array(
'driver' => 'postgres',
'persistent' => false,
'host' => 'localhost',
'login' => 'postgres',
'password' => 'password',
'database' => 'new_tsa',
'prefix' => '',
);
}
2.开始写你这个数据库的model文件,下面是其中的一个
<?php
class PgGrade extends AppModel {
var $name = 'PgGrade';
//这句说明用的是common数据库配置
var $useDbConfig = 'common';
//自定义表名
var $useTable = 'grade';
//自定义表的主键
var $primaryKey = 'grade_id';
//字段约束
var $validate = array(
'grade_name' => array('notempty'),
);
//关系设置
var $hasMany = array(
'PgUserInfo' => array(
'className' => 'PgUserInfo',
'foreignKey' => 'grade_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
?>
3.已配置完成,你可以在crl里测试使用了,如
class LibController extends AppController {
var $name = 'Lib';
var $helpers = array('Html', 'Form');
var $layout = 'show';
var $uses = array('PgGrade');
/////////////////////////////////////////////////////////////////////////////////////////////////
function index($tea_id = null)
{
pr($this->PgGrade>find('all'));
exit;
}
}