【问题标题】:Connecting to mulitple databases with DMZ DataMapper使用 DMZ Data Mapper 连接到多个数据库
【发布时间】:2023-04-01 12:38:01
【问题描述】:

我正在为 CodeIgniter 使用 DMZ DataMapper ORM。

我确信可以将其配置为连接到多个数据库。我还没有找到任何文档。以前有人做过吗?

【问题讨论】:

    标签: php codeigniter codeigniter-datamapper


    【解决方案1】:

    感谢您的想法。

    在应用程序/config/database.php中

    $db['other_database'] = array(
    'hostname' => 'your_host_name',
    'username' => 'your_user_name',
    'password' => 'your_paasword',
    'database' => 'other_database_name',
    'prefix' => '',
    'dbdriver' => 'mysql',
    'pconnect' => true,
    'db_debug' => true,
    'cache_on' => false,
    'char_set' => 'utf8',
    'cachedir' => '',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'autoinit' => FALSE,
    'stricton' => FALSE,
    

    );

    class Your_new_class 扩展 Datamapper {

    public $db_params = 'other_database';
    

    }

    【讨论】:

      【解决方案2】:

      动态设置数据库参数很容易。这是我使用的:

      class DatamapperExt extends Datamapper {
      
        public function __construct($id, $db) {
          if ($db != null) {         
            // use these params as default - they don't change much. 
            $this->db_params = array_merge(array( 
              'dbdriver'  => 'mysql',
              'pconnect'  => true,
              'db_debug'  => true,
              'cache_on'  => false,
              'char_set'  => 'utf8',
              'cachedir'  => '',
              'dbcollat'  => 'utf8_general_ci',
            ), $db);
          parent::__construct($id);
        }
      }
      

      用法:

      class YourModel extends DatamapperExt {
      
      }
      
      $db = array(
        'hostname'      => 'database_host',
        'username'      => 'database_username',
        'password'      => 'database_password',
        'database'      => 'database_name',
        'prefix'        => 'CI table_prefix - if any',
      );
      
      $your_model_with_diff_db = new YourModel(NULL, $db);
      $model_with_id_diff_db   = new YourModel(12, $db);
      

      我就是这样做的...看来您必须在构造中指定 db_params 以使其动态工作,否则数据映射器会根据现有的数据库设置尝试构造/解析关系等。

      如果你的db已经设置好了,运行时不需要修改参数,也可以在config/database.php中新建一个入口:

      $db['other_database'] = array(  
        'hostname'      => 'database_host',
        'username'      => 'database_username',
        'password'      => 'database_password',
        'database'      => 'database_name',
        'prefix'        => 'CI table_prefix - if any',
      )
      

      (请注意,我上面的编码风格与 dbs 的标准 CI 不同。)

      然后使用:

      class YourModel extends DatamapperExt {
         var $db_params = 'other_database';
      }
      

      【讨论】:

        【解决方案3】:

        解决方法很简单:

        在database.php中创建另一个数据库配置并添加

        var $db_params = 'name_of_other_database';
        

        到模型。

        【讨论】:

          猜你喜欢
          • 2021-12-09
          • 2014-07-04
          • 2019-08-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-18
          相关资源
          最近更新 更多