duanhai1455 2013-03-23 21:49
浏览 35
已采纳

在php-activerecord中按关系查找

I'm trying on php-activerecord for a project – I've used it before in Lithium and CodeIgniter – but I am stuck at something that I think should be pretty easy. But I simply can not wrap my head around it.

So, I've got two tables:

post       |id  |title  |content
           ----------------------
           |1   |Test   |Lorem...

and:

post_meta  |id  |post_id  |publish_date
           ----------------------------
           | 1  |1        |2013-03-23

Now, if I would like to match the title in the post table, and the publish_date in the post_meta-table – how would I go about in the simplest and most elegant way?

As it stands now I seem to have to do two separate queries, first at the meta table to extract the posts with the correct publish_date – and then loop through that one until I find a matching title. Or I can use relationships ($has_one and $belongs_to) – but that way it feels like I am doing it the "other way around" when I want to extract my post-data ($postMetas->post->content).

Feel free to come up with ideas and solutions – or even alternatives to an ORM that makes this is simple and elegant as possible.

  • 写回答

1条回答 默认 最新

  • dpje52239 2013-03-24 14:20
    关注

    You can just use a joined structure in a finder. Check out this part of the manual.

    Basically, it says you can join using known connections, and use those as base for what you get returned. The books examples on that page are quite good, see:

    # fetch all books joining their corresponding author
     7 $book = Book::all(array('joins' => array('author')));
     8 # sql => SELECT `books`.* FROM `books`
     9 #      INNER JOIN `authors` ON(`books`.author_id = `authors`.id)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动