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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:03:49+00:00 2026-05-22T23:03:49+00:00

I currently have two models: Product and Service. Both share the same table structure

  • 0

I currently have two models: Product and Service. Both share the same table structure and the same methods. However, when I update one method, I’ll have to do the same with the other model. And it gets messy, not to mention I’m causing redundancy and it’s not the best practice available.

I know the models can be linked to the same table, so I’ll just have to add a $useTable = 'stuff' in the model and I could add a type field. However, I can’t figure out how to create a “parent” model since each model extends the AppModel.

To be fair, I know almost next to nothing about OOP, so I could be missing something very obvious and simple.

What I would do without knowing how to extend two children (Product and Service) to a parent (Stuff) would be something like:

$this->Stuff->type = 'product';
$products = $this->Stuff->findAvailable();

But I bet there’s a much simpler, cleaner approach.

Thanks.

  • 1 1 Answer
  • 1 View
  • 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-22T23:03:50+00:00Added an answer on May 22, 2026 at 11:03 pm

    Use a Behavior. As stated in the cookbook:

    Model behaviors are a way to organize some of the functionality defined in CakePHP models. They allow us to separate logic that may not be directly related to a model, but needs to be there. By providing a simple yet powerful way to extend models, behaviors allow us to attach functionality to models by defining a simple class variable. That’s how behaviors allow models to get rid of all the extra weight that might not be part of the business contract they are modeling, or that is also needed in different models and can then be extrapolated.

    http://book.cakephp.org/view/1071/Behaviors

    The link should give you a pretty good idea of where to start.

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

Sidebar

Related Questions

I have two ActiveRecord models of the same class: #1: represents the current committed
I (currently) have two forms, one that needs to call the other. In other
I currently have two models: Campaigns and Videos. Videos belongs to Campaigns and a
I'm trying to get my head around this one: Say you have two models
I currently have a Rails 3.0.10 app and I made two models with simple
Currently i have two models: Rate and Item Rate is a model for voting,
I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I have two related validations on separate models. I can validate one way but
I have two models: Person and Relation. The second one stores information about relations
I have two models, Story and Chapter . A story has_many chapters, one of

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.