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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:46:34+00:00 2026-05-28T02:46:34+00:00

For example, I have this relationship: UserContact hasMany Contact Contact hasOne Info Contact hasMany

  • 0

For example, I have this relationship:

UserContact hasMany Contact
Contact hasOne Info
Contact hasMany Response

And I need to paginate Contact, so I use Containable:

$this->paginate = array(
            'limit'=>50,
            'page'=>$page,
            'conditions' =>array('Contact.id'=>$id),
            'contain'=>array(
                'Response',
                'Info'
                )
            );

I want to add search by Info.name, and by Response.description. It works perfect for Info.name, but it throws an error if I try using Response.description, saying that the column doesn’t exist.

Additionally, I tried changing the relationship to Contact hasOne Response, and then it filters correctly, but it only returns the first response and this is not the correct relationship.

So, for example, if I have a search key $filter I’d like to only return those Contacts that have a matching Info.name or at least one matching Response.description.

  • 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-28T02:46:34+00:00Added an answer on May 28, 2026 at 2:46 am

    If you look at how CakePHP constructs SQL queries you’ll see that it generates contained “single” relationships (hasOne and belongsTo) as join clauses in the main query, and then it adds separate queries for contained “multiple” relationships.

    This makes filtering by a single relationship a breeze, as the related model’s table is already joined in the main query.

    In order to filter by a multiple relationship you’ll have to create a subquery:

    // in contacts_controller.php:
    $conditionsSubQuery = array(
      'Response.contact_id = Contact.id',
      'Response.description LIKE' => '%'.$filter.'%'
    );
    $dbo = $this->Contact->getDataSource();
    $subQuery = $dbo->buildStatement(array(
        'fields' => array('Response.id'),
        'table' => $dbo->fullTableName($this->Contact->Response),
        'alias' => 'Response',
        'conditions' => $conditionsSubQuery
    ), $this->Contact->Response);
    $subQuery = ' EXISTS (' . $subQuery . ') ';
    
    $records = $this->paginate(array(
        'Contact.id' => $id,
        $dbo->expression($subQuery)
    ));
    

    But you should only generate the subquery if you need to filter by a Response field, otherwise you’ll filter out contacts that have no responses.

    PS. This code is too big and ugly to appear in the controller. For my projects I refactored it into app_model.php, so that each model can generate its own subqueries:

    function makeSubQuery($wrap, $options) {
        if (!is_array($options))
            return trigger_error('$options is expected to be an array, instead it is:'.print_r($options, true), E_USER_WARNING);
        if (!is_string($wrap) || strstr($wrap, '%s') === FALSE)
            return trigger_error('$wrap is expected to be a string with a placeholder (%s) for the subquery. instead it is:'.print_r($wrap, true), E_USER_WARNING);
    
        $ds = $this->getDataSource();
    
        $subQuery_opts = array_merge(array(
            'fields' => array($this->alias.'.'.$this->primaryKey),        
            'table' => $ds->fullTableName($this),        
            'alias' => $this->alias,   
            'conditions' => array(),     
            'order' => null, 
            'limit' => null,
            'index' => null, 
            'group' => null
        ), $options);
    
        $subQuery_stm = $ds->buildStatement($subQuery_opts, $this);
        $subQuery = sprintf($wrap, $subQuery_stm);
        $subQuery_expr = $ds->expression($subQuery);
        return $subQuery_expr;
    }
    

    Then the code in your controller becomes:

    $conditionsSubQuery = array(
        'Response.contact_id = Contact.id',
        'Response.description LIKE' => '%'.$filter.'%'
    );
    $records = $this->paginate(array(
        'Contact.id' => $id,
        $this->Contact->Response->makeSubQuery('EXISTS (%s)', array('conditions' => $conditionsSubQuery))
    ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example i have this xml. I need to get value of parameter val
For example we have this line chart at Google Code API there is a
OK. For example I have this line in my txt file: 1|1,12;7,19;6,4;8,19;2,2 As you
How would I split a node/element at a position (selection). Example I have this
I am reading a file byte-by-byte. Say for example i have this byte: 0x41
I have this example FactoryGirl.define do @site = FactoryGirl.create(:my_site) factory :user do email {
I have this example: public class Inheritance { public static class Animal { public
Suppose we have this example: http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html with source code available here: http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/ How can
jquery.ui.position is not taking the margin values when positioning items? I have this example,
I have this URL example.com/photo?people I want to rewrite this URL in the form

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.