php 没有数据库的 Laravel 模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24575572/
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
Laravel model without db
提问by Dusan Plavak
I have model Meal which can contain model Ingredient and Ingredient has a lot of properties...
我有模型 Meal 可以包含模型成分,成分有很多属性......
I have all ingredient in DB and also some meals....
我有 DB 中的所有成分,还有一些饭菜......
But I want to create new meal but without storing it in DB.
但我想创建新餐但不将其存储在数据库中。
so something like:
所以像:
$meal = new Meal;
$meal->ingredients()->attach(5);
where 5 is id of ingredient in DB.
其中 5 是 DB 中成分的 ID。
However this will fail because $meal is not stored in DB yet and attach() function trying to create a new record in meal_ingredient table....
但是,这将失败,因为 $meal 尚未存储在数据库中,并且 attach() 函数尝试在 meal_ingredient 表中创建新记录....
So is there any way how to create "offline" model and connect it with "online" data?
那么有什么方法可以创建“离线”模型并将其与“在线”数据连接起来?
Thanks
谢谢
回答by LaravelMG
Your Question:
你的问题:
So is there any way how to create "offline" model and connect it with "online" data?
那么有什么方法可以创建“离线”模型并将其与“在线”数据连接起来?
Our Take:
我们的看法:
Yes, you can use a sort of Laravel Model without a Database(offline as you phrase it). Please refer to jenssegers/laravel-model. Basically it's a class implementing
ArrayAccess
,ArrayableInterface
,JsonableInterface
with some states and behaviours as needed.Yes, there should be a way to connect "Online"
Illuminate\Database\Eloquent\Model
with your "offline" Model:POO
andDesign Pattern
are there to the rescue. Get your hands dirty, don't hesitate to delve into the source code!
是的,您可以使用一种没有数据库的 Laravel模型(如您所说的离线)。请参考jenssegers/laravel-model。基本上它是一个实现类
ArrayAccess
,ArrayableInterface
,JsonableInterface
一些州和行为需要。是的,应该有一种方法可以将“在线”
Illuminate\Database\Eloquent\Model
与您的“离线”模型连接起来:POO
并Design Pattern
在那里进行救援。把手弄脏,不要犹豫,深入研究源代码!
We suggest you to roll your own "Offline" Model based on the source code of jenssegers/laravel-model
and extend "Online" Illuminate\Database\Eloquent\Model
(Decorator pattern or whatever!?) to make it have knowledge of the former. The plumbing is left to you, no spoon fed code so far ;-)
我们建议您基于源代码推出自己的“离线”模型jenssegers/laravel-model
并扩展“在线” Illuminate\Database\Eloquent\Model
(装饰器模式或其他!?),使其了解前者。管道留给你,到目前为止没有勺子喂代码;-)
Notes:
笔记:
You may likely have to define some custom dependent (helper) classes of Illuminate\Database\Eloquent\Model
such as Illuminate\Database\Eloquent\Relations\BelongsToMany
and so on.
您可能需要定义一些自定义的依赖(帮助程序)类,Illuminate\Database\Eloquent\Model
诸如此类Illuminate\Database\Eloquent\Relations\BelongsToMany
。
FIY, you can also find a relevant sample of extending Illuminate\Database\Eloquent\Model
here jarektkaczyk/Eloquent-triple-pivotusing latest PHP features.
FIY,您还可以在Illuminate\Database\Eloquent\Model
此处 找到使用最新 PHP 功能扩展jarektkaczyk/Eloquent-triple-pivot的相关示例。
Happy coding.
快乐编码。
回答by daVe
You should treat the object like a collection to work "offline". And remember to build the objects it they doesn't exist.
您应该将对象视为“离线”工作的集合。并记住构建它们不存在的对象。
$ingredient = new Ingredient;
$ingredient->id = 5;
$meal = new Meal;
$meal->ingredients->add($ingredient);
So neither the Meal nor the Ingredient number 5 has to exists in DB.
因此,无论是膳食还是成分 5 都不必存在于 DB 中。