1

I have created following function in controller:

    function beg($status_id = null) {
    if(!empty($status_id)){
        $conditions = array('winner_id >' => 0, 'Product.beginner' => '1', 'Product.status_id' => $status_id);
    }else{
        $conditions = array('winner_id >' => 0, 'Product.beginner' => '1');
    }
    $this->paginate = array('conditions' => $conditions, 'limit' => $this->appConfigurations['adminPageLimit'], 'order' => array('end_time' => 'asc'), 'contain' => array('Product' => array('Category'), 'Status', 'Winner'));

    $this->set('products', $this->paginate('Product'));
    $this->set('statuses', $this->Product->Status->find('list'));
    $this->set('selected', $status_id);
    $this->set('extraCrumb', array('title' => __('Product List', true), 'url' => 'beg'));

    $this->render('beg');
}

I want to add one more condition in it, that is:

Search winner_id from Accounts table match with 'user_id' in accounts table, and only add Products in the list where winner Id is in Accounts table ('user_id' in accounts table).

Anybody know how to add condition with this $conditions = array('winner_id >' => 0, 'Product.beginner' => '1', 'Product.status_id' => $status_id);

I have added on more condition like below

 $acc_id = $this->Account->find('all', array('Account.user_id'));
$conditions += array('Product.winner_id' => $acc_id);

But its not working, anybody please tell me where I am getting wrong

Akaash
  • 171
  • 1
  • 2
  • 12

2 Answers2

3

if you wanted to add one more condition to the $conditions than you can try using code

$conditions += array('Product.status_id' => $status_id);

hope it will help

MaNKuR
  • 2,578
  • 1
  • 19
  • 31
  • I have account table and want winner_id from Product Table to be in Account table user_id. But when I do $conditions += array('Product.winner_id' => 'Account.user_id'); its not working. Issue with this part is 'Account.user_id', how will I populate it and then use iteration to check if id found then show it – Akaash Apr 08 '13 at 14:56
  • 2
    if i am correct, then a join will be required to accomplish the task. Then in this case above `$condtion += ..` wont required. This functionality can be achieved by `join` clause with _account_ table with `on` clause. More info [Click Here](http://stackoverflow.com/questions/806650/how-do-i-write-a-join-query-across-multiple-tables-in-cakephp). Hope it would make sense to you – MaNKuR Apr 09 '13 at 06:23
  • Is this correct, or how will I add these values in condition? $acc_id = 1,2,3,4,5; $conditions += array('Product.winner_id' => $acc_id); – Akaash Apr 09 '13 at 09:50
  • 2
    `$acc_id` need to be an array to equip with `array('Product.winner_id' => $acc_id);` in this case the condition will be something like `where Product.winner_id IN (1,2,3,4,5)` Check this and let me know – MaNKuR Apr 09 '13 at 14:12
  • I did with either a lengthy way, but it worked atleast..Run a query take values and then did this 'Product.winner_id' => array_map('intval', explode(',', $val))..which worked finally..:) – Akaash Apr 09 '13 at 15:54
  • 2
    This can also be achieved by Cakephp Association relation [more info](http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html). You can check with `$conditions = array('Product.winner_id IN('.$acc_id.')` without calling ` explode` method. – MaNKuR Apr 10 '13 at 10:05
2

yes you can add it with code like below

if ($account_user){
      $conditions['accounts.user_id'] = $account_userid;
}

above code will add one more conditions in same array $conditions.

liyakat
  • 11,825
  • 2
  • 40
  • 46
  • I have to add this condition below or above the code $conditions = array('winner_id >' => 0, 'Product.beginner' => '1', 'Product.status_id' => $status_id); – Akaash Apr 08 '13 at 11:46
  • 1
    yes above of $this->paginate = array('conditions' => $conditions, 'limit' => $this->appConfigurations['adminPageLimit'], 'order' => array('end_time' => 'asc'), 'contain' => array('Product' => array('Category'), 'Status', 'Winner')); – liyakat Apr 08 '13 at 12:09