TimKan 2012-09-05 15:07:23 2703次浏览 1条回复 0 0 0

Hi,

我是Yii的初学者,在学习链接教材 http://yiibook.com/book/agile_web_application_development_with_yii1.1_and_php5/chapter-6 第六章(英文版121页)时遇见问题,请问下面的代码怎样使用,应该写在哪个文件哪个function里面?

.................... 有了这些,我们可以相当简单的访问所有的与项目(Project)关联的问题(Issue)和/或用户:

//create the Project model instance by primary key:
$model = Project::model()->findbyPk(1);
//get an array of all associated Issue AR instances
$allIssues = $model->issues;
//get an array of all associated User AR instance
$allUsers = $model->users;
//get the User AR instance representing the owner of
//the first issue associated with this project
$ownerOfFirstIssue = $model->issues[0]->owner;

通常我们需要写复杂的SQL join语句来访问这样的关系型数据。而使用了Yii中的关系型AR,将我们从这种复杂漫长痛苦的过程中解放出来。我们现在可以使用优雅精简的面向对象语法来访问这些关系了...................

  • 回复于 2012-09-05 23:53 举报

    首先你得懂得这些语句都是干么用的。我看了看都是从Model里面取得数据的。
    MVC就是Controller从Model里取得数据,然后把这些数据传给view来返回给浏览器用户。
    所以,这些语句就要写到Controller相应的Action里面。具体写到哪个方法里面,这个得看业务逻辑了。

您需要登录后才可以回复。登录 | 立即注册