ruckuus/php-activerecord-service-provider

Silex 的 PHPActiveRecord 服务提供者

0.0.1 2014-03-19 09:42 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:39:57 UTC


README

PHP ActiveRecord 服务提供者为 Silex。

历史

我本来打算使用 [可用的 ActiveRecord 扩展] (https://github.com/RafalFilipek/ActiveRecordExtension/blob/master/ActiveRecordExtension.php),但我意识到 Silex 的后续版本中 registerNamespace() 已经被弃用。这个作品是实验性的,它只具备“工作”的最基本功能。

获取

安装 ActiveRecordServiceProvider 的推荐方法是 通过 composer

只需为您的项目创建一个 composer.json 文件

{
    "require": {
        "ruckuus/php-activerecord-service-provider": "dev-master",
        "silex/silex": "1.0.*@dev"
    }
}

参数

  • ar.model_dir - 模型文件夹所在的路径(不带尾随斜杠)
  • ar.connections - 连接数组 (name => connection)。连接示例
    • mysql://username:password@localhost/database_name
    • pgsql://username:password@localhost/development
    • sqlite://my_database.db
    • oci://username:passsword@localhost/xe
  • ar.default_connection - 默认模型连接。

注册

use Silex\Application;
use Ruckuus\Silex\ActiveRecordServiceProvider;

$app = new Application();

$app->register(new ActiveRecordServiceProvider(), array(
    'ar.model_dir' => __DIR__ . '/App/Model',
    'ar.connections' =>  array ('development' => 'mysql://root@localhost/database_name'),
    'ar.default_connection' => 'development',
));

用法

__DIR__ . '/app/Model' 中创建您的模型。例如。

namespace App\Model;

class User extends \ActiveRecord\Model {
    static $has_many = array (
        array('problem'),
        array('luck')
    )
}

在您的应用程序中

use App\Model\User;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

class UserProvider implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $user = User::find_by_username(strtolower($username));
        
        if ($user->dirty_attributes()) {
            throw new UnsupportedUserException(sprintf('Bad credentials for "%s"'), $username);
        }
    }
}

有关更多信息,请查看 PHP ActiveRecord 网站。它的 wiki