rockeinstein/eloquent-zf2

Zend Framework 2 的 Eloquent ORM 模块

1.0.0 2016-02-01 18:03 UTC

This package is not auto-updated.

Last update: 2024-09-20 17:48:35 UTC


README

Zend Framework 2 的 Eloquent ORM 模块。

特性

  • Eloquent ModelCapsule 支持开箱即用。
  • 表单验证器:RecordExistsNoRecordExists(相当于 Zend\Validator\Db\RecordExistsZend\Validator\Db\noRecordExists)。
  • 认证适配器(抽象实现支持 AuthenticationService 和两个实现 CallbackCheckAdapterCredentialTreatmentAdapter)。

计划中的特性

  • 支持多连接配置。
  • 支持缓存。
  • 支持分页器。
  • 支持日志。
  • 会话处理器(目前不确定是否需要)。
  • 支持迁移(通过 ZFTool)。

安装

$ composer require edvinasme/eloquent-zf2:dev-master

设置

  • EloquentZF2 添加到 config/application.config.php 中的 modules 数组
  • 现在您可以通过 SM 获取 EloquentZF2:$sm->get('EloquentZF2') 这将为您提供对 Eloquent 查询构建器、模式构建器等的访问(有关更多信息,请参阅 https://laravel.net.cn/docs/database
  • vendor/edvinasme/eloquent-zf2/config/database.eloquent.config.php.dist 复制到 config/autoload/database.eloquent.config.php 并添加数据库凭证。

模型

您的模型应扩展 Illuminate\Database\Eloquent\Model,例如

    <?php
    namespace Album\Model;

    // skipping some code here

    use Illuminate\Database\Eloquent\Model as EloquentZF2Model;

    class Album extends EloquentZF2Model implements InputFilterAwareInterface
    {

验证器

当填充 InputFilter(通常在 Model 的 getInputFilter() 方法中)时,您可以使用 EloquentZF2\Validator\RecordExistsEloquentZF2\Validator\NoRecordExists 验证器,这些验证器将检查数据库中的记录存在性并根据该字段进行验证。

支持以下选项键

  • table => 要验证的数据库表
  • schema => 要检查匹配的架构(数据库)
  • field => 要检查匹配的字段
  • exclude => 可选的 where 子句或要排除的查询字段/值对
  • connection => 可选的数据库连接名称

以下示例检查表 users、字段 login 与表单输入值,同时排除 login = test@example.org 的记录

$inputFilter->add($factory->createInput(array(
              'name'     => 'email    ',
                'required' => true,
                'validators' => array(
                    array(
                        'name'    => 'EloquentZF2\Validator\noRecordExists',
                        'options' => array(
                            'table' => 'users',
                            'field' => 'login',
                            'exclude' => array(
                                'field' => 'login',
                                'value' => 'text@example.org',
                                ),
                            ),
                        ),
                    ),
                )
            )
        );

认证

为了帮助您以类似 ZendDB 的方式实现 EloquentZF2 的认证,提供了一个抽象认证适配器(Authentication\\Adapter\EloquentDb.php)和两个实现

  • Authentication\Adapter\CallbackCheckAdapter.php - 允许您提供自定义回调函数。当您想使用 bcrypt 等检查凭证时,这非常有用。如果未提供回调函数,则默认为简单的 ($a == $b) 比较。以下是一个示例。
  • Authentication\Adapter\CredentialTreatmentAdapter.php - 允许您提供 SQL 语句、函数或过程(例如 MD5(?), PASSWORD(?) 等),在检查凭证之前应应用于给定的凭证。默认为 '?',相当于 (passwordField = password) 比较。以下是一个示例。

这两个适配器都以与 Zend 的认证适配器非常相似的方式实现,并且与 Authentication Service 兼容。请参阅 Zend 的 Authentication Service 文档 了解更多信息。

CallbackCheckAdapter.php 示例

这是一个使用 Bcrypt(Zend 的实现)的示例。这通常放在您的控制器登录操作中

/* SomeController.php */

// use CallbackCheckAdapter as AuthAdapter
use EloquentZF2\Authentication\Adapter\CallbackCheckAdapter as AuthAdapter;

// ... controller code skipped ...

public function loginAction() {

    // ... skipping validation and form code ...

    // define custom callback function (bcrypt)
    $callback = function($a, $b) {
        $bcrypt = new \Zend\Crypt\Password\Bcrypt(array('cost' => '14'));
        return $bcrypt->verify($b, $a);
    };

    // init auth adapter
    $authAdapter = new AuthAdapter('default', 'users', 'login', 'password', $callback);

    // set auth credentials (assuming it was posted by form)
    $authAdapter
        ->setIdentity($request->getPost('login'))
        ->setCredential($request->getPost('password'));

    // authenticate
    $authResult = $authAdapter->authenticate();

CredentialTreatmentAdapter.php 示例

这是一个使用 MD5(?) 的示例。这通常放在您的控制器登录操作中

/* SomeController.php */

// use CallbackCheckAdapter as AuthAdapter
use EloquentZF2\Authentication\Adapter\CredentialTreatmentAdapter as AuthAdapter;

// ... controller code skipped ...

public function loginAction() {

    // ... skipping validation and form code ...
    $callback = 'MD5(?)';

    // init auth adapter
    $authAdapter = new AuthAdapter('default', 'users', 'login', 'password', $callback);

    // set auth credentials (assuming it was posted by from)
    $authAdapter
        ->setIdentity($request->getPost('login'))
        ->setCredential($request->getPost('password'));

    // authenticate
    $authResult = $authAdapter->authenticate();
}