初始化控制器MVC中的对象

时间:2015-04-24 13:09:46

标签: php model-view-controller db2 zend-server

我在本教程之后从头开始尝试使用MVC,并在尝试从控制器初始化新对象时遇到500错误。

http://www.phpro.org/tutorials/Model-View-Controller-MVC.html

employeelist.class.php

<?php

class employeelist
{
    private $employees = array();

    public function getEmployees() {
        sql='SELECT F0101.ABAN8, F0101.ABALPH FROM F0101 INNER JOIN FP0102 ON F0101.ABAN8=FP0102.VEAN8 WHERE FP0102.VESTAT=''';
        $stmt= db2_prepare($this->connection,$sql);
        $result= db2_execute($stmt);
        while ($row = db2_fetch_assoc($stmt)) {
            $employees = array($row['ABAN8'],$row['ABALPH'])
        }
        return array($employees);
    }

}
?>

employeelistController.php     

class employeelistController Extends baseController {

public function index() {
        $registry->employeelist = new employeelist;
        $this->registry->template->employeelist = $registry->employeelist->getEmployees();
        $this->registry->template->show('employeelist_index');
    }
}

?>

employeelist_index.php

<?php include('includes/header.php'); ?>
    <div class="row">
        <?php include('includes/accounting-menu.php'); ?>
        <div class="col-md-9">
            <div class="row">
                <ul class="nav nav-pills">
                  <li role="presentation" class="active"><a href="/commissions/index">Επισκόπηση προμήθειων</a></li>
                  <li role="presentation"><a href="/commissions/statement">Αναλυτική κατάσταση προμηθειών</a></li>
                </ul>
            <br/>
            </div>
        </div>
    </div>
<?php include('includes/footer.php'); ?>

在控制器文件中注释掉以下内容时,错误消失

 $this->registry->template->employeelist = $registry->employeelist->getEmployees();
 $this->registry->template->show('employeelist_index');

显然,我误解了如何从控制器内初始化对象。有人对如何解决这个问题有任何建议吗?

1 个答案:

答案 0 :(得分:0)

在档案 employeelist.class.php

$stmt= db2_prepare($this->connection,$sql);

$ this-&gt; connection为null,引用未声明的变量,更新为

$stmt= db2_prepare($this->registry->user->connection,$sql);