doupa8922 2010-01-29 01:14 采纳率: 100%
浏览 41
已采纳

刚开始使用Kohana ......有几个问题

I just started using Kohana a couple days ago, and I have a few questions that I cant seem to find an answer to anywhere.

Using ORM, how can you package information before you send it off? If I have a user model, and each user has a username, email, date of birth, etc... how can I package information (like add the users age as a property) before I send it to the controller?

Using ORM again, I see that if you select a user, you can also select all things related to that user (ie: articles, comments, etc.). How can you order that related information before sending it to the controller?

ie:

<?php
$user = ORM::factory('user', $id);
$user->articles; // holds all articles belonging to a user.

// how can you order those articles dynamically?

In the view, how can you get information about a user who is logged in? Do you use Auth (Auth::instance()->get_user()), or is there any other way?

Also, If you know any other tips/advice/qwerks about Kohana, please drop a line or two, you may answer a future question of mine.

Thanks.

Edit: Another question. Using the ORM, I want to be able to load all articles that posted, in which a user has made a comment.

comments table

comment_id
user_id
article_id
etc....

Using ORM, I can access all articles posted by a user, but how would I be able to access all articles in which the user has commented on?

Thanks

  • 写回答

1条回答 默认 最新

  • dongmi0760 2010-01-29 05:25
    关注

    If you are using Kohana v3, you can order records like so:

    $articles = ORM::factory('user', $id)->articles
                                         ->order_by('date', 'DESC')
                                         ->find_all();
    

    If you are using Kohana v2, the same thing can be achieved using:

    $articles = ORM::factory('user', $id)->orderby('date', 'DESC')
                                         ->articles;
    

    And you were correct about accessing the user record through Auth::instance()->get_user() (if you are using the ORM Auth driver)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加