简体   繁体   English

MVC和Mysql概念难题-想要从VIEW检索mysql数据以获取从CONTROLLER返回的项目列表

[英]MVC and Mysql conceptual dilemma - Want to retrieve mysql data from the VIEW for a list of items that is returned from the CONTROLLER

This may be chalked up to exhaustion, but I am new to MVC programming and while I have some grasp on it, I am really struggling with a concept. 这可能被用尽了,但是我是MVC编程的新手,尽管我对此有所了解,但我确实在为一个概念而苦苦挣扎。 I am working in php/codeigniter on a system that allows a user to post entries and other users are able to vote on them. 我正在使用php / codeigniter,该系统允许用户发布条目,其他用户可以对它们进行投票。 I have stored the entries in a mysql table entries and the votes in another table votes . 我已经存储在另一个表 MySQL表条目和选票的条目。

The field that links them together would be entries->id and votes->entryId . 将它们链接在一起的字段将是entry-> id投票-> entryId

So, in my controller , I have a function that calls the model and gets the recent entries: 因此,在我的控制器中 ,我有一个调用模型并获取最新条目的函数:

$data['recentEntries'] = $this->Entries->getRecentEntries($id);

where $id is the id number of the user's entries you want to get. 其中$ id是您要获取的用户条目的ID号。

I want to be able to display each entry alongside the corresponding votes for each entry, and normally without MVC I would simply find all the entries for a user, then in a foreach loop, find and display all the votes for each particular entryId . 我希望能够显示每个条目以及每个条目的对应票数,并且通常不使用MVC,我只会为用户找到所有条目,然后在foreach循环中,找到并显示每个特定entryId的所有票数。

However, with MVC, the entries are loaded into the variable $data['recentEntries'] and I wouldn't or shouldn't use the foreach loop to query mysql in the view . 但是,使用MVC,将条目加载到变量$ data ['recentEntries']中,并且我不会也不应使用foreach循环在view中查询mysql。

I feel like I am missing a critical mysql or MVC concept here that should take care of this quite simply, so any direction or guiding would be incredibly appreciated. 我觉得我在这里缺少一个关键的mysql或MVC概念,应该很简单地解决这个问题,因此任何方向或指导都将得到极大的赞赏。

Many thanks in advance. 提前谢谢了。

-- Update -- -更新-

So I've updated the controller so: 所以我已经更新了控制器,以便:

$data['recentEntries'] = $this->Entries->getRecentEntries($id);

$data['votes'] = array();

foreach ($data['recentEntries'] as $entry) {
    array_push($data['votes'], $this->Votes->getVotes($entry->id));
}

So that $data['votes'] now contains the votes for all the entries for a particular user. 这样$ data ['votes']现在包含特定用户的所有条目的投票。 Is this the right way to go about it? 这是正确的方法吗? If so, then once in the view's foreach loop, how do I find and display the appropriate values from this $data['votes'] array? 如果是这样,那么一旦进入视图的 foreach循环,如何从此$ data ['votes']数组中查找并显示适当的值?

View: 视图:

if (count($recentEntries)>0) {
    foreach ($recentEntries as $row)
    {
        echo $row->content;
    }
}

You probably to maintain it as a map rather than a simple array so that you maintain the association between an entry and it's votes. 您可能将其维护为地图而不是简单数组,以便维护条目与其票数之间的关联。 And, yes, I think it's best to populate the entire model in the controller rather than querying the DB from the view. 而且,是的,我认为最好在控制器中填充整个模型,而不是从视图中查询数据库。

foreach ($data['recentEntries'] as $entry) {
     $data['votes'][$entry->id] = $this->Votes->getVotes($entry->id);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM