简体   繁体   English

Laravel的ORM与Symfony的学说相比如何工作

[英]How does Laravel's ORM works comparated to Symfony's Doctrine

I'm a Symfony 2 developper who's beginning on Laravel. 我是一个开始使用Laravel的Symfony 2开发人员。 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). 我对Laravel的ORM有点迷失,似乎我们必须直接处理数据库才能手动创建表...在Symfony上,这是Doctrine根据映射类(和@ORM注释)自动完成的。

Is the concept totally different on Laravel, or did I just not find the way to do it like on Symfony ? 这个概念在Laravel上是完全不同的,还是我只是没有找到像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. 您的问题还不够清楚,但是我想您想知道Eloquent模型是如何映射表的,在这种情况下,您必须使用表名(在数据库中)为单词的复数形式(但不是强制性的),例如表用于包含用户数据应该是users或POST数据表应该是posts ,但你可以使用任何你想要的。

To map the users table with an Eloquent model; Eloquent模型映射用户表; 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: Laravel将从posts表中查询,但是如果您的表名与Laravel想要的标准方式不同,则您必须通过在模型中添加属性来告诉Laravel表名是什么,例如:

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

You can use Post model as; 您可以将Post模型用作;

// 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): Laravel具有像DoctrineMigrationsBundle这样的迁移,因此在您的模型(整个)中,您只需编写(例如):

class YourClass extends Eloquent {
}

So no need to overcharge your model with attribute, laravel do it automatically 因此,无需为模型加重属性,laravel会自动执行

http://laravel.com/docs/migrations for more information aboutmigration http://laravel.com/docs/migrations了解有关迁移的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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