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 6699267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:40:30+00:00 2026-05-26T06:40:30+00:00

I am using a virtual field in a model. That field represents the sum

  • 0

I am using a virtual field in a model. That field represents the sum of several fields: SUM(Model.amount_1 + Model.amount_2 + ...). This is the schema:

create table `current_transfers` (
  `id` integer unsigned not null auto_increment primary key,
  `description` varchar(1024) not null,
  `product_amount` decimal(13,2) default 0.0 not null,
  `tax_amount` decimal(13,2) default 0.0 not null,
  `other_amount` decimal(13,2) default 0.0 not null,
  `record_id` integer unsigned not null,
  constraint foreign key (`record_id`) references `records`(`id`) on delete cascade,
) ENGINE=InnoDB;

And this is the definition of the virtual field in CurrentTransfer model:

var $virtualFields = array(
  'amount' => 'SUM(
    CurrentTransfer.product_amount +
    CurrentTransfer.tax_amount +
    CurrentTransfer.other_amount)'
);

Let’s say the table does not have any records yet and you use $this->paginate() in RecordsController to retrieve a list of results, like this:

class RecordsController extends AppController {

  var $name = 'Records';

  var $paginate = array(
    'CurrentTransfer' => array(
      'order' => array('CurrentTransfer.amount' => 'desc'),
    ),
  );

  // ...

  public function view($id) {
    // ...
    $this->paginate('CurrentTransfer', array('CurrentTransfer.record_id' => $id));
    // ...
  }

  // ...
}

I have found two different behaviors:

  • If the virtual field is actually defined in the model the result is an array with one element storing an empty record, i.e. every field is set to NULL.
  • If the virtual field is not defined in the model the result is an empty array, which is the common behavior in CakePHP when a query returns an empty set of values.

I would always expect the second behavior, since is easier to compare if there are results or not.

Why is CakePHP returning a fictitious result when there are actually no records in the table? Why the addition of virtual fields produce such behavior?

  • 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-05-26T06:40:31+00:00Added an answer on May 26, 2026 at 6:40 am

    SUM() is an aggregate function that sums up all rows in a column, not all columns in a row as you probably want. As an aggregate function, it always returns a value, at least NULL. That means the SQL query returns at least one row with all columns set to NULL. Cake is simply picking this up.

    I think you’re actually looking for this:

    public $virtualFields = array(
      'amount' => 'CurrentTransfer.product_amount + CurrentTransfer.tax_amount + CurrentTransfer.other_amount'
    );
    

    I.e. the simple + operator without the SUM().

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

Sidebar

Related Questions

My problem is that iam using admin generator for model with some extra virtual
I'm working on a graphics application that is using virtual classes fairly extensively. It
I want to implement my own clustering algorithm using this Virtual Earth javascript API:
I have this code to show a map using the Virtual Earth API: <script
I have a model like this: public PurchaseOrder { [Required] [StringLength(15)] public virtual string
I have an entity model that has audit information on every table (50+ tables)
This time I have problem with virtual fields. I have core class for my
This time I have problem with virtual fields. I have core class for my
I'm using a preDqlSelect() callback to add a virtual field. But the validation of
I am using CakePHP 2.0 and I have a Model with this 'virtualFields': Country.php:

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.