Posted by: james | June 29, 2009

New 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);
}

OROROROROROROROROROROROROROROROROROROROROROROROROR

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”;
}

Before it you have to load that class so for that in IndexController.php
public function init()
{
Zend_Loader::loadclass(‘Blog’);
}

now when you want to execute any query in indexController.php at that time first create object of that class and make coding.
ex.

// coding structure for insert data

$some varilables = $this->_request->getPost();
then
$data_arr = array(

‘table coiumn name’ => $some varilables // use all the variables and table’s column name
)

$blog = new Blog();
$blog->insert($data_arr) // no need for tablename

//coding structure for select data

$reg = new Reg(); // create ur table object
$select = $reg->select()
->where(‘fname = ?’,$name) // now here use ur logic, that means join, where…..
->where(‘pass = ?’,$pass); // here i use two where with AND

$stmt = $select->query(); // now execute qyery…
$result = $stmt->fetchAll();// now fetch data in array// here you can use fetchOne,find,fetchOne,fetchRow etc… as ur need.
print_r($result); exit; // to see output..

//Threr are two ways to use select query.

1. $contcat = new Contact() // class name
$select = $contact->select()
->where(‘rid = ?’,$this->sess_id->id)
$this->view->result = $contact->fetchAll($select); // here u can also use only $result but to use in foreach loop at                                             .phtml page use this kind of concept
2. $contcat = new Contact()
$this->view->result = $contact->fetchAll();

//to assign any value to session steps are as under..

for ex. you want to store id in session then first of all in indexController.php

before evry function mean at the top write,

protected $sess_user;

now in public function init()
{
$this->sess_user = new Zend_Session_Namespace(‘sess_user’);
}

now go in action wherer you get value of id, in our case we get value in loginAction so we will write it in that action,

$this->sess_user->id = $results[‘id’];

now use this variable as session and remember dont write like this

$this->sess_user = $results[‘id’] // it will throw error

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.

Now when you make application with multiple tables at that time before you start decide which is primary key, foriegn key and all that. and then read all about foreign key, primary key from zend_db_table chapter and after making tables go ahead also read 15.8 zend_db_table relationship and in that there will one example and use of $_refrencemap and $_dependentTables.

Parent Table-> independet table
child Table->  dependet table

now to create their foreign key relation in zend use $_referenceMap

protected $_referenceMap = array(
‘Bug’ => array (
‘columns’ => column name of child or dependet table
‘refTableClass’ => Class Name of the parent table, not the table name
‘refColumns’ => Primary ke coiumn name of parent table
)
)

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 zend_view….

Note:
When you use same form in edit and add at that time don’t forget to use createRow function in Add action and see the project named             application

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: