简体   繁体   中英

How does Laravel's ORM works comparated to Symfony's Doctrine

I'm a Symfony 2 developper who's beginning on Laravel. I'm a little bit lost with Laravel's ORM, it seems that we have to directly deal with the database to create tables manually... On Symfony, this was automatically made by Doctrine according to the mapping classes (and @ORM annotations).

Is the concept totally different on Laravel, or did I just not find the way to do it like on Symfony ?

Your question is not clear enough but I guess you want to know how Eloquent models map tables, in this case you have to use your table names (in database) the plural form of the word (but not mandatory), for example, a table that used to contain user data should be users or for post data the table should be posts but you may use whatever you want.

To map the users table with an Eloquent model; all you need to do i; create a model like this:

Post extends Eloquent {
    //...
}

Now you may use something like this:

$posts = Post::all();
$post = Post::find(1);

Laravel will query from the posts table but if your table name is different than the standard way Laravel wants then you have to tell Laravel what is the table name by adding a property in the model, for example:

Post extends Eloquent {
    protected $table = 'article'; // Laravel will query from article table
    //...
}

You can use Post model as;

// Laravel will query from article table in the database
// because you gave the $table property with the table name

$posts = Post::all();
$post = Post::find(1);

Read more on the manual .

Laravel have migration like DoctrineMigrationsBundle, so in your model(entitie) you just write (for exemple):

class YourClass extends Eloquent {
}

So no need to overcharge your model with attribute, laravel do it automatically

http://laravel.com/docs/migrations for more information aboutmigration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM