Saturday, 15 August 2009

Zend database examples

Zend_Db_Table is a way for zend framework to interact with the database.

As a beginner it is a challenge to find examples how to interact with the zend table. I have collected a number of examples.

I am using here the MVC model.

I have placed a cless in /Models/DbTable/Puzzel.php. Here i "tell" the framework that i will be using the table 'puzelle'. See below class.

class Model_DbTable_Puzelle extends Zend_Db_Table_Abstract
{
protected $_name = 'puzelle';

}


In a controller file i place the SQL queries in the functions.

// here i tell zend that i will use the puzzel table.
$puzzelTBL = new Model_DbTable_Puzelle();


Select

"Select * FROM apple Where data = $to and answer = 0"

IN ZEND

$to = date("Y-m-d");
$result = $this -> puzzelTBL -> fetchRow(' date = "'.$to.'" AND answer = 0');
$row = $result ->toArray();


Insert

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

in ZEND

$data = array(
'column1' => $value1,
'column2' => $value2,
'column3' => $value3
);

$puzzelTBL -> insert( $data);

0 comments:

Post a Comment