Onesinus Saut
Onesinus Saut

Reputation: 344

cakephp access field search in model that have relation model

Excuse me everybody that have a great skill in cakephp, i'am beginer in cakephp, i don't know how to access field which i want to use the value.

This is the array data show from controller

Array
(
`[Claim] => Array`
        (
            [id] => 121
            [name] => Gwoo the Kungwoo
            [created] => 2007-05-01 10:31:01
        )
    [ClaimDetail] => Array
        (
            [0] => Array
                (
                    [id] => 123
                    [claim_id] => 121
                    [title] => On Gwoo the Kungwoo
                    [body] => The Kungwooness is not so Gwooish
                    [date] => 2006-05-01 10:31:01
                )
            [1] => Array
                (
                    [id] => 124
                    [claim_id] => 121
                    [title] => More on Gwoo
                    [body] => But what of the 'Nut?'
                    [date] => 2006-05-01 10:41:01
                )
        )
);

in the controller i make the conditions,

$conditions = array(
                    'ClaimDetail.date between ? AND ?'  => array(
                        $this->request->query['start_date'],
                        $this->request->query['end_date']
                    ),
                    'Claim.delete_flag' => 0
                );

but cakephp show the error, unknown field,

this is the find,

$claim = $this->Claim->find('all', array(
                'conditions'    => $conditions
            ));



Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ClaimDetail.date' in 'where clause'


there're any other ways for me to make conditions like what i want,

thankss before and after... T_T stuck  one week 

Upvotes: 0

Views: 174

Answers (1)

Indrasis Datta
Indrasis Datta

Reputation: 8606

The problem is you're applying the condition on an associated model and not the original model. One way to solve this is to join the two tables.

   $joins   =   array(
                 array(
                    'table' => 'claim_details',
                    'alias' => 'ClaimDetail',
                    'type' => 'INNER',
                    'conditions' => array(
                         'Claim.id = ClaimDetail.claim_id',
                         'ClaimDetail.date between "'.$this->request->query['start_date'].'" AND "'.$this->request->query['end_date'].'" '
                     )
                 )
            );

   $claim = $this->Claim->find('all', array(
           'joins'       => $joins
        ));

This should give you what you're looking for.

Peace! xD

Upvotes: 1

Related Questions