Doctrine 2 for Laravel5
, (*1)
A Doctrine 2 implementation that melts with Laravel 5.1.X., (*2)
Documentation
Begin reading the full documentation here or go to a specific chapter right away., (*3)
- Installation
-
How It Works
- Basics
- Entity Manager
- Timestamps
- Soft Deleting
- Authentication
- Schemas
-
Doctrine Configuration
- Metadata Configuration
- Annotation Reader
- Metadata
- MIT License
Caveats
As a result the composer install may require you to change
the minimum-stability
in your composer.json
to dev
., (*4)
如果你不想影响其他包的稳定性, 您可以在您的 composer.json
增加下面的属性:, (*5)
"prefer-stable": true, (*6)
安装
通过Composer安装之前,编辑你项目的 composer.json
文件,添加 uedehua/laravel5-doctrine
., (*7)
这个代码还处于早期阶段,但功能齐全。可以预料会有一些小得变化,不会有大得变动., (*8)
"require": {
"uedehua/laravel5-doctrine": "5.1.*"
}
接下来,通过Composer命令更新你得项目:, (*9)
php composer update
一旦安装了包,您需要添加服务供应商. 打开项目 app/config/app.php
配置文件, 在providers
数组内添加新的项目., (*10)
'UeDehua\LaravelDoctrine\Provider\DoctrineOrmProvider'
接下来添加Facade. 打开 app/config/app.php
配置文件, 在aliases
数组内添加新的项目., (*11)
'DoctrineOrm' => 'UeDehua\LaravelDoctrine\Facade\DoctrineOrm'
别忘记发布你的配置., (*12)
php artisan config:publish uedehua/laravel5-doctrine --path=vendor/uedehua/laravel5-doctrine/config
2 Minutes
This package uses the Laravel database configuration and thus it works right out of the box. With the Entity Manager facade (or service locator) you can interact with repositories.
It might be wise to check out the Doctrine 2 docs to know how it works.
The little example below shows how to use the EntityManager in it simplest form., (*13)
<?php
$user = new User;
$user->setName('Mitchell');
EntityManager::persist($user);
EntityManager::flush();
The User
used in the example above looks like this., (*14)
<?php
use Doctrine\ORM\Mapping AS ORM;
/**
* @ORM\Entity
* @ORM\Table(name="hk_user")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $name;
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
}
If you've only used Eloquent and its models this might look bloated or frightening, but it's actually very simple. Let me break the class down., (*15)
<?php
use Doctrine\ORM\Mapping AS ORM;
/**
* @ORM\Entity
* @ORM\Table(name="hk_user")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $name;
}
The only thing that's actually important in this entity
are the properties. This shows you which data the entity
holds., (*16)
With Doctrine 2 you can't interact with database by using the entity User
. You'll have to use Entity Manager and repositories
.
This does create less overhead since your entities aren't extending the whole Eloquent model
class. Which can dramatically slow down your application a lot if you're working with thousands or millions of records., (*17)
License
This package is licensed under the MIT license., (*18)