简体   繁体   English

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

[英]Laravel without Eloquent & database migrations?

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. 我已经成为一名PHP程序员已有12年了,而且很多次重新发明了这个轮子,为我们的闭源网络应用程序构建了自己的框架,该框架作为托管解决方案提供,使用相同的共享数据库顾客。

Now I'm trying out Laravel 5 and noticed that almost every example uses Eloquent and database migrations. 现在我正在尝试Laravel 5,并注意到几乎每个例子都使用Eloquent和数据库迁移。 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). 对我来说,似乎这些东西都是针对简单的数据库以及不喜欢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. 我们的MySQL数据库包含100多个表,许多存储过程和许多触发器,我无法想象在ORM中。 We use Navicat for database-design and testing SQL-queries. 我们使用Navicat进行数据库设计和测试SQL查询。 For upgrading the database to a newer version of the app, we already wrote some nice scripts and even visual tools. 为了将数据库升级到更新版本的应用程序,我们已经编写了一些很好的脚本甚至可视化工具。

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? 您有什么推荐的吗?

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 Builder it gives much better performance , You need to write plain query in Laravel use \\DB::statement(); Laravel迁移旨在保持数据库版本(同时使用版本控制器),eloquent旨在简化表之间的关系映射,适用于复杂情况,如多个Join,并且由于性能问题而不推荐所有它,然后您可以选择更好的Query Builder性能,你需要在Laravel中使用\\DB::statement();写简单查询\\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.. 希望能帮助到你..

Using an ORM simplifies your life as most common scenario's are already covered. 使用ORM可以简化您的生活,因为大多数常见方案已经涵盖在内。 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. 当然,并非所有场景都可以由ORM处理,因此可以编写RAW查询。 Using Laravel's Query Builder you could do something like this: 使用Laravel的Query Builder您可以执行以下操作:

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

If you want to execute things like ALTER or SET 's then you can use DB::Statement . 如果你想执行像ALTERSET这样的东西,你可以使用DB::Statement

So just to note, Eloquent is Laravel's ORM while the Query Builder is the layer for building your queries in a safe way. 所以,只是要注意, Eloquent是Laravel的ORM,而Query 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 Arrayable that provides even more methods to make for happy times. 编辑:值得一提的是,一个Eloquent模型提供了一些方便的额外功能,也简化了一些事情,比如JSON转换__toString,受保护的属性,日期转换等。当获取多个模型时,它们将存储在一个Collection ,一个提供均匀的Arrayable中为快乐时光做出更多方法。 Check it out: http://laravel.com/docs/5.1/eloquent-collections 请查看: http//laravel.com/docs/5.1/eloquent-collections

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应用程序时,这是一个很大的问题,因为数据库中存在太多逻辑(我没有参加这个项目只看了一眼)

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

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