vaffel / dao
此包最新版本(1.1)没有提供许可信息。
PHP 数据访问对象库
1.1
2015-06-15 18:59 UTC
Requires
- php: >=5.4.0
- ext-memcached: *
- psr/log: ~1.0.0
- ruflin/elastica: ~1.2.1.0
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-15 04:06:41 UTC
README
DAO库使得与数据库对象交互变得简单,并在更改数据时抽象出保持memcached和elasticsearch数据更新的麻烦。
安装
将"vafel/dao": "dev-master"
添加到项目的依赖列表中。
设置
在用DaoFactory检索DAO实例之前,必须设置DAO;
// Set up read only mysql Vaffel\Dao\DaoFactory::setServiceLayer( Vaffel\Dao\DaoFactory::SERVICE_MYSQL, Vaffel\Dao\Models\Dao\MySQL::DB_TYPE_RO, $roPdo // PDO instance, with read-only access ); // Set up read+write mysql Vaffel\Dao\DaoFactory::setServiceLayer( Vaffel\Dao\DaoFactory::SERVICE_MYSQL, $rwPdo, // PDO instance, with write access Vaffel\Dao\Models\Dao\MySQL::DB_TYPE_RW );
除了RO和RW MySQL服务外,还需要设置elasticsearch和memcached服务层;
Vaffel\Dao\DaoFactory::setServiceLayer( Vaffel\Dao\DaoFactory::SERVICE_MEMCACHED, $memcached // Memcached instance ); Vaffel\Dao\DaoFactory::setServiceLayer( Kidsa\DaoFactory::SERVICE_ELASTIC_SEARCH, $elasticClient // Elastica\Client instance );
使用方法
在初始设置后,您可以通过在DaoFactory类上静态调用getDao
来检索应用中类的DAO实例。
$userDao = DaoFactory::getDao('MyApplication\Models\User'); // Return user with id 1337 $user = $userDao->fetch(1337); // Set first name on the returned user $user->setFirstName('Kristoffer'); // Persist the updated user object $userDao->save($user);
DAO类的类名通过在模型名中最后一个反斜杠后天真地添加Dao\
来找到。