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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:04:46+00:00 2026-05-25T13:04:46+00:00

I am aiming to create a summary page of activity on an application I

  • 0

I am aiming to create a summary page of activity on an application I am currently working on. I have identified that I must do the following:

Get all stories from people I am subscribed to and format them like the following:

[Username] has posted [StoryName] - View story

Get all stories that users I am connected to have posted comments on

[Username] has posted a comment on [StoryName] - View story

I am unsure how I can get both arrays, display them the format I want but order them by the posted date (in the same way people like Facebook do)

What is the best way to go about this?

Please Note: The answer must be something which is easily extendible. I am considering following wordpress’ model and creating a Posts table which has a Post Type field.

  • 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-25T13:04:46+00:00Added an answer on May 25, 2026 at 1:04 pm

    What you’re trying to do is pretty much built-in with CakePHP. The big thing is to make sure that your models are properly associated. Once this is accomplished, Cake will do most of the heavy lifting for you.

    For your situation, use 3 models associated like this:

    class Story extends AppModel{
        var $belongsTo = 'Author';
        var $hasMany = 'Comment';
    }
    
    class Author extends AppModel{
        var $hasMany = array( 'Story', 'Comment' );
        var $hasAndBelongsToMany = 'User';
    }
    
    class Comment extends AppModel{
        var $belongsTo = array( 'Author', 'Story' );
    }
    

    Set up your tables according to the Cake conventions. Then a little CakePHP magic in your controller:

    $this->Story->Author->bindModel( array( 'hasOne' => array( 'AuthorsUsers' ) ) );
    $myAuthors = $this->Story->Author->find( 'list', array(
        'fields' => array( 'id' ),
        'conditions' => array( 'AuthorsUsers.user_id' => $userId ),
        'recursive' => false 
        ) );
    
    $stories = $this->Story->find( 'all', array(
        'fields' => array( 'Story.id', 'Story.title', 'Author.id', 'Author.name' ),
        'order' => 'published_date DESC',
        'conditions' => array( 'Author.id' => $myAuthors ),
        'recursive' => 2
        ) );
    

    Quick explanation of what’s going on:

    • bindModel() lets Cake know that you want to use the HABTM association to find Authors by the associated User id. (Cake convention is to have a table called ‘authors_users’ to join the Author->User HABTM association.)
    • If you debug the $myAuthors variable, you’ll see that it gets a simple array of ids.
    • 'conditions' => array( 'field' => array() ) will get parsed as “WHERE field IN (…)”. In this example, we get all models WHERE 'Author.id' IN $myAuthors.
    • The short $this->Story->find() call is the beauty of Cake. It will automatically find all Story models matching the specified conditions, and it will find the other models associated with each found Story. (You can tell it not to find associated models by turning off Recursive or using Containable behavior.)
    • Debugging the $stories variable will show you a structure like the following:
    Array
    (
        [0] => Array
            (
                [Story] => Array
                    (
                        [id] => 1
                        [title] => 'Common Sense'
                        [published_date] => 1776-01-10 
                    )
                [Author] => Array
                    (
                        [id] => 1
                        [name] => 'Thomas Paine'
                    )
                [Comment] => Array
                    (
                        [0] => Array
                            (
                                [id] => 1
                                [text] => 'Revolutionary!'
                                [Author] => Array
                                    (
                                        [id] => 3
                                        [name] => 'Silence Dogood'
                                    )
                            )
                        [1...n] => ...
                    )
            )
        [1...n] => ...
    )
    

    You can then use that structure in your View to display data as you desire.

    I have a feeling there should be a way to do it with just 1 query, but this way works and doesn’t require you to do any subqueries or insert custom SQL into your Cake find() call.

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

Sidebar

Related Questions

I am aiming to create a .bat launcher that will execute a command line
Im aiming to create an app that allows me to download multiple images from
I am aiming for clean URL's in my WP site, currently I have the
I am aiming to create a FQL query to retrieve all of my friends
I have some code that currently builds up an In statement in SQL. I
I'm working on a tool to simplify an application's deployment. Hence I'm aiming to
i am aiming to create an Excel file for the user to download via
I am aiming to find out different security concerns that can occur on Android
I'm aiming to write an application for galaxy tab and decide to use platform
I'm working on a small java project aiming to transform a BitSet into several

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.