EnglishEspañolTiếng ViệtAs a PHP developer, you might have come across the term ORM, ORM is a way to work with databases in the same way you work with classes and objects" /> EnglishEspañolTiếng ViệtAs a PHP developer, you might have come across the term ORM, ORM is a way to work with databases in the same way you work with classes and objects" />
Length:ShortLanguages:url-selector#selectionChanged" data-controller="url-selector">EnglishEspañolTiếng Việt

As a PHP. developer, you might have come across the term ORM. ORM is a way lớn work with databases in the same way you work with classes & objects. If you were to delve deeper inkhổng lồ how web applications are designed and built, after doing some exploring in their ORM you would find two well-known patterns: Active sầu RecordData Mapper.Quý Khách đang xem: Doctrine là gì

Active sầu Record refers khổng lồ mapping an object lớn a database row. Indeed, each row in the database is tied to an object. When you retrieve sầu a row from the database you can update, delete or save using the object itself. That’s how Eloquent and Paris work, and how it’s done in Ruby on Rails.

Bạn đang xem: Doctrine là gì

On the other hand, Data Mapper is a layer of software which separates the in-memory objects from the database. With Data Mapper the in-memory objects needn’t know that there is even a database present. They need no Squốc lộ interface clinkerhq.com or knowledge of the database schema. One such solution is Doctrine.


*

What Is Doctrine?

Doctrine is an ORM which implements the data mapper pattern và allows you to lớn make a clean separation of the application’s business rules from the persistence layer of the database.

Some of the advantages I discovered while using Doctrine with Laravel are:

Faster và easier to use. Entities are just plain PHPhường objects. Doctrine utilizes a “clinkerhq.com first” approach, so you can create entities first, & then generate a database for them automatically. The reverse case is also possible, but I do not recommover it. Supports annotations, XML & YAML for schema. Dquốc lộ (a replacement for SQL) abstracts your tables away. Doctrine events allow you khổng lồ easily hook onto specific database events and perform certain actions. Repositories are more faithful khổng lồ the repository pattern. Transactional write-behind methodology lets Doctrine have less interaction with the Database until the flush() method is called.

Of course, Doctrine has disadvantages too, but it is up lớn the programmer lớn choose the right ORM.

Doctrine DQL

Dquốc lộ stands for Doctrine Query Language. Dquốc lộ brings you object query language, which means that instead of a traditional relational query, you have sầu queries in object form.

Dquốc lộ allows you lớn write database queries in an object-oriented way, which is helpful when you need to lớn query the database in a way which cannot be achieved (or is very difficult) using the mặc định repository methods.

Xem thêm: Bốn Năm Kỳ Tích Xạ Thủ Hoàng Xuân Vinh Bắn Súng Đẳng Cấp Thế Giới?

Sample DQL Query:

SELECT b.id as ItemId, b.title as ItemTitle , b.url as ItemUrl FROM AlirezaDomainIdentityEntitiesMenu u WHERE u.id =:id

Doctrine Filters

Doctrine provides Squốc lộ màn chơi limitations, so there is no need to maintain the clause in multiple repositories of your project. This enhances security and makes your clinkerhq.com easier lớn read.

Let’s look at an example:

/** * JoinColumn(name="user_id", referencedColumnName="id") **/ private $user;As you can see in the User entity, the result of JoinColumn is limited lớn only items with the condition of WHERE user_id = :user_id.

Setting Up Doctrine 2

To phối up Doctrine, there is a bridge khổng lồ allow for matching with Laravel 5’s existing configuration. To install Doctrine 2 within our Laravel project, we run the following command:

composer require laravel-doctrine/ormAs usual, the package should be added lớn the app/config.php, as the service provider:

LaravelDoctrineORMDoctrineServiceProvider::class,The alias should also be configured:

"EntityManager" => LaravelDoctrineORMFacadesEntityManager::classFinally, we publish the package configuration with:

php artisan vendor:publish --tag="config"Doctrine needs no database configuration & uses the current Laravel configuration, but if you want lớn override it you should change the Doctrine config tệp tin in Config/doctrine.php:

"managers" => env("APP_DEBUG"), "meta" => env("DOCTRINE_METADATA", "annotations"), "connection" => env("DB_CONNECTION", "mysql"), "namespaces" => ,That’s all there is lớn it.

What Is an Entity?

“Entity” refers lớn an object which has a distinct identity. An entity must have sầu a specific identifier which is quality throughout the entire system, such as a customer or a student. There would be other objects, such as email addresses, which are not entities, but value objects.

Let’s create a Post Entity App/Entity/Post.php:

namespace AppEntity;use DoctrineORMMapping as ORM;/** * ORMColumn(type="text") */ private $body; public function __construct($input) $this->setTitle($input); $this->setBody($input); public function getId() return $this->id; public function getTitle() return $this->title; public function setTitle($title) $this->title = $title; public function getBody() return $this->body; public function setBody($body) $this->toàn thân = $body; }The class properties should be the same as the fields in the database table, or you can define them with the Colum("name"="myfield") annotation.

What Is a Repository?

The repository allows all your clinkerhq.com lớn use objects without needing lớn know how the objects are persisted. The repository contains all the knowledge of persistence, including mapping from tables to objects. This provides a more object-oriented view of the persistence layer & makes the mapping clinkerhq.com more encapsulated.

Xem thêm: Tim Sharky Là Ai Girl Porn Tube, Tim Sharky With His Thai Girl Porn Tube

Now it’s time to create the Repository in App/Repository/PostRepo.php:

namespace AppRepository;use AppEntityPost;use DoctrineORMEntityManager;class PostRepo /** * var EntityManager */ private $em; public function __construct(EntityManager $em) $this->em = $em; public function create(Post $post) $this->em->persist($post); $this->em->flush(); public function update(Post $post, $data) $post->setTitle($data); $post->setBody($data); $this->em->persist($post); $this->em->flush(); public function PostOfId($id) return $this->em->getRepository($this->class)->findOneBy( $id >); public function delete(Post $post) $this->em->remove($post); $this->em->flush(); /** * create Post * return Post */ private function prepareData($data) return new Post($data); The Doctrine EntityManager works as the access point for the complete management of your entities.Then, create the Controller App/Http/Controllers/PostController.php:

I prefer lớn create my own Validator based on Laravel’s Validator class. Here’s the Validator AppValidationPostValidator.php:

namespace AppValidation;use Validator;class PostValidator public static function validate($input) Max:80

Conclusion

If you have not previously worked with Doctrine 2, I hope this article has been interesting & informative. Laravel 5 does not use Doctrine, but as you can see, there are some packages which allow us lớn easily use it with Laravel. I created a simple blog tiện ích with Laravel 5 & Doctrine ORM, & I hope this can help you to create your desired phầm mềm. I welcome your comments.


Chuyên mục: KHÁI NIỆM
Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *