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

  • Home
  • SEARCH
  • 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 67197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:13:20+00:00 2026-05-10T19:13:20+00:00

What is the best way of working with calculated fields of Propel objects? Say

  • 0

What is the best way of working with calculated fields of Propel objects?

Say I have an object ‘Customer’ that has a corresponding table ‘customers’ and each column corresponds to an attribute of my object. What I would like to do is: add a calculated attribute ‘Number of completed orders’ to my object when using it on View A but not on Views B and C.

The calculated attribute is a COUNT() of ‘Order’ objects linked to my ‘Customer’ object via ID.

What I can do now is to first select all Customer objects, then iteratively count Orders for all of them, but I’d think doing it in a single query would improve performance. But I cannot properly ‘hydrate’ my Propel object since it does not contain the definition of the calculated field(s).

How would you approach it?

  • 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. 2026-05-10T19:13:20+00:00Added an answer on May 10, 2026 at 7:13 pm

    There are several choices. First, is to create a view in your DB that will do the counts for you, similar to my answer here. I do this for a current Symfony project I work on where the read-only attributes for a given table are actually much, much wider than the table itself. This is my recommendation since grouping columns (max(), count(), etc) are read-only anyway.

    The other options are to actually build this functionality into your model. You absolutely CAN do this hydration yourself, but it’s a bit complicated. Here’s the rough steps

    1. Add the columns to your Table class as protected data members.
    2. Write the appropriate getters and setters for these columns
    3. Override the hydrate method and within, populate your new columns with the data from other queries. Make sure to call parent::hydrate() as the first line

    However, this isn’t much better than what you’re talking about already. You’ll still need N + 1 queries to retrieve a single record set. However, you can get creative in step #3 so that N is the number of calculated columns, not the number of rows returned.

    Another option is to create a custom selection method on your TablePeer class.

    1. Do steps 1 and 2 from above.
    2. Write custom SQL that you will query manually via the Propel::getConnection() process.
    3. Create the dataset manually by iterating over the result set, and handle custom hydration at this point as to not break hydration when use by the doSelect processes.

    Here’s an example of this approach

    <?php  class TablePeer extends BaseTablePeer {     public static function selectWithCalculatedColumns()     {         //  Do our custom selection, still using propel's column data constants         $sql = '             SELECT ' . implode( ', ', self::getFieldNames( BasePeer::TYPE_COLNAME ) ) . '                  , count(' . JoinedTablePeer::ID . ') AS calc_col               FROM ' . self::TABLE_NAME . '               LEFT JOIN ' . JoinedTablePeer::TABLE_NAME . '                 ON ' . JoinedTablePeer::ID . ' = ' . self::FKEY_COLUMN         ;          //  Get the result set         $conn   = Propel::getConnection();         $stmt   = $conn->prepareStatement( $sql );         $rs = $stmt->executeQuery( array(), ResultSet::FETCHMODE_NUM );          //  Create an empty rowset         $rowset = array();          //  Iterate over the result set         while ( $rs->next() )         {             //  Create each row individually             $row = new Table();             $startcol = $row->hydrate( $rs );              //  Use our custom setter to populate the new column             $row->setCalcCol( $row->get( $startcol ) );             $rowset[] = $row;         }         return $rowset;     } } 

    There may be other solutions to your problem, but they are beyond my knowledge. Best of luck!

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

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Should I use this Priority flag rather than implementing another… May 11, 2026 at 10:15 am
  • added an answer Oh well - I've tried to solve that problem myself.… May 11, 2026 at 10:15 am
  • added an answer There are limits to how many notifications can be sent… May 11, 2026 at 10:15 am

Related Questions

What is the best way of working with calculated fields of Propel objects? Say
What is the best way of creating an alphabetically sorted list in Python?
What is the best way of dynamically writing LINQ queries and Lambda expressions? I
What is the best way of doing this? tmpnam() returns a path to a
what is the best way of displaying/using the revision number in a java webapp?
What is the best way of doing case-insensitive string comparison in C++ without transforming
What is the best way of handling trying to get data from a DataReader
What is the best way of referencing a dynamic delegate. I'd like to do
What is the best way of taking a query and transforming it into a
What is the best way of converting a multi-dimensional javascript array to JSON?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.