atrauzzi/laravel-doctrine

此包已被废弃,不再维护。作者建议使用 laravel-doctrine/orm 包。

Web Artisan 框架的 ORM

1.0.0 2015-01-22 23:46 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:24:20 UTC


README

Join the chat at https://gitter.im/atrauzzi/laravel-doctrine Scrutinizer Code Quality Build Status SensioLabsInsight

此库已被 laravel-doctrine/orm 取代

尽管此库尚未废弃,但有一个功能更完整且更新更快的替代品 laravel-doctrine/orm 可用。去看看吧!

Web Artisan 框架的 ORM

Laravel 的 Eloquent ORM 对于轻量级使用非常出色,但是当你需要一个功能更全面的 ORM 时,几乎没有东西能比得上 Doctrine

这是一个 Doctrine 2.x 到 Laravel 的集成,作为一个 composer 包。Doctrine 的 EntityManager 实例可以通过名为 Doctrine 的外观或者通过依赖注入来访问。

元数据通过 注解驱动程序 或自定义的、利用类似 Laravel 配置语法的 config 驱动程序来获取。

安装

安装方式与 Laravel 包的常规安装方式相同。

在您的 composer.json 中插入以下配置

"minimum-stability": "dev",
"prefer-stable": true

在包部分(require)

"atrauzzi/laravel-doctrine": "dev-master"

之后,只需运行 composer update

将服务提供者添加到您的 Laravel 应用程序中的 config/app.php。在 providers 数组中添加

Atrauzzi\LaravelDoctrine\ServiceProvider::class,

如果需要,可以在同一文件中的 facades 数组中添加以下内容

'EntityManager' => Atrauzzi\LaravelDoctrine\Support\Facades\Doctrine::class,

您需要运行此命令发布包配置。

php artisan vendor:publish --provider="Atrauzzi\LaravelDoctrine\ServiceProvider" --tag="config"

使用方法

您可以通过使用 Doctrine 外观来简单地获取您连接的 EntityManager 实例

改编自 Doctrine 文档

<?php
$user = new User;
$user->setName('Mr.Right');
EntityManager::persist($user);
EntityManager::flush();

Laravel 5 的示例实体

namespace App\Lib\Domain\Entities;

use Doctrine\ORM\Mapping as ORM;
use Atrauzzi\LaravelDoctrine\Trait\Time;

/**
 * @ORM\Entity
 * @ORM\Table(name="Post")
 */
class Post
{
    use Time;

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;
    /**
     * @ORM\Column(type="string")
     */
    private $title;
    /**
     * @ORM\Column(type="text")
     */
    private $body;
    public function __construct($input)
    {
        $this->setTitle($input['title']);
        $this->setBody($input['body']);
    }
    public function getId()
    {
        return $this->id;
    }
    public function getTitle()
    {
        return $this->title;
    }
    public function setTitle($title)
    {
        $this->title=$title;
    }
    public function setBody($body)
    {
        $this->body=$body;
    }
    public function getBody()
    {
        return $this->body;
    }
}

建议您阅读所有 ORM 文档。尝试使用 Laravel 的控制台进行实验,并完成教程。

祝您享受!

Doctrine 控制台

如果您需要运行 ORM 命令,则在项目根目录中需要一个 cli-config.php 文件,具有以下实现

<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Illuminate\Foundation\Application;

require __DIR__.'/bootstrap/autoload.php';

/** @var Application $app */
$app = require_once __DIR__.'/bootstrap/app.php';

/** @var Illuminate\Contracts\Http\Kernel $kernel */
$kernel = $app->make('Illuminate\Contracts\Console\Kernel');
$kernel->bootstrap();

$app->boot();

$entityManager = $app->make('Doctrine\ORM\EntityManager');

return ConsoleRunner::createHelperSet($entityManager);

为了验证您的模式,您可以这样做:

$ vendor/bin/doctrine orm:validate-schema

认证驱动程序

此包允许您使用自己的用户模型来自定义认证驱动程序。为了使用Doctrine认证驱动程序,您需要注意以下结构。

  • 在您的应用程序中拥有表示可认证用户的用户模型
  • 编辑/config/doctrine.php配置文件以设置认证模型和用户提供者
  • 编辑/config/auth.php配置文件以设置认证驱动程序。

现在,让我们了解这个驱动程序是如何工作的。

用户模型

您的应用程序必须有一个实现了Illuminate\Contracts\Auth\Authenticatable接口的模型。默认情况下,此包附带一个Doctrine认证提供者,它使用模型中的emailpassword作为唯一有效的凭据。以下代码显示了有效的用户模型

namespace App\Models;

use Illuminate\Contracts\Auth\Authenticatable;

/**
 * Class User
 * @entity
 * @table(name="users")
 */
class User implements Authenticatable {

    /**
     * @var int
     * @column(type="integer", name="id_user")
     * @generatedValue(strategy="AUTO")
     * @id
     */
    protected $id;

    /**
     * @var string
     * @column(type="string", unique=true)
     */
    protected $email;

    /**
     * @var string
     * @column(type="string")
     */
    protected $password;

    /**
     * @var string
     * @column(type="string")
     */
    protected $token;

    public function getEmail()
    {
        return $this->email;
    }

    public function getPassword()
    {
        return $this->password;
    }


    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->id;
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->token;
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->token = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'remember_me_token';
    }
}

重要的是要知道Laravel需要我们的模型实现此接口提供的某些规则

  • getAuthIdentifier()
  • getRememberToken()
  • setRememberToken($value)
  • getRememberTokenName()
doctrine.php

一旦您创建了有效的用户模型,您就可以在以下配置文件中指定它

'auth' => [
    'authenticator' => 'Atrauzzi\LaravelDoctrine\DoctrineAuthenticator',
    'model' => 'App\Models\User',
]
  • 如果您想基于emailpassword构建您的认证系统,则可以使用默认的Doctrine认证器。
  • 如果您需要实现自己的Doctrine认证器,则通过传递类名设置authenticator键。
  • 如果您想使用本地的Laravel认证驱动程序,则将authenticator键设置为null值或仅将其注释掉。
auth.php

最后,要将Doctrine驱动程序设置为默认的认证系统,您需要将值设置为doctrine.auth

'driver' => 'doctrine.auth',

许可证

Laravel框架是在MIT许可证下的开源软件。

此项目旨在确保最大的兼容性。

元数据

我对听取有关此包的反馈和建议很感兴趣。请随时提交工单

访问laravel-doctrine

laravel-doctrine是由Alexander Trauzzi制作的,得到了contributors.md中所有人的帮助!