0

I have the following code in "IndexController.php":

$sql = 'SELECT * FROM ?';
$stmt = new Zend_Db_Statement_Mysqli($db, $sql);
$this->view->projects = $stmt->execute(array('projects'));

... which is just used to retrieve all project objects from the database and pass them to the view. However when I run this code I get the following error:

Notice: Undefined variable: db in C:\wamp\www\PROJECTS_Zend\projectManager\application\controllers\IndexController.php on line 19

Fatal error: Call to a member function quoteIdentifier() on a non-object in C:\wamp\www\PROJECTS_Zend\projectManager\library\Zend\library\Zend\Db\Statement.php on line 181

I am not sure what variable db is, or what it should be, but if you have any information on this I would be greatful if you could enlighten me.

3
  • Isn't that what the model is for (not an answer I know)? Commented Feb 16, 2012 at 15:08
  • Do you mean I should put this code into the model? Commented Feb 16, 2012 at 15:09
  • In zend we have Model, View, Controller. We basically use Model for database related things. And I don't recommend writing sql statements in the controller anyway. You can use some classes in that case. It will make your code perfect. Anyway to answer your question, could you please put the code you used to initialize the $db object? Commented Feb 16, 2012 at 15:18

2 Answers 2

2

Queries in MVC frameworks like Zend, Kohana and Codeigniter should be done in the "Model" class.

Not the controller class!

Model classes handle data from a data store like a database or a file.

You should do this in the Model class as it will have all the functionality extended from the Zend_db class

$db is your variable which holds all your details to connect to your database (hostname, username, pass, database name)

You have to define this earlier in your code

One more thing about MVC

  • You should have FAT MODELS that handle alot of data.
  • You should have Paranoid Controllers who manage all processes
  • You should have DUMB Views which cant do anything important!

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

1

Well this way you are ruining the whole M(odel) concept of the MVC.

Anyways $db is an instance of Zend_Db. I would advice you to read http://framework.zend.com/manual/en/zend.db.adapter.html.

Although this might lead to awful code:

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'     => '127.0.0.1',
    'username' => 'webuser',
    'password' => 'xxxxxxxx',
    'dbname'   => 'test'
));

3 Comments

I don't think you understand: it should be in a model, not your controller.
Ok So do I place this $db code into the top of my model class? Thanks!
You place this "inside" your model class :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.