Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting the column 'country_id' in 'where' clause is ambiguous - error in search query in cakephp 1.3
    primarykey
    data
    text
    <p>I am working with CakePHP 1.3 version for search functionality using Search Plugin.</p> <p>I have three models:</p> <ul> <li>Demo,</li> <li>Country</li> <li>State</li> </ul> <p><code>Demo</code> has two foreign keys, <code>country_id</code> and <code>state_id</code>. <code>State</code> has the foreign key <code>country_id</code>.</p> <p>What I am doing is, I have search form which have country &amp; state drop down which fetch all data from countries &amp; states table. When i search any of country from dropdown &amp; submit the form it will show me below error. If i search using only state dropdown i get the correct result.</p> <p>When I execute the search query, I get the error</p> <blockquote> <p>'Column '<code>country_id</code>' in where clause is ambiguous'</p> </blockquote> <p>My query is:</p> <pre><code>SELECT `Demo`.`id`, `Demo`.`demo2`, `Demo`.`desc`, `Demo`.`subject`, `Demo`.`gender`, `Demo`.`country_id`, `Demo`.`state_id`, `Demo`.`image_url`, `Country`.`id`, `Country`.`name`, `State`.`id`, `State`.`country_id`, `State`.`description` FROM `demos` AS `Demo` LEFT JOIN `countries` AS `Country` ON (`Demo`.`country_id` = `Country`.`id`) LEFT JOIN `states` AS `State` ON (`Demo`.`state_id` = `State`.`id`) WHERE `country_id` = 2 </code></pre> <p>Model relationships in Demo table:</p> <pre><code>var $belongsTo = array( 'Country' =&gt; array( 'className' =&gt; 'Country', 'foreignKey' =&gt; 'country_id', 'conditions' =&gt; '', 'fields' =&gt; '', 'order' =&gt; '' ), 'State' =&gt; array( 'className' =&gt; 'State', 'foreignKey' =&gt; 'state_id', 'conditions' =&gt; '', 'fields' =&gt; '', 'order' =&gt; '' ), ); </code></pre> <p>The controller query to fetch all Country in dropdown is:</p> <blockquote> <p>$country=$this->Country->find('list'); //just display the list of country in dropdown</p> </blockquote> <p>The query search the data from all fields except <code>Country</code> (<code>country_id</code>), because it will not know which <code>country_id</code> it is looking for from table <code>Demo</code> or table <code>State</code>. I need the <code>country_id</code> from the demo table to get the correct result.</p>
    singulars
    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