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
  • 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-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 models, say Product and Bundle. products table is related to product_prices
Currently I have two MediaWikis, one a slightly older version than the other. I
I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I currently have two text boxes which accept any number. I have a text
All, I currently have two projects that are under SourceSafe that I am unable
I currently have an asp.net website hosted on two web servers that sit behind
I currently have speakers set up both in my office and in my living
I have a model Product it has two fields size & colours among others
I want to have two separate forms on a single create page and one
I have two roles being used on my site currently, author & admin. Authors

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.