开发者

cakephp - view with data from multiple tables as one

开发者 https://www.devze.com 2023-02-18 23:52 出处:网络
I want to have a view for each table (letters, filing开发者_Go百科s, notes) which each have a name and date.I want to have a separate view for items from each table that also fall into a particular ca

I want to have a view for each table (letters, filing开发者_Go百科s, notes) which each have a name and date. I want to have a separate view for items from each table that also fall into a particular category -- say categoryx -- so it is a separate view. The categoryx view would combine particular fields from each of the multiple tables into one table, in effect, ordered by date.

E.g.,

Category x

DATE       NAME          ORIGTABLE
1/2/11     SmithToJones  Letters
1/3/11     Filing on X   Filings
1/4/11     Note re X     Notes
1/7/11     JonesToSmith  Letters

I'm just stumped on whether there is a good or straightforward way to do this. Open to options. Thanks


I think I have the answer, though it's not perfect. I do this in the controller (may try it in the model next):

$query='select name,date, "letters" as "origtable" from letters
UNION SELECT name,date, "filings" as "origtable" from filings 
UNION SELECT name,date, "notes" as "origtable" from notes
ORDER BY date';

$things=$this->Letter->query($query);
$this->set('items',$things);

This gets me an array I can parse out. If I add id to it, then I can easily create links to the relevant item in the relevant table.

0

精彩评论

暂无评论...
验证码 换一张
取 消