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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:46:47+00:00 2026-06-06T14:46:47+00:00

I’m using the cakephp framework to develop an application and I’m running into some

  • 0

I’m using the cakephp framework to develop an application and I’m running into some trouble understanding the associations between these models fully. Below you can see the four models along with their relative database fields.

User
 id

Profile
 id
 user_id

Post (A blog post on the users profile)
 id
 profile_id
 topic_id

Topic (A topic for a blog post)
 id
 name

Here are the associations as they currently stand:

User
 hasOne: Profile

Profile
 hasMany: Posts

Post
 belongsTo: Topic, Profile

Now my problem. I am unsure if you have to define associations like User hasMany Posts or if it’s already assumed because User hasOne Profile and Profile hasMany Posts. My other problem is defining the relationship between a post and its topic.

  • A profile can have unlimited posts
  • A post must be associated with a profile
  • A post can only have one topic
  • The topic table contains a list of all topics
  • A post does not NEED a topic

Given these criteria how should my associations look? All the research I’ve done on associations only shows simple examples.

I’m using CakePHP version 2.1.3

Thanks for any and all help and/or advice in advance

  • 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-06T14:46:48+00:00Added an answer on June 6, 2026 at 2:46 pm

    You can recursively find associations of associations, or even better, use Containable.

    In the model (I recommend putting it in AppModel, since I find myself using Containable for everything):

    class AppModel extends Model {
        public $actsAs = array('Containable');
        ...
    }
    

    Then when you call read (or find, or paginate) for User, most likely in your controller, do this:

    $this->User->contain(array(
        'Profile' => array(
            'Post'
        )
    ));
    $data = $this->User->read();
    $set('user',$data);
    

    If you set that data to your view, you can then access the id of one of the posts from $user['Profile']['Post'][0]['id'].

    Now for your next question, you can have conditional associations.

    public $hasMany = array(
        'Topic' => array(
            'className' => 'Topic',
            'conditions' => 'Post.topic_id IS NOT NULL'
        )
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.