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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:38:11+00:00 2026-06-12T00:38:11+00:00

I have an array that I am passing to my view and it’s joined

  • 0

I have an array that I am passing to my view and it’s joined with multiple tables. Let’s call this array Submissions. All submissions contain SubmissionVotes and a typical data submission object looks like this:

 [1] => Array
        (
            [Submission] => Array
                (
                    [user_id] => 17
                    [title] => Yo mama so fat when she went to the movies she sat next to everyone
                    [source] => http://www.ahajokes.com/ym01.html
                    [slug] => yo-mama-so-fat-when-she-went-to-the-movies-she-sat-next
                    [category] => funny
                    [created] => 2012-09-26 21:00:35
                    [id] => 104
                )

            [User] => Array
                (
                    [id] => 17
                    [username] => bob_cobb
                )

            [SubmissionsVote] => Array
                (
                    [0] => Array
                        (
                            [id] => 323
                            [user_id] => 2
                            [submission_id] => 104
                            [vote_type] => up
                            [when] => 0000-00-00 00:00:00
                            [voted_ip] => 842844107
                        )

                    [1] => Array
                        (
                            [id] => 322
                            [user_id] => 17
                            [submission_id] => 104
                            [vote_type] => up
                            [when] => 0000-00-00 00:00:00
                            [voted_ip] => 1163843117
                        )

                )

            [SubmissionThumbnails] => Array
                (
                    [0] => Array
                        (
                            [id] => 6
                            [submission_id] => 104
                            [original] => http://www.ahajokes.com/g/smlogo.gif
                            [extension] => jpg
                            [slug] => http://www.ahajokes.com/g/smlogo.gif
                        )

                )

        )

Right now I’m just doing a foreach through this in my view and grabbing: $submission['Submission']['id'], $submission['Submission']['category'] and other useful stuff from it. This is all fine, but I’d like to somehow be able to just make a new object (or something equally as intuitive) that has the number of up vote_type and number of down vote_type so that I can just be like $submission['Submission']['upVotes'] / $submission['Submission']['downVotes'].

I’m doing the join in CakePHP and here is my query:

        $this->find('all', array(
                'fields' => array(
                    'Submission.user_id',
                    'Submission.title',
                    'Submission.source',
                    'Submission.slug',
                    'Submission.category',
                    'Submission.created',
                    'User.id',
                    'User.username'),
                'order' => 'Submission.created DESC'));

The only reason I want to do this is because going through and having to add up all upvotes and downvotes within my view would be quite the pain, not to mention pretty sloppy.

How can I improve this?

  • 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-12T00:38:13+00:00Added an answer on June 12, 2026 at 12:38 am

    You could try using virtual fields: http://book.cakephp.org/2.0/en/models/virtual-fields.html.

    In your Submission model, you would have something similar to:

    public $virtualFields = array(
        'up_count' => 'SELECT COUNT(*) FROM submission_votes AS up_count WHERE submission_id = Submission.id AND vote_type = "up"',
        'down_count' => 'SELECT COUNT(*) FROM submission_votes AS down_count WHERE submission_id = Submission.id AND vote_type="down"',
    );
    

    In the above example, submission_votes is your database table name for your SubmissionVote model. Be sure to heed the warning: http://book.cakephp.org/2.0/en/models/virtual-fields.html#limitations-of-virtualfields.

    If you just want to manipulate the data array, another option might be to use the something from the Hash class, such as Hash::reduce.

    http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::reduce

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

Sidebar

Related Questions

Hello I have an array that looks like this after passing it through uksort()
I have an array that looks something like this: Array ( [0] => apple
I have an array that looks like this: [ Object actions: Array[2] comments: Object
I have one array that is filled by mysql_query. Values that are in this
I have an Array that contains some elements multiple times. Now I want to
I have this array that I am needing to insert into the database. My
I have an array that was populated by this script : var imgs =
I have a view spec that was passing but is broken now that pagination
I am passing a list to a view. My array looks like this: [football,
I have a array of info that has multiple Names that I want to

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.