A simple example of using Zend Framework Zend_Db_Table

To be a professional PHP developer, framework probably is a final solution. Since ZF is Zend’s product, I think it would not be a bad idea if I learn it even I’m not sure which framework is better. Anyway, the best learning approach is to start studying right now. Here is my first practice example.

error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
//date_default_timezone_set('Europe/London');
$rootDir = dirname(dirname(__FILE__));
//echo dirname(__FILE__); C:\wamp\www\zf-tutorial\testdb
//echo $rootDir;          C:\wamp\www\zf-tutorial
set_include_path( $rootDir . '/library'. PATH_SEPARATOR . get_include_path());
 
require_once('../library/Zend/Loader.php');
Zend_Loader::loadClass('Zend_Db');
//Zend_Loader::loadClass('Zend_Db_Table_Abstract');
Zend_Loader::loadClass('Zend_Db_Table');
//Zend_Controller_Front::run('/path/to/controllers');
//Zend::loadClass('Zend_Db');
 
$params = array (
	'host' => 'localhost',
	'username' => 'root',
	'password' => 'admin',
	'dbname' => 'zf-tutorial'
);
$db = Zend_Db::factory('PDO_MYSQL', $params);
 
Zend_Db_Table::setDefaultAdapter($db);
 
class Albums extends Zend_Db_Table_Abstract
{
	protected $_name = 'albums';
}
 
$albums = new Albums();
$data = array(
        'artist' => 'Ben',
        'title' => 'Ramsey'
);
$id = $albums->insert($data);

Of course, you should have a database table setup on your local machine first.

Leave a Comment