{"id":831,"date":"2022-08-30T15:07:54","date_gmt":"2022-08-30T15:07:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-get-total-price-from-estimate-to-show-up-in-index-ctp-using-cakephp-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:07:54","modified_gmt":"2022-08-30T15:07:54","slug":"how-to-get-total-price-from-estimate-to-show-up-in-index-ctp-using-cakephp-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-get-total-price-from-estimate-to-show-up-in-index-ctp-using-cakephp-collection-of-common-programming-errors\/","title":{"rendered":"How to get total price from estimate to show up in index.ctp using CakePHP-Collection of common programming errors"},"content":{"rendered":"<p>I have Estimates hasMany EstimateDetails hasMany Items. What I&#8217;m trying to do is get a total on my estimates Index view.<\/p>\n<p>The Debug of $estimates on index.ctp:<\/p>\n<pre><code>Array\n(\n[0] =&gt; Array\n    (\n        [Estimate] =&gt; Array\n            (\n                [id] =&gt; 1\n                [name] =&gt; City Base Cinema 10\n                [employee_id] =&gt; 1\n                [created] =&gt; 2011-07-12 18:08:37\n                [modified] =&gt; 2011-07-12 18:08:37\n            )\n\n        [Employee] =&gt; Array\n            (\n                [id] =&gt; 1\n                [first_name] =&gt; Edward\n                [last_name] =&gt; Ramon\n                [phone] =&gt; 2106959672\n                [hourly_pay] =&gt; 0.00\n                [position_id] =&gt; 2\n                [created] =&gt; 2011-07-12 17:56:42\n                [modified] =&gt; 2011-07-12 17:56:42\n                [fullname] =&gt; Edward Ramon\n                [Position] =&gt; Array\n                    (\n                        [id] =&gt; 2\n                        [name] =&gt; Estimator\n                    )\n\n                [Estimate] =&gt; Array\n                    (\n                        [0] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [name] =&gt; City Base Cinema 10\n                                [employee_id] =&gt; 1\n                                [created] =&gt; 2011-07-12 18:08:37\n                                [modified] =&gt; 2011-07-12 18:08:37\n                            )\n\n                    )\n\n                [Job] =&gt; Array\n                    (\n                    )\n\n            )\n\n        [EstimateDetail] =&gt; Array\n            (\n                [0] =&gt; Array\n                    (\n                        [id] =&gt; 1\n                        [estimate_id] =&gt; 1\n                        [qty] =&gt; 10\n                        [item_id] =&gt; 1\n                        [feet] =&gt; 10\n                        [inches] =&gt; 2\n                        [adjustment] =&gt; -0.20\n                        [Estimate] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [name] =&gt; City Base Cinema 10\n                                [employee_id] =&gt; 1\n                                [created] =&gt; 2011-07-12 18:08:37\n                                [modified] =&gt; 2011-07-12 18:08:37\n                            )\n\n                        [Item] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [type] =&gt; W\n                                [eje] =&gt; W\n                                [description] =&gt; 44x335\n                                [price unit] =&gt; perpound\n                                [weight] =&gt; 335\n                                [price] =&gt; 0.80\n                                [fulldesc] =&gt; W44x335\n                            )\n\n                    )\n\n                [1] =&gt; Array\n                    (\n                        [id] =&gt; 3\n                        [estimate_id] =&gt; 1\n                        [qty] =&gt; 3\n                        [item_id] =&gt; 1\n                        [feet] =&gt; 4\n                        [inches] =&gt; 5\n                        [adjustment] =&gt; 0.00\n                        [Estimate] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [name] =&gt; City Base Cinema 10\n                                [employee_id] =&gt; 1\n                                [created] =&gt; 2011-07-12 18:08:37\n                                [modified] =&gt; 2011-07-12 18:08:37\n                            )\n\n                        [Item] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [type] =&gt; W\n                                [eje] =&gt; W\n                                [description] =&gt; 44x335\n                                [price unit] =&gt; perpound\n                                [weight] =&gt; 335\n                                [price] =&gt; 0.80\n                                [fulldesc] =&gt; W44x335\n                            )\n\n                    )\n\n                [2] =&gt; Array\n                    (\n                        [id] =&gt; 4\n                        [estimate_id] =&gt; 1\n                        [qty] =&gt; 10\n                        [item_id] =&gt; 1\n                        [feet] =&gt; 10\n                        [inches] =&gt; 10\n                        [adjustment] =&gt; 0.00\n                        [Estimate] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [name] =&gt; City Base Cinema 10\n                                [employee_id] =&gt; 1\n                                [created] =&gt; 2011-07-12 18:08:37\n                                [modified] =&gt; 2011-07-12 18:08:37\n                            )\n\n                        [Item] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [type] =&gt; W\n                                [eje] =&gt; W\n                                [description] =&gt; 44x335\n                                [price unit] =&gt; perpound\n                                [weight] =&gt; 335\n                                [price] =&gt; 0.80\n                                [fulldesc] =&gt; W44x335\n                            )\n\n                    )\n\n                [3] =&gt; Array\n                    (\n                        [id] =&gt; 5\n                        [estimate_id] =&gt; 1\n                        [qty] =&gt; 10\n                        [item_id] =&gt; 3\n                        [feet] =&gt; 10\n                        [inches] =&gt; 10\n                        [adjustment] =&gt; 0.00\n                        [Estimate] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [name] =&gt; City Base Cinema 10\n                                [employee_id] =&gt; 1\n                                [created] =&gt; 2011-07-12 18:08:37\n                                [modified] =&gt; 2011-07-12 18:08:37\n                            )\n\n                        [Item] =&gt; Array\n                            (\n                                [id] =&gt; 3\n                                [type] =&gt; W\n                                [eje] =&gt; W\n                                [description] =&gt; 44x290\n                                [price unit] =&gt; perpound\n                                [weight] =&gt; 290\n                                [price] =&gt; 0.80\n                                [fulldesc] =&gt; W44x290\n                            )\n\n                    )\n\n                [4] =&gt; Array\n                    (\n                        [id] =&gt; 6\n                        [estimate_id] =&gt; 1\n                        [qty] =&gt; 10\n                        [item_id] =&gt; 6\n                        [feet] =&gt; 10\n                        [inches] =&gt; 2\n                        [adjustment] =&gt; 0.00\n                        [Estimate] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [name] =&gt; City Base Cinema 10\n                                [employee_id] =&gt; 1\n                                [created] =&gt; 2011-07-12 18:08:37\n                                [modified] =&gt; 2011-07-12 18:08:37\n                            )\n\n                        [Item] =&gt; Array\n                            (\n                                [id] =&gt; 6\n                                [type] =&gt; PL\n                                [eje] =&gt; PL\n                                [description] =&gt; 4X3\n                                [price unit] =&gt; ea\n                                [weight] =&gt; 1\n                                [price] =&gt; 0.30\n                                [fulldesc] =&gt; PL4X3\n                            )\n\n                    )\n\n            )\n\n        [Job] =&gt; Array\n            (\n                [0] =&gt; Array\n                    (\n                        [id] =&gt; 1\n                        [job_number] =&gt; 2353\n                        [name] =&gt; City Base Cinema 10\n                        [company_id] =&gt; 1\n                        [estimate_id] =&gt; 1\n                        [contract_amount] =&gt; 253000\n                        [employee_id] =&gt; 2\n                        [location_id] =&gt; 1\n                        [created] =&gt; 2011-07-12 18:17:59\n                        [modified] =&gt; 2011-07-16 12:17:55\n                        [Company] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [name] =&gt; Search Construction\n                                [address] =&gt; 123 street\n                                [city] =&gt; San Antonio\n                                [state] =&gt; TX\n                                [zip] =&gt; 78023\n                                [company_type_id] =&gt; 1\n                                [created] =&gt; 2011-07-12 18:16:37\n                                [modified] =&gt; 2011-07-12 18:16:37\n                            )\n\n                        [Estimate] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [name] =&gt; City Base Cinema 10\n                                [employee_id] =&gt; 1\n                                [created] =&gt; 2011-07-12 18:08:37\n                                [modified] =&gt; 2011-07-12 18:08:37\n                            )\n\n                        [Employee] =&gt; Array\n                            (\n                                [id] =&gt; 2\n                                [first_name] =&gt; Adam\n                                [last_name] =&gt; Morales\n                                [phone] =&gt; \n                                [hourly_pay] =&gt; 20.00\n                                [position_id] =&gt; 1\n                                [created] =&gt; 2011-07-16 12:16:49\n                                [modified] =&gt; 2011-07-16 12:16:49\n                                [fullname] =&gt; Adam Morales\n                            )\n\n                        [Location] =&gt; Array\n                            (\n                                [id] =&gt; 1\n                                [address] =&gt; 456 street\n                                [city] =&gt; Helotes\n                                [state] =&gt; TX\n                                [zip] =&gt; 78023\n                                [location_type_id] =&gt; 1\n                                [full_local] =&gt; 456 street Helotes, TX 78023\n                            )\n\n                    )\n\n            )\n\n    )\n\n)\n<\/code><\/pre>\n<p>I tried setting a variableField on the Estimate Model:<\/p>\n<pre><code>var $virtualFields = array('total' =&gt; 'SUM(EstimateDetail.qty*EstimateDetail.Item.weight*EstimateDetail.Item.price)');\n<\/code><\/pre>\n<p>My Index.ctp:<\/p>\n<tr>\n<th><\/th>\n<th><\/th>\n<th><\/th>\n<th><\/th>\n<th><\/th>\n<th class=\"actions\"><\/th>\n<\/tr>\n<p>\u00a0<\/p>\n<td>\u00a0<\/td>\n<td><\/td>\n<td>\u00a0<\/td>\n<td class=\"actions\"><\/td>\n<p>and I get an error.<\/p>\n<p>The error I get is:<\/p>\n<pre><code>Warning (2): Invalid argument supplied for foreach() [APP\/views\/estimates\/index.ctp, line 15]\n<\/code><\/pre>\n<p>and the Sql:<\/p>\n<pre><code>1054: Unknown column 'EstimateDetail.qty' in 'field list\n<\/code><\/pre>\n<p>Please Help.<\/p>\n<ol>\n<li>\n<p>Short answer would be &#8211; don&#8217;t try to do what you&#8217;re doing with SQL\/<code>virtualFields<\/code> (if you want, I can elaborate on details why).<\/p>\n<p>Just iterate over your results via <code>foreach($estimates)<\/code> and calculate sum yourself in PHP.<\/p>\n<p><strong>UPDATE<\/strong>: You can&#8217;t (easily) use aggregate (<code>SUM<\/code>) in <code>find()<\/code> in your example because:<\/p>\n<ol>\n<li>for this, find should be done in one query &#8211; i.e. using <code>JOIN<\/code>s &#8211; and Cake won&#8217;t use <code>JOIN<\/code>s willingly on hasMany relationships (it does many simple <code>SELECT<\/code>s instead), which means you&#8217;d have to do them manually (http:\/\/book.cakephp.org\/view\/1047\/Joining-tables).<\/li>\n<li>query with <code>SUM<\/code> would return just one row (grouped), meaning that you&#8217;d have to do un-trivial subquery to get both <code>SUM<\/code> AND your results.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:48:01. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have Estimates hasMany EstimateDetails hasMany Items. What I&#8217;m trying to do is get a total on my estimates Index view. The Debug of $estimates on index.ctp: Array ( [0] =&gt; Array ( [Estimate] =&gt; Array ( [id] =&gt; 1 [name] =&gt; City Base Cinema 10 [employee_id] =&gt; 1 [created] =&gt; 2011-07-12 18:08:37 [modified] =&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-831","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=831"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/831\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}