MVC基本面:沿该视图传递工作的? [英] MVC Fundamentals: Passing work along to the view?

查看:109
本文介绍了MVC基本面:沿该视图传递工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Doctrine2和codeIgniter2,并且是新来的两个,以及对OOP / MVC,所以请使用简单的解释:)

I'm using Doctrine2 and CodeIgniter2, and am new to both, as well as to OOP/MVC, so please use simple explanations :)

有关测试的目的,我有一个控制器模型和视图。我想从一个包含用户信息的表显示数据。姓氏,姓名,身份证号码等等。

For testing purposes, I have a Controller a Model and a View. I want to display data from a table that contains user information. First name, last name, ID number, and so forth.

我的控制器,使得该模型 - 一个电话从学说实体获取数据,然后控制器传递数据到视图。

My controller makes a call to the model- which retrieves data from the doctrine entity, and then the controller passes that data to the view.

(控制器)

class Test_v_to_m extends CI_Controller {

    public function index() {

        $this->load->model('testing/test_v_to_m_model');
        $data = $this->test_v_to_m_model->display_user_info();

        $this->load->view('testing/test_v_to_m_view', $data );
    }
}

(模型)

class Test_v_to_m_model extends CI_Model{

public function display_user_name() {

    $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Users u");
    return $query->getResult();

(视图)

//print_r($data);

第一个问题是:我如何传递对象或数组一起视图中一个有用的方法?这工作,如果我只是在处理一个单一变量:

First question is: How do I pass the object or array along to the view in a useful way? This works if I'm just dealing with a single variable:

(控制器)

$user = $this->doctrine->em->find('Entities\User', $user_id);
$data['firstname'] = $user->getFirstName();
$this->load->view('testing/test_v_to_c_view_2',$data);

(视图)

echo $firstname;

但我不知道该怎么做类似的事情时,其数组或多维数组。

But I don't know how to do something similar when its an array, or a multidimensional array.

第二个问题是,是否让视图做任何实际工作(PHP的逻辑,循环的foreach等),还是做这一切的控制器,而且只做格式和显示视图。

The second question is whether or not to let the view do any real work (php logic, loops, foreach, etc) or to do all of that in the controller and have the view only do formatting and display.

推荐答案

是的,你可以只通过多维数组的视图,然后根据需要访问它。
例如。

Yes, You can just pass multi-dimensional array to the view and then access it as required. e.g.

$template_date['result_arr'] = array(
array('firstname' => 'abc', 'lastname' => 'xyz')
, array('firstname' => 'abc', 'lastname' => 'xyz')
);

在您的视图文件 -

in your view file -

foreach($result_arr as $key => $row) {
echo $row['firstname'].' <br />';
}

重新您的第二个问题 - 按我的理解 - 它的优良使用一些的foreach,在视图中循环,但它是最好的,如果业务逻辑保持在控制器和模型。希望这是有道理的给你。

Re your 2nd question - As per my understanding - it's fine to use some foreach, for loops in the view but it's best if business logic is kept to controllers and models. Hope it makes sense to you.

这篇关于MVC基本面:沿该视图传递工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆