Posted by: james | June 11, 2009

zend db

simple steps to create zend normal database appliaction:

first of all create new zend framework project.

if you are using zend studio then there will you get file structure as well as requrire files with coding but if you dont use it then pls see that pdf of getting started with zend framewrok.

Now make some sense with your application. and decide how many pages you want to display and create action for all of that and also .phtml file for every page.

here i take example of blog registration page.

in that i need 4 page

1. view

2. add

3.edit

4.delete

Now create in application->view->script-> view.phtml, add.phtml, edit.phtml, delete.phtml.

and now in IndexControoler.php create action for all that .phtml files public function indexAction() { //for view }

public function addAction() { // for add }

public function editAction() { //for edit }

public function deleteAction() { //for delete }

Now check that all the urls are working right or not with some echo statements. and if you get error like page not found then pls check ur .htaccess file and put it at your root folder, i mean to say ur project name is zend_blog so your folder name will also zend_blog so put .htaccess file there.

Now create database called zend_blog. in that create table name with blog_data.

Now to get access of that table we have to follow some steps as under.

1. create config.ini in application folder write in that

[general] db.adapter = PDO_MYSQL

db.config.host = localhost

db.config.username = root

db.config.password = *****

db.config.dbname = zend_blog

2. now in index.php or bootstrap.php write.

Zend_Loader::loadClass(‘Zend_Config_Ini’);

Zend_Loader::loadClass(‘Zend_Registry’);

Zend_Loader::loadClass(‘Zend_Db’);

Zend_Loader::loadClass(‘Zend_Db_Table’);

$config = new Zend_Config_Ini(‘./application/config.ini’,’general’);

$registry = Zend_Registry::getInstance();

$registry->set(‘config’,$config);

$db = Zend_Db::factory($config->db); Zend_Db_Table::setDefaultAdapter($db);

Zend_Registry::set(‘temp’,$db);

Now in IndexController.php define one variable

protected $data;

and write in function

public function init()

{

$this->data = Zend_registry::get(‘temp’);

// now when you want to use table name or want to execute any query use data.

for ex. insert query

$this->data->insert(‘tablename’,array with data);

$this->view->result = $this->data->fetchAll();

$this->data->delete($where);

$this->data->update($data_arr,$where);

}

OROROROROROROROROROROROROROROROROROROROROR

Always use thisAlways use thisAlways use thisAlways use this

You can create database connectivity by using this way.

in index.php file write,

$config = new Zend_Config_Ini(‘./application/config.ini’,’general’);

$registry = Zend_Registry::getInstance();

$registry->set(‘config’,$config);

$db = Zend_Db::factory($config->db->adapter,$config->db->config->toArray());

Zend_Db_Table::setDefaultAdapter($db);

Now in model folder create one new php file and write in that

class Blog extendes zend_db_table

{

protected $_data = “your tablename”;

}

now when you want to execute any query in indexController.php

at that time first create object of that class and make coding

. ex.

$blog = new Blog();

$blog->insert($data_arr)

// no need for tablename if you have more than one table then create seprate class for every table, that means if you have 10 tables then there will be 10 classes now after that do appropriate coding in particular html page,

i mean to use zend_form or zend_view to make html layout.

after making html layout do coding in appropriate function in indexController file.

Now to make more clear concepts make application with 3 to 4 tables and with zend_form and zend_layout.

To get any value in zend use,

$req = $this->getRequest();

$id = $req->getParam(‘id’);

To post any value in zend use

$name = $this->_request->getPost(‘name’);

_request and getRequest OR _helper and getHelper are same i mean to say _ or get are same. Now make this application using zend_layout and

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: