Friday, October 1, 2010

Cake History Table Searching

Hi,

I've been searching for a simple solution to this for a few days, but
no joy. Any help appreciated.

I have two database tables. A job and a job state. The job state is an
historic table, where the most recently created row (with id) is
linked to the job. The association in cake looks like this:-

var $hasMany = array (
'JobStatus' => array (
'className' => 'JobStatus',
'foreignKey' => 'job_id',
'dependent' => false,
'order' => 'JobStatus.created desc',
'limit' => '1',
);

This works fine for retrieval, but searching is proving very
difficult. Using find with containable creates too many queries.

I am currently using:-

$this->query("SELECT * FROM jobs AS j, job_statuses AS js WHERE
js.created = (
SELECT MAX( created )
FROM job_statuses jss
WHERE jss.job_id = j.id )
AND j.id = js.job_id
AND js.stage_code_id = '91'")

But there must be a better way, for this relatively common database
pattern.

Any suggestions greatly appreciated!

Thanks

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: