什么时候在laravel中使用雄辩的模型(When to use an eloquent model in laravel)
我正在开发一组后端API,将由android / iphone应用程序使用。 有些情况下我必须使用超过2或3个表的连接。
例如:
我有一个与其喜欢,SKU,评论和类别表相关联的产品表。 如果我使用预先加载来获取与products表关联的所有值,那么它将执行以下查询:
select * from `products` where `products`.`products_id` = '1' select * from `skus` where `skus`.`products_id` in ('1') select * from `likes` where `likes`.`products_id` in ('1') select * from `comments` where `comments`.`products_id` in ('1')
这已经运行4个查询与所有'*'。 如果我需要选择特定的列,我将不得不在模型关系函数中对其进行硬编码,它将不是动态的。 这将破坏我的应用程序的性能和可伸缩性。
雄辩的模型在哪里发挥作用? 它是否只是从查询中获得了更快开发和抽象层的优势? 或者是否有任何特定的地方我可以使用雄辩的模型而不影响性能? 我的理解是,如果我有更多的表,我最好使用连接而不是雄辩?
I am developing a set of backend APIs which will be used by an android/iphone app. There are instances where I have to use joins of more than 2 or 3 tables.
For example:
I have a products table that is associated with its likes, SKU's, comments, and category tables. If I am using eager loading to get all the values associated with the products table, it's going to execute the following queries:
select * from `products` where `products`.`products_id` = '1' select * from `skus` where `skus`.`products_id` in ('1') select * from `likes` where `likes`.`products_id` in ('1') select * from `comments` where `comments`.`products_id` in ('1')
This has run 4 queries with all '*'. If I need to have specific columns to be selected, I will have to hardcode it in the model relationship function and it will not be dynamic. This will kill the performance and scalability of my application.
Where does the eloquent model come into play? Does it just give the advantage of faster development and an abstraction layer from the queries? Or are there any specific places where I can use eloquent model without compromising performance? What I understand is, if I have more tables, I better be using joins rather than eloquent?
原文:https://stackoverflow.com/questions/34149995
最满意答案
Angular自动修剪输入值。 在输入标签上设置
ng-trim="false"
以禁用默认修剪。如果设置为false,Angular将不会自动修剪输入。 输入[type = password]控件将忽略此参数,这些控件永远不会修剪输入。
(默认值:true)
angular.module('myapp', []) .controller('MainCtrl', function($scope) { $scope.foo = { txt: '' }; $scope.kd = function(e) { if (e.keyCode === 9) { var ta = angular.element(e.target); var val = ta.val(); var st = ta[0].selectionStart, ed = ta[0].selectionEnd; ta.val(val.substring(0, st) + '\t' + val.substring(ed)); ta[0].selectionStart = ta[0].selectionEnd = st + 1; //console.log(ta.triggerHandler); ta.triggerHandler('change'); e.preventDefault(); } } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myapp"> <div ng-controller="MainCtrl"> <textarea ng-model="foo.txt" rows="10" cols="75" ng-trim="false" ng-keydown="kd($event)"></textarea> <br> <br> <pre> {{ foo | json }} </pre> </div> </div>
Angular automatically trims the input value. Set
ng-trim="false"
on input tag to disable default trimming.If set to false Angular will not automatically trim the input. This parameter is ignored for input[type=password] controls, which will never trim the input.
(default: true)
angular.module('myapp', []) .controller('MainCtrl', function($scope) { $scope.foo = { txt: '' }; $scope.kd = function(e) { if (e.keyCode === 9) { var ta = angular.element(e.target); var val = ta.val(); var st = ta[0].selectionStart, ed = ta[0].selectionEnd; ta.val(val.substring(0, st) + '\t' + val.substring(ed)); ta[0].selectionStart = ta[0].selectionEnd = st + 1; //console.log(ta.triggerHandler); ta.triggerHandler('change'); e.preventDefault(); } } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myapp"> <div ng-controller="MainCtrl"> <textarea ng-model="foo.txt" rows="10" cols="75" ng-trim="false" ng-keydown="kd($event)"></textarea> <br> <br> <pre> {{ foo | json }} </pre> </div> </div>
相关问答
更多-
Angular自动修剪输入值。 在输入标签上设置ng-trim="false"以禁用默认修剪。 ngTrim 如果设置为false,Angular将不会自动修剪输入。 输入[type = password]控件将忽略此参数,这些控件永远不会修剪输入。 (默认值:true) angular.module('myapp', []) .controller('MainCtrl', function($scope) { $scope.foo = { txt: '' }; ...
-
一种方法是使用Get-Content的-Encoding参数,例如: Get-Content foo.txt -Encoding byte | % {"0x{0:X2}" -f $_} 如果您有PowerShell社区扩展 ,则可以使用Format-Hex命令: Format-Hex foo.txt Address: 0 1 2 3 4 5 6 7 8 9 A B C D E F ASCII -------- ------------------------------- ...
-
虽然效率不高,但这可以做到这一点: var text=''; $("textarea").focus(function() { text = $(this).val(); }); $("textarea").blur(function() { if($(this).val().length<20) { $(this).val(text); } }); Though not very efficient, this would do the work: v ...
-
看起来你有类似的问题。 如果我正确阅读,请尝试:
-
在后台线程中运行executeCmds()方法并使用Platform.runLater()更新文本区域: public void executeCmds(){ Thread thread = new Thread(() -> { createTempDirectory(); copyConfigPropetiesFileValues(); copyConfigProperties(); copyYMLFile(); co ...
-
正在搜索\ n和\ t(Searching for \n and \t)[2023-10-10]
更改 while(getline(file,myString,'\n') ) { for (size_t i = 0; i < myString.size(); ++i) { fout <<"."<< bitset<8>(myString.c_str()[i]); } } 至 int c; while( (c = file.get()) != EOF ) { fout <<"."<< bitset<8>(c); } Change while(getline(fil ... -
\ n和\ t并使用javascript或.net将它们显示在
中(\n and \t and getting them to display in a [2023-01-04]using javascript or .net) 在.NET中,您可以使用String.Replace : s = s.Replace(@"\n", "\n"); s = s.Replace(@"\t", "\t"); 您可能需要考虑在将数据插入数据库之前是否应该进行此替换,而不是在获取数据时,因为您可能只插入一次但是多次获取它。 另一方面,如果您在客户端上执行此操作,则会从服务器卸载工作。 但是,如果他们没有启用Javascript,它将看起来不对。 In .NET you can use String.Replace: s = s.Replace(@ ... -
您可以检测到按键,如果该字符为G 7或N,则可以添加新行“\ n” 您可以查看此主题以了解如何检测按键。 在python中轮询键盘(检测按键) 查看底部答案,它包括一个易于检测印刷机的包装 更新 您将需要使用一些按键检测或某些东西而不是while循环来“动态”执行此操作,但我认为这将使您开始; (虽然这是有效的,而()只是在那里显示onpress或你想出的应该是什么,可以删除,脚本工作正常) import sys #you need to change while() with on_press from ...
-
是的,您是对的,您正在处理特定于平台的编码换行符的方法: 在Windows平台中,换行符通常编码为\r\n 在Linux / Unix / OS X中,换行符通常编码为\n 同样,Web浏览器倾向于支持这些约定:IE使用\r\n换行,而Safari和Firefox使用\n 。 作为解决方案,考虑使用知道不同新行编码的Python函数,例如提供通用换行支持。 例如,如果要将字符串拆分为行,请使用拆分线: lines = r.splitlines() Yes, you are correct, you are ...
-
我的textarea每次提交页面时都会创建“\ n”“\ n”字符(My textarea creates “\n” “\r” characters every time page is submitted)[2022-02-21]
您的正则表达式实际上是在搜索回车符和新行( \n\r )。 你可能想用它们的字符串形式搜索它们。 preg_replace('~\\r\\n~','
', $string); Your regex is literally searching for carriage returns and new lines (\n\r). you probably wanted to search for them in their string form. preg_replace('~\\r\\n~',' ...