MihaiMiY
MihaiMiY

Reputation: 23

Trying to get data from Model to View and i get these errors: Trying to get property of non-object and Undefined variable

I'm trying to create a basic MVC 'framework' but i have some problems with geting data from Models to Views.

class Controller
{

    public $_database = null;
    public $_model = null;

    public function __construct()
    {
        $this->connect();
    }

    private function connect()
    {
        $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);

        $this->_database = new PDO(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=' . DB_CHARSET, DB_USER, DB_PASS, $options);
    }

    public function model($model)
    {
        require_once '../app/models/'. $model .'.php';
        return new $model($this->_database);
    }

    public function view($view)
    {
        require '../app/views/'. $view .'.php';
    }
}


class Contact extends Controller
{
    public function index()
    {
        $name = 'David';
        $this->view('contact/index');
    }

First of all my view function (in Controller.php) didnt work properly:

By echo $name in 'contact/index.php' view i get this error: Undefined variable: name

If i just call require '../app/views/contact/index.php' in Contact controller, index method its works perfectly, but by calling the view method from Controller i get this error.

And the second problem is that i cant get data from Models properly

class User
{
    public $_database;

    public function __construct($database)
    {
        try 
        {
            $this->_database = $database;
        } 
        catch (PDOException $e) 
        {
            exit('Database connection could not be established.');
        }
    }

    public function select()
    {
        $stmt = "SELECT * FROM users WHERE ID = :id";
        $query = $this->_database->prepare($stmt);
        $query->execute(array(':id' => 1));
        return $query->fetchAll(PDO::FETCH_OBJ);
    }
}

By calling $name = $this->model('User')->select in Contact Controller and echo $name->Username in 'contact/index' view i get this error: Trying to get property of non-objec

I used PDO::FETCH_OBJ to return an object instead of an array but still dosn't work.

Upvotes: 0

Views: 419

Answers (2)

pbond
pbond

Reputation: 1927

Create a set function in your Controller for variables you want access to in your view, for example (taken from CakePHP):

    public function set($one, $two = null) {
        if (is_array($one)) {
            if (is_array($two)) {
                $data = array_combine($one, $two);
            } else {
                $data = $one;
            }
        } 
        else {
            $data = array($one => $two);
        }
        $this->viewVars = $data + $this->viewVars;
    }

Now you can use $this->set('variable', 'value') if you want $variable to be accessible in your view (with the value value).

Then right before you render your view, use extract($this->viewVars); and you'll be able to use $variable in your view.

Upvotes: 0

colburton
colburton

Reputation: 4715

Your select is returning an array of all users. You reduce the result to one with the query, but PDO does not know that.

Use $query->fetch(PDO::FETCH_OBJ) instead.


Your view is loaded (require) in the Controller::view() method. It only has access to the variables, this method itself can access. Therefore it cannot reach $name.

You could change your view method like this:

public function view($view, array $parameters = [])
{
     foreach ($parameters as $varname => $value) {
           $$varname = $value;
     }

     require '../app/views/'. $view .'.php';
}

Now you can use it like this:

public function index()
{
    $this->view('contact/index', ['name'=>'David']);
}

Upvotes: 0

Related Questions