{"id":3331,"date":"2014-03-23T02:20:58","date_gmt":"2014-03-23T02:20:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/virtual-fields-as-aliases-to-other-model-field-sql-unknown-column-error-collection-of-common-programming-errors-2\/"},"modified":"2014-03-23T02:20:58","modified_gmt":"2014-03-23T02:20:58","slug":"virtual-fields-as-aliases-to-other-model-field-sql-unknown-column-error-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/virtual-fields-as-aliases-to-other-model-field-sql-unknown-column-error-collection-of-common-programming-errors-2\/","title":{"rendered":"Virtual Fields as aliases to other Model field &#8211; SQL: Unknown Column Error-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m new to an existing Cake project where we are trying to use a virtualField within a model to alias another model field. In Context:<\/p>\n<pre><code>class Product extends AppModel {\n\n    var $name = 'Product';\n    var $hasOne = array('ProductPrice');\n\n    var $virtualFields = array(\n        'price' =&gt; 'ProductPrice.current_price'\n    );\n\n    \/\/ Typical fields in the database for Product.  id, name, etc.\n}\n\nclass ProductPrice extends AppModel {\n    var $name = 'ProductPrice';\n    var $belongsTo = array('Product');\n\n    \/\/  Fields are product_id, current_price\n}\n<\/code><\/pre>\n<p>The ProductPrice model is for a view in the database that contains different price tiers with a current_price column allowing retrieval of the current price of the product. When accessing the Product model via something like:<\/p>\n<pre><code>$this-&gt;Product-&gt;find('all' ....);\n<\/code><\/pre>\n<p>I don&#8217;t have any problems with getting the price field. The problem is if the query to Product is done indirectly through something like<\/p>\n<pre><code>$this-&gt;Inventory-&gt;find('all');\n<\/code><\/pre>\n<p>We get:<br \/>\n<code>SQL Error: 1054: Unknown column 'ProductPrice.current_price' in 'field list' [CORE\/cake\/libs\/model\/datasources<\/code>\/dbo_source.php, line 681]<\/p>\n<p>I know that the issue is that the SQL generated by the Inventory query does not attempt to join the ProductPrice view. I assumed that this would happen automagically via the Product model as it knows it &#8220;hasOne&#8221; ProductPrice.<\/p>\n<p>I&#8217;ve tried setting &#8220;recursive&#8221; on the Inventory model to 2,1, etc. with no success.<\/p>\n<p>What am I missing?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m new to an existing Cake project where we are trying to use a virtualField within a model to alias another model field. In Context: class Product extends AppModel { var $name = &#8216;Product&#8217;; var $hasOne = array(&#8216;ProductPrice&#8217;); var $virtualFields = array( &#8216;price&#8217; =&gt; &#8216;ProductPrice.current_price&#8217; ); \/\/ Typical fields in the database for Product. id, [&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-3331","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3331","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=3331"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3331\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}