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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:31:17+00:00 2026-05-18T12:31:17+00:00

I have the next question. There is part of code: $result = $this->find(‘all’, array(

  • 0

I have the next question.

There is part of code:

        $result = $this->find('all', array(
            'contain' => array(
                'User' => array(
                    'fields' => 'id',
                    'conditions' => array(
                        'id' => $user_id
                    )
                )
            ),
            'fields' => 'url'
        ));

This find() called in method of Project model class and I want to retrieve just projects where User.id equal some user id passed to my method.
User HABTM Project and Project HABTM User.

But after executing I have the next:


app/views/projects/index.ctp (line 1)

Array
(
    [0] => Array
        (
            [Project] => Array
                (
                    [url] => http://purpled.biz
                    [id] => 1
                )

            [User] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [ProjectsUser] => Array
                                (
                                    [user_id] => 4
                                    [project_id] => 1
                                    [projects_users_role_id] => 0
                                )

                        )

                )

        )

    [1] => Array
        (
            [Project] => Array
                (
                    [url] => http://google.com
                    [id] => 2
                )

            [User] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [ProjectsUser] => Array
                                (
                                    [user_id] => 4
                                    [project_id] => 2
                                    [projects_users_role_id] => 0
                                )

                        )

                )

        )

    [2] => Array
        (
            [Project] => Array
                (
                    [url] => http://test.com
                    [id] => 3
                )

            [User] => Array
                (
                )

        )

)

As you see there is the last array with empty [User] array, so how can I exclude this empty array (whole [2] array I mean) without using foreach/if blocks? I know that using bindModel will do all as well but this is not for my question 😉 So, how?

  • 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-18T12:31:17+00:00Added an answer on May 18, 2026 at 12:31 pm

    Placing your conditions within the ‘contain’ key will filter the results within that particular model.

    Retrieving the right data
    I’ve answered a question before very similar to this here. You would just need to replace the models mentioned there with your own models. If you have trouble understanding the answer there, let us know.

    Tips for your current model structure
    I notice that your ProjectsUser model has an extra field “projects_users_role_id”. When your Join Model is complex (more than just id, model1_id, model2_id), then you need to represent it by a model in itself. It’s generally referred to as a hasMany Through relationship.

    User hasMany ProjectsUser
    Project hasMany ProjectsUser
    ProjectsUserRole hasMany ProjectsUser
    
    ProjectsUser belongsTo User
    ProjectsUser belongsTo Project
    ProjectsUser belongsTo ProjectsUserRole
    

    You could also rename “ProjectsUser” to something nicer like “ProjectMembership”.

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

Sidebar

Related Questions

Ok, firstly I have the following code working.. although my question is this; should
This is probably a multi-part question. Background: we have a native (c++) library that
I have the following code: if ($_POST['submit'] == Next) { foreach($_POST['info'] as $key =>
This question comes from a range of other questions that all deal with essentially
This is a two-part question about adding a third-party library (JAR) to an Android
I'm going to start of by noting that I have next to no python
I have the next function: function setImagesWidth(id,width) { var images = document.getElementById(id).getElementsByTagName(img); for(var i
In my fieldset I have labels next (side) to my textboxes, but for some
How could I best implement the following scenario? I have a Next Period... button
I have First/Last/Previous/Next buttons that change the selected child node of a TreeViewItem. Setting

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.