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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:34:59+00:00 2026-06-12T06:34:59+00:00

I would like to add some properties and methods to a Doctrine model class,

  • 0

I would like to add some properties and methods to a Doctrine model class, so that every time an instance of this class is created, the properties are automatically set once, and can be accessed with the related getter, but these values are not stored in the database, instead they are calculated from other properties (these stored in the db) of the same class.

For instance let’s say I have this class MyModel in the schema.yml:

MyModel:
  actAs:
    Timestampable: ~
  tableName: my_model
  columns:
    id:         { type: integer(8), primary: true, notnull: true, autoincrement: true }
    quantity:   { type: decimal(12), scale: 3, notnull: true }
    price:      { type: decimal(12), scale: 3, notnull: true }

often I need to know the total amount, but I don’t want to store it in the database: I could just do

class MyModel extends BaseMyModel
{
  public function getTotalAmount()
  {
    $this->total_amount = $this->getQuantity() * $this->getPrice(); 
  }    
}

and then every time I need to know the total amount, I can just call $my_model->getTotalAmount().

However, I would like to have something like this

class MyModel extends BaseMyModel
{
  public function setTotalAmount()
  {
    $this->total_amount = $this->getQuantity() * $this->getPrice(); 
  }

  public function getTotalAmount()
  {
    return $this->total_amount;
  }
}

and when creating a new instance of the MyModel class like

$my_model = Doctrine_Core::getTable("MyModel")->find(1);

I would like the setTotalAmount() function to be executed automatically, so the total_amount value is calculated once and can be accessed with the $my_model->getTotalAmount() without having to recalculate it every time the function is called.

Any suggestion?

  • 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-12T06:35:00+00:00Added an answer on June 12, 2026 at 6:35 am

    This is just plain wrong:

    class MyModel extends BaseMyModel
    {
      public function setTotalAmount()
      {
        $this->total_amount = $this->getQuantity() * $this->getPrice(); 
      }
    
      public function getTotalAmount()
      {
        return $this->total_amount;
      }
    }
    

    Setting a value means actually passing a parameter, and updating related field. Even if you update such value at hydration time, what if you change such values later, and you access getTotalAmount? Should you call setTotalAmount every time you update anything? No way.

    class MyModel extends BaseMyModel
    {
      public function getTotalAmount()
      {
        $this->total_amount = $this->getQuantity() * $this->getPrice(); 
      }    
    }
    

    This is totally right – you get a value based on two stored values, with maximum consistence.

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

Sidebar

Related Questions

I'd like to add some custom instance properties to an ASP.Net User object. for
I would like to add some background music to the splash screen in the
I would like to add some paragraphs or new lines or words dynamically but
I would like to add some custom data to an image the user generates
I would like to add some data to the body of a http request
I would like to add some interactive capability to a python CLI application I've
I would like to add some debugs for my simple ruby functions and I
I would like to add some external .dll libraries e.g. glut32.dll (but it's only
I have two doubts: 1. I would like to add some client side support
I would like to add an element to jquery.colorbox.js(want to do some fancey styling)

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.