In Doctrine 2, the database is automatically created to reflect the meta data in the entity annotations.ĭoctrine 2 entities are just plain PHP objects and so they don’t know anything about persistence. In Laravel we use migration files to create the database schema. You will notice in the example above that we list details about how the entity should be stored such as what the table should be called and what type of column should be used. This looks a little bit overwhelming at first, but it is really just simple markup that gives Doctrine meta data about the class. Unlike Eloquent, Doctrine 2 entities have annotations that describe the entity. It is therefore very light because it does not come with the overhead of the entire ORM. With Doctrine, your entities are just plain PHP objects as they do not inherit any of the overhead from extending an ORM class.Ī Doctrine User entity might look something like this: use Doctrine\ORM\Mapping as ORM Īs you can see this is just a plain PHP class. So for example, you can save the entity at any point in your code just by calling the save() method: $user = User::find(123) When interacting with the User entity, you have all of the methods of Eloquent available because you are extending Eloquent. The User class might have a couple of relationships and perhaps some helper methods, but for the most part it would be an empty class. Typically whilst using Eloquent, you would write something like this: class User extends Eloquent One of the big differences between Doctrine 2 and Eloquent is, Doctrine 2 entities are just plain old PHP, whereas Eloquent entities inherit all of the persistence logic of the ORM. In this tutorial I want to explore exactly how Doctrine 2, an ORM that implements the Data Mapper pattern, is different to Eloquent. Having the business logic of the object and the persistence logic of the database tied together definitely makes working with an Active Record ORM easier to pick up. Calling save() on the object updates the database, and all of the magic persistence happens behind the scenes. With Active Record, you basically just have an object that you can manipulate and then save. One of the really great things about ORM’s that implement the Active Record pattern like Eloquent is, they are really easy and really intuitive to use.
0 Comments
Leave a Reply. |