Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting CakePHP's searchable behavior results to contain deeper associations
    primarykey
    data
    text
    <p>I am trying to use CakePHP 1.3.5's <a href="http://code.google.com/p/searchable-behaviour-for-cakephp/" rel="nofollow">searchable behavior</a> with containable behavior to return search results for a specified model <em>and</em> an associated model (Article belongsTo User).</p> <p>Ignoring the searchable behavior for a moment, the following call to <strong>find()</strong>:</p> <pre><code>$this-&gt;Article-&gt;find('all', array( 'conditions' =&gt; array('Article.is_published' =&gt; 1), 'fields' =&gt; array('Article.id'), 'contain' =&gt; array('User.name') )); </code></pre> <p>Executes this SQL query:</p> <pre><code>SELECT `Article`.`id`, `User`.`name`, `User`.`id` FROM `articles` AS `Article` LEFT JOIN `users` AS `User` ON (`Article`.`user_id` = `User`.`id`) WHERE `Article`.`is_published` = 1 </code></pre> <p>And returns the following array:</p> <pre><code>Array ( [0] =&gt; Array ( [Article] =&gt; Array ( [id] =&gt; 10 ) [User] =&gt; Array ( [name] =&gt; Author Name [id] =&gt; 7 ) ) ... ) </code></pre> <hr> <p>Which is exactly what's expected. However, the following call to <strong>search()</strong>:</p> <pre><code>$this-&gt;Article-&gt;search($query, array( 'conditions' =&gt; array('Article.is_published' =&gt; 1), 'fields' =&gt; array('Article.id'), 'contain' =&gt; array('Article' =&gt; array('User.name')) )); </code></pre> <p>Executes this SQL query:</p> <pre><code>SELECT `Article`.`id` FROM `search_index` AS `SearchIndex` LEFT JOIN `articles` AS `Article` ON (`SearchIndex`.`model` = 'Article' AND `SearchIndex`.`association_key` = `Article`.`id`) WHERE `Article`.`is_published` = 1 AND MATCH(`SearchIndex`.`data`) AGAINST('search term' IN BOOLEAN MODE) AND `Article`.`id` IS NOT NULL </code></pre> <p>And returns this array:</p> <pre><code>Array ( [0] =&gt; Array ( [Article] =&gt; Array ( [id] =&gt; 9 ) ) ... ) </code></pre> <hr> <p>Looking at the search() method, it is returning <code>$this-&gt;SearchIndex-&gt;find('all', $findOptions);</code>. <strong>$findOptions</strong> contains the following:</p> <pre><code>Array ( [conditions] =&gt; Array ( [Article.is_published] =&gt; 1 [0] =&gt; MATCH(SearchIndex.data) AGAINST('search term' IN BOOLEAN MODE) ) [fields] =&gt; Array ( [0] =&gt; Article.id ) [contain] =&gt; Array ( [Article] =&gt; Array ( [0] =&gt; User.name ) ) ) </code></pre> <p>The association isn't getting lost along the way, because inside SearchableBehavior, <code>$this-&gt;SearchIndex-&gt;Article-&gt;belongsTo['User']</code> is present and intact immediately before and after the call to find() inside the search() method.</p> <p>The call to search() returns the exact same thing for all of the following <strong>values of 'contain'</strong>:</p> <pre><code>array('Article' =&gt; array('User.name')) array('Article' =&gt; array('User')) array('Article' =&gt; array('User' =&gt; array())) array('Article' =&gt; array('User' =&gt; array('fields' =&gt; array('User.name')))) array('Article' =&gt; array('User' =&gt; array('fields' =&gt; array('name')))) </code></pre> <p>Am I doing something wrong? I think I'm using the same format as is instructed in <a href="http://book.cakephp.org/view/474/Containable#Containing-deeper-associations-857" rel="nofollow">the CakePHP documentation</a>, and I haven't found anything online that suggests that you have to do something special to get search results with associated data.</p> <p>I know that I could easily achieve the result that I want by just looking up the Users with additional calls to find(), but I'd like to get containable behavior to work like it's supposed to and cut down on unnecessary extra database queries.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload