Jump to content

Archived

This topic is now archived and is closed to further replies.

completeamateur

Confused... Zend_db

Recommended Posts

Hi there,

 

I have run through the "Getting started with zend..." tutorial which went fairly smoothly.  I'm now trying to run a simple query but Zend keeps throwing the error:

 

Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)' in /Library/WebServer/Documents/library/Zend/Db/Statement/Pdo.php:238 Stack trace: #0 /Library/WebServer/Documents/library/Zend/Db/Statement.php(283): Zend_Db_Statement_Pdo->_execute(Array) #1 /Library/WebServer/Documents/library/Zend/Db/Adapter/Abstract.php(430): Zend_Db_Statement->execute(Array) #2 /Library/WebServer/Documents/library/Zend/Db/Adapter/Pdo/Abstract.php(220): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Table_Select), Array) #3 /Library/WebServer/Documents/library/Zend/Db/Table/Abstract.php(1189): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Table_Select)) #4 /Library/WebServer/Documents/library/Zend/Db/Table/Abstract.php(1044): Zend_Db_Table_Abstract->_fetch(Object(Zend_Db_Table_Select)) #5 /Library/WebServer/Documents/villa4life/application/controllers/BodymoorheathController.php(10): Zend_Db_Table_Abstract->fetchAll('SELECT * FROM c...') #6 in /Library/WebServer/Documents/library/Zend/Db/Statement/Pdo.php on line 238

 

I believe this is the problem code:

 

$this->view->comments = $comments->fetchAll('SELECT * FROM comments ORDER BY dateTime');

 

Any suggestions.  I'm very confused by the fundamentals of executing queries in Zend.

Share this post


Link to post
Share on other sites

I'm not sure what's the error about, so let's try a different approach :)

 

Why don't you use the model for working with the database? That's the point of the MVC.

 

I'll try to write a basic model for your comments table, based on the info provided. This model should go in: /Library/WebServer/Documents/villa4life/application/models/Comments.php

 

<?php

class Comments extends Zend_Db_Table
{
protected $_name = 'comments'; // The table name
protected $_primary = 'id_comment'; // The name of the primary column
protected $_schema = 'database_name'; // Name of the database

public function getAll()
{
	$sql = $this->select()->from('comments');	
	$rowsetComments = $this->fetchAll($sql);
	return $rowsetComments;
}

}

?>

 

Now, the 'id_comment', 'comment', 'author' and the 'date' columns here are fictitious, change them to suit your scenario. To use it in the controller, use it like this:

// some where in some action function...
$comments = new Comments();
$this->view->comment = $comments->getAll();

And that should do it...

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.