AttwFramework 的数据库组件

dev-master 2014-10-06 17:28 UTC

This package is auto-updated.

Last update: 2024-09-16 03:06:09 UTC


README

Total Downloads Latest Unstable Version License

AttwFramework 的数据库组件。AttwFramework

##Composer ###下载

{
    "require": {
        "attwframework/db": "dev-master"
    }
}

##支持 ###数据库

###驱动

##如何使用 ###与关系数据库连接 创建一个连接对象,将连接配置的实例传递给构造函数

use Attw\Db\Connection\PDOConnector;

$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');

连接集合

use Attw\Db\Connection\Connector\Config\MySQLConnectionConfig;
use Attw\Db\Connection\PDOConnector;
use Attw\Db\Collection as DBCollection;

$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');

$connections = DBCollection::getInstance();
$connections->add('ConnectionName', $connector);

连接器的默认方法有

  • Attw\Db\Connection\ConnectorInterface::getConnection() 返回连接
  • Attw\Db\Connection\ConnectorInterface::getDriver() 返回已连接的驱动程序
  • Attw\Db\Connection\ConnectorInterface::query($sql) 执行 SQL 查询
  • Attw\Db\Connection\ConnectorInterface::prepare($sql) 准备一个要执行的语句并返回一个语句对象
  • Attw\Db\Connection\ConnectorInterface::exec($sql) 执行 SQL 语句并返回受影响的行数
  • Attw\Db\Connection\ConnectorInterface::lastInsertId([ string $name ]) 返回最后插入行的 ID 或序列值
  • Attw\Db\Connection\ConnectorInterface::getStatement( $sql ) 返回语句类

###存储方法 用于与数据库交互的方法

Crud: ####插入数据

use Attw\Db\Connection\PDOConnector;
use Attw\Db\Storage\Storage;
use Attw\Db\Sql\MySQL;

$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');
$storage = new Storage($connector, new MySQL());
$storage->create('users', array(
    'name' => 'Gabriel Jacinto', 
    'email' => 'gamjj74@hotmail.com',
    'age' => 15,
    'gender' => 'male'
))->execute();

####更新数据

$storage->update('users', array('name' => 'Gabriel Jacinto'), array('id' => 17))
    ->execute();

####删除数据

$storage->remove('users', array('id' => 17))
    ->execute();

####选择数据

$stmt = $storage->select('users')->where(array('id' => 17));
$stmt->execute();
print_r($stmt->fetch());

####计算结果

$stmt = $storage->select('users')->where(array('id' => 17));
$stmt->execute();
$total = $stmt->rowCount();

####执行 SQL 查询

$connector->query("DELETE FROM `users` WHERE `id` = '20'");

####预处理语句

use Attw\Db\Connection\PDOConnector;
use Attw\Db\Storage\Storage;
use Attw\Db\Sql\MySQL;

$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');

$stmt = $connector->getStatement("SELECT * FROM `users` WHERE `id` = ?");
$stmt->execute(array(20));
$userData = $stmt->fetch();

###实体 实体是表示表的类。要创建实体,创建一个扩展 Attw\Db\Storage\Entity\AbstractEntity 的类。实体的配置必须放在名为 $_configs 的属性中,作为数组。如果表的列表示其他表的注册,则创建一个如示例中的 entities 索引。如果列表示日期时间列,则创建一个 datetime 索引。

namespace Your\Namespace\Entity;

use Attw\Db\Storage\Entity\AbstractEntity;

class User extends AbstractEntity
{
    protected $_configs = array(
        'table' => 'users',
        'primary_key' => 'id',
        'entities' => array(
            'category' => 'Your\Namespace\Entity\Category'
        ),
        'datetime' => array(
            'created_at',
            'updated_at'
        )
    );
    
    protected $id;
    protected $username;
    protected $email;
    protected $category;
    protected $created_at;
    protected $updated_at;
}

####插入和更新 如果指定了主键并且它存在,将执行更新。

插入

use Attw\Db\Connection\PDOConnector;
use Attw\Db\Storage\Storage;
use Attw\Db\Sql\MySQL;
use Attw\Db\Entity\EntityStorage;

$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');
$storage = new Storage($connector, new MySQL());
$entityStorage = new EntityStorage($storage);

$user = new User();
$user->username = 'Gabriel';
$user->email = 'gamjj74@hotmail.com';

$entityStorage->persist($user);

更新

$user = new User(17);//Id on contructor
$user->email = 'other@email.com';

$entityStorage->persist($user);

####删除

$user = new User(17);

$entityStorage->remove($user);

####获取 获取一个

$user = new User(21);

$data = $entityStorage->fetch($user);

获取所有

$user = new User();

$data = $entityStorage->fetchAll($user);