cakephp条件分配数据库配置不起作用

时间:2014-02-04 07:07:10

标签: cakephp

除了$ default之外,我在database.php上有这2个配置

public $client1db = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    // LOCAL PC
    'login' => '***',
    'password' => '***',
    'database' => 'laborbaseb',
    'prefix' => ''
);      
public $client2db = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    // LOCAL PC
    'login' => '****',
    'password' => '***',
    'database' => 'laborbasec',
    'prefix' => ''
);  

下面是__construct,我们在url上进行赋值。 (例如,网址是

http://localhost/laborbase/client1 (shown upon debugging).

实际上比较'client1'时它确实比较好,它显示正确的字符串值。

public function __construct() {
        debug(Router::url(null, true));
        $url = explode('/', Router::url(null, true)); //explode('/', $this->here);
        $clientsuffix = $url[4];

        //$this->default = $this->client1db; // THIS WORKS

        if ($clientsuffix == 'client1') {
            $this->default = $this->client1db; // THIS DOES NOT WORK
            debug(' conditioned1: '.$clientsuffix); // this is shown 'client1'

        };
        if ($clientsuffix == 'client2') {
            $this->default = $this->client2db; // THIS DOES NOT WORK
            debug('conditioned2: '.$clientsuffix); // this is shown 'client2'

        };
}

要进行测试,无论如何都要运行以下查询;每个db在同一记录上具有不同的值以进行区分。它在未进行分配时返回$ default的记录,并在分配确实时返回相关记录。

public function clientLogin($id = null) {
     $this->autoRender = false;
    $this->RequestHandler->setContent('json', 'application/json');
    $options = array('conditions' => array('User.' . $this->User->primaryKey => 1));
    $clientArray = $this->User->find('first', $options);
    ob_end_clean();
    echo json_encode($clientArray);
}     

输出

等记录
{"User":{"id":"1","name":"basea","username":"b","password":"0fc9c1bac6bbc068a2e72ca95e39763e2ef68297","role":"admin","created":"2013-11-20 18:23:56","modified":"2013-11-20 18:23:56"}}       

“basea”来自$ default。 “baseb”来自$ clientdb1。 “basec”来自$ clientdb2。

当正确分配$ clientdb1时,查询将从databaseB输出记录:

{"User":{"id":"1","name":"baseb","username":"client1","password":"0fc9c1bac6bbc068a2e72ca95e39763e2ef68297","role":"admin","created":"2013-11-20 18:23:56","modified":"2013-11-20 18:23:56"}}

病情如何影响?如果超出条件(尝试切换),分配如上所述。

感谢您的帮助。

0 个答案:

没有答案