phpmodel-view-controllerdb2zend-server

Initialize object in controller MVC


I'm exprimenting with MVC from the ground up following this tutorial and getting 500 errors when trying to initialize a new object from a controller.

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'); ?>

When commenting out the following in the controller file, the error goes away

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

Clearly, I'm misunderstanding something about how to initialize an object from within the controller. Anybody got any suggestions on how to fix this?


Solution

  • In file employeelist.class.php

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

    $this->connection is null, reference to undeclared variable, updated to

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