没有 Eloquent 和数据库迁移的 Laravel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34428002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:52:16  来源:igfitidea点击:

Laravel without Eloquent & database migrations?

phpmysqllaraveleloquentdatabase-migration

提问by Dylan

I'm a PHP programmer for 12 years now, and pretty much re-invented the wheel many times, building my own framework for our closed-source web-app, which is offered as a hosted solution, using the same shared database for all customers.

我现在是 12 年的 PHP 程序员,并且几乎重新发明了很多次,为我们的闭源网络应用程序构建了我自己的框架,该框架作为托管解决方案提供,对所有人使用相同的共享数据库顾客。

Now I'm trying out Laravel 5 and noticed that almost every example uses Eloquent and database migrations. To me it seems these kind of things are targeted towards simple databases and people who don't like SQL or database-design (but I might be wrong).

现在我正在尝试 Laravel 5 并注意到几乎每个示例都使用 Eloquent 和数据库迁移。对我来说,这些东西似乎是针对简单的数据库和不喜欢 SQL 或数据库设计的人(但我可能错了)。

Our MySQL database contains 100+ tables, a lot of stored procedures and many triggers which I just can't imagine doing in an ORM. We use Navicat for database-design and testing SQL-queries. For upgrading the database to a newer version of the app, we already wrote some nice scripts and even visual tools.

我们的 MySQL 数据库包含 100 多个表、许多存储过程和许多我无法想象在 ORM 中做的触发器。我们使用 Navicat 进行数据库设计和测试 SQL 查询。为了将数据库升级到更新版本的应用程序,我们已经编写了一些不错的脚本甚至可视化工具。

So basically my question is if Laravel is really intended to be used with Eloquent and migrations or that I'm really missing out a lot of functionality without them.

所以基本上我的问题是 Laravel 是否真的打算与 Eloquent 和迁移一起使用,或者如果没有它们我真的错过了很多功能。

What do you recommend?

你有什么建议吗?

回答by Jobin Jose

Its upto you,

由你决定,

Laravel migration is aimed for keeping database version (while using version controller) also the eloquent is aimed for simple relation mapping between tables for complex situations like multiple Join and all its not recommended due to performance issue then you can choose Query Builderit gives much better performance , You need to write plain query in Laravel use \DB::statement();

Laravel 迁移旨在保持数据库版本(同时使用版本控制器),而且 eloquent 的目的是针对复杂情况(例如多个 Join)在表之间进行简单的关系映射,并且由于性能问题不推荐所有这些,那么您可以选择Query Builder它提供了更好的性能,你需要在 Laravel 中编写普通查询使用\DB::statement();

The Laravel is a perfect match for Angular Js, more Laravel is just a wrapper of few nice PHP components that is capable for providing rapid results.

Laravel 是 Angular Js 的完美搭档,更多 Laravel 只是一些能够提供快速结果的优秀 PHP 组件的包装器。

hope it helps..

希望能帮助到你..

回答by tomvo

Using an ORM simplifies your life as most common scenario's are already covered. Fetching data, managing relationships and eager/lazy loading are a breeze. It also protects you from any injection vulnerabilities you might create when writing your own hardcoded queries. Of course not all scenario's can be handled by an ORM, hence the possibility to write RAW queries. Using Laravel's Query Builderyou could do something like this:

使用 ORM 可以简化您的生活,因为已经涵盖了最常见的场景。获取数据、管理关系和急切/延迟加载都是轻而易举的。它还可以保护您免受您在编写自己的硬编码查询时可能创建的任何注入漏洞的影响。当然,并非所有场景都可以由 ORM 处理,因此可以编写 RAW 查询。使用 Laravel 的Query Builder你可以做这样的事情:

$results = DB::select( DB::raw("SELECT * FROM users WHERE username = :username"), ['username' => 'johndoe']);

$results = DB::select( DB::raw("SELECT * FROM users WHERE username = :username"), ['username' => 'johndoe']);

If you want to execute things like ALTERor SET's then you can use DB::Statement.

如果你想执行像ALTERorSET之类的东西,那么你可以使用DB::Statement.

So just to note, Eloquentis Laravel's ORM while the Query Builderis the layer for building your queries in a safe way.

所以请注意,EloquentLaravel 的 ORMQuery Builder是用于以安全方式构建查询的层。

So whether or not to use Eloquent is up to you, I believe they've done a good there with a solid ORM but you're always free to implement another ORM like Doctrine, Data Mapper, etc. There are Laravel bindings for most of those.

因此,是否使用 Eloquent 取决于您,我相信他们在使用可靠的 ORM 方面做得很好,但您始终可以自由地实现另一个 ORM,如 Doctrine、Data Mapper 等。大部分都有 Laravel 绑定那些。

Edit:Worth mentioning is also that an Eloquent Model provides some handy extra's that also simplifies things, like a JSON convert __toString, protected attributes, date casting, etc. When fetching multiple models they will be stored in a Collection, an Arrayablethat provides even more methods to make for happy times. Check it out: http://laravel.com/docs/5.1/eloquent-collections

编辑:还值得一提的是,Eloquent 模型提供了一些方便的额外功能,它们也简化了事情,例如 JSON 转换 __toString、受保护的属性、日期转换等。当获取多个模型时,它们将存储在一个 中CollectionArrayable它提供了更多的方法为快乐时光做准备。检查一下:http: //laravel.com/docs/5.1/eloquent-collections

回答by Marcin Nabia?ek

You can run simple SQL queries in Laravel if you want, but indeed most examples are using Eloquent.

如果需要,您可以在 Laravel 中运行简单的 SQL 查询,但实际上大多数示例都使用Eloquent.

I've been doing project with over 100 tables and it most cases it's possible to use Eloquent instead of putting raw SQL queries each time but it's rather my preference.

我一直在做超过 100 个表的项目,在大多数情况下,可以使用 Eloquent 而不是每次都放置原始 SQL 查询,但这更像是我的偏好。

However you mentioned that you have many stored procedures and triggers in database. To be honest you should rethink that because now you might be putting to much business logic into Database and your logic is both in Database and in Application.

但是您提到您在数据库中有许多存储过程和触发器。老实说,您应该重新考虑一下,因为现在您可能会将很多业务逻辑放入数据库中,而您的逻辑既在数据库中又在应用程序中。

I saw a couple months ago such database in MsSQL and it was horrible - nobody really knew what is happening and when you wanted to migrate this database to MySQL and Laravel application it was a big problem because there was too much logic in database (I haven't took part in this project only took a look at it)

几个月前我在 MsSQL 中看到了这样的数据库,这太可怕了——没有人真正知道发生了什么,当你想把这个数据库迁移到 MySQL 和 Laravel 应用程序时,这是一个大问题,因为数据库中有太多的逻辑(我没有没有参与这个项目只是看了看)