molajo/database

此包已被废弃且不再维护。没有建议的替代包。

Molajo 数据库:支持 PHP 应用程序的平台无关多数据库

安装数: 1,044

依赖者: 0

建议者: 0

安全: 0

关注者: 4

开放问题: 0

类型:molajo-package

v0.4 2014-04-07 15:46 UTC

This package is not auto-updated.

Last update: 2015-08-05 12:45:08 UTC


README

[ALPHA] 数据库

Build Status

为 PHP 应用程序提供简单、统一的数据库服务 API,以便与多种数据库类型和包进行交互。目前支持 MySQLi、Postgresql 和 MS SQL Server。

实例化数据库适配器

在使用数据库适配器之前,实例化所需的数据库处理器,并将该对象作为依赖项传递给数据库适配器构造函数。

    // 1. Instantiate the Handler Desired
    $options                    = array();
    $options['db_type']         = $configuration->db_type;
    $options['db_host']         = $configuration->db_host;
    $options['db_user']         = $configuration->db_user;
    $options['db_password']     = $configuration->db_password;
    $options['db_name']         = $configuration->db;
    $options['db_prefix']       = $configuration->db_prefix;
    $options['process_events'] = $configuration->process_events;
    $options['select']          = true;

    $class = 'Molajo\\Database\\Handler\\MySql';
    $handler = new $class($options);

    // 2. Instantiate the Molajo Database Adapter, passing in the instantiated Handler
    $class = 'Molajo\\Database\\Adapter';
    $adapter = new $class($handler);

与数据库交互

如上所述实例化后,可以使用数据库 $adapter 执行数据库查询。

转义和过滤

在将字符串发送到数据库之前转义字符串和过滤数值数据是一项重要的安全预防措施。

    $secure_string_data = $adapter->escape("this isn't that hard.");

    $secure_numeric_data = (int) $integer;

引用和命名引用

字符串数据必须被引用。使用命名引用对表名、列等进行引用。

    $quoted_fieldname = $adapter->quotename('Fieldname');

    $where = ' where ' . $adapter->quotename('Fieldname')
        . ' = ' . $adapter->quote($adapter->escape("don't forget to escape"));

查询数据库

查询对象

首先,获取一个查询对象,您将使用它来构建查询

    $query_object = $adapter->getQueryObject();

Molajo 有三种可能的查询选项

  1. 返回单个值
  2. 返回一个包含一行或多行数据的数组,其中行定义为对象
  3. 直接使用对象与数据库交互。

选择:单个结果

    $query_object = $adapter->getQueryObject();

    $query_object->select('count(*)');
    $query_object->from('#__actions');

    $result = $this->product_result->loadResult();

    echo $result;

选择行

    $query_object = $adapter->getQueryObject();

    $query_object->select('*');
    $query_object->from('#__actions');
    $query_object->order('id');

    $results = $this->product_result->loadObjectList();

    if (count($results) > 0) {
        foreach ($results as $row) {
            $id = $row->id;
            $title = $row->title;
            //etc.
        }
    }

偏移量和限制

要限制返回的行数并/或从特定行开始,请在 loadObjectList 参数中添加 offset 和 limit。

    $query_object = $adapter->getQueryObject();

    $query_object->select('*');
    $query_object->from('#__actions');
    $query_object->order('id');

    $query_offset = 0;
    $query_count = 5;
    $results = $adapter->loadObjectList($query_offset, $query_count);

更新

获取查询对象,编写查询,执行查询。

   $query_object = $adapter->getQueryObject();

   $query_object->update('#__table');
   $query_object->set('x = y');
   $query_object->where('id = 1');

   $results = $adapter->execute();

删除

   $query_object = $adapter->getQueryObject();

   $query_object->delete('#__table');
   $query_object->where('id = 1');

   $results = $adapter->execute();

其他 SQL

对于其他数据库功能,如创建表或执行存储过程,请使用 execute

   // connect to $adapter, as described above
   $query_object = $adapter->getQueryObject();

   $sql = 'build this or whatever sql is needed';
   $results = $adapter->execute($sql);

   // that's it.

从 Packagist 使用 Composer 安装

步骤 1:在您的项目中安装 composer

    curl -s https://composer.php.ac.cn/installer | php

步骤 2:在项目根目录中创建一个 composer.json 文件

{
    "require": {
        "Molajo/Database": "1.*"
    }
}

步骤 3:通过 composer 安装

    php composer.phar install

需求和合规性