Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:13:39+00:00 2026-06-02T06:13:39+00:00

I’m new to an existing Cake project where we are trying to use a

  • 0

I’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 = 'Product';
    var $hasOne = array('ProductPrice');

    var $virtualFields = array(
        'price' => 'ProductPrice.current_price'
    );

    // Typical fields in the database for Product.  id, name, etc.
}

class ProductPrice extends AppModel {
    var $name = 'ProductPrice';
    var $belongsTo = array('Product');

    //  Fields are product_id, current_price
}

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:

$this->Product->find('all' ....);

I don’t have any problems with getting the price field. The problem is if the query to Product is done indirectly through something like

$this->Inventory->find('all');

We get:
SQL Error: 1054: Unknown column 'ProductPrice.current_price' in 'field list' [CORE/cake/libs/model/datasources/dbo_source.php, line 681]

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 “hasOne” ProductPrice.

I’ve tried setting “recursive” on the Inventory model to 2,1, etc. with no success.

What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:13:40+00:00Added an answer on June 2, 2026 at 6:13 am

    TLDR:

    You cannot use fields from a different model in a VirtualField.


    Other options:

    If you’re doing a query like:

    $this->Inventory->find('all');
    

    You can use something like CakePHP’s Containable behavior to make sure you’re getting the data you want:

    //controller code
    $inv = $this->Inventory->getInventory();
    
    //model code
    class Inventory extends AppModel {
    
        public $actsAs = array('Containable');
    
        public function getInventory() {
            return $this->find('all', array(
                'contain' => array(
                    'Product' => array(
                        'ProductPrice'
                    )
                )
            ));
        }
    }
    

    Using containable like in the above code example should return the data in a format something like this:

    [0] => Array
        (
            [Inventory] => Array
                (
                    [id] => 12345
                )
            [Product] => Array
                (
                    [0] => Array
                        (
                            [id] => 54321
                            [title] => product A
                        )
                    [ProductPrice] => Array
                    (
                        [id] => 6789
                        [current_price] => 24.99
                    )
                )
    //...
    

    When you get the data like that, it should be easy to access the Product’s current price.

    You could also just do it in the controller, but it’s better practice to keep your queries in the model to stay within the “Fat Model, Skinny Controller” mantra. If you really want to keep it in your controller, you can just do this:

    $inv = $this->find('all', array(
        'contain' => array(
            'Product' => array(
                'ProductPrice'
            )
        )
    );
    

    (BUT – you still have to specify that a model $actsAs Containable (per first code example).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am confused How to use looping for Json response Array in another Array.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.