Home > centOS, Fedora 12, PHP, Unix/Linux, Zend > Zend model

Zend model

How to zend model/database?

1. model or database > Zend_Db_Table

Practical: Steps:
[root@www myzf]# vi /var/www/html/myzf/application/configs/application.ini

[production]
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = 123
resources.db.params.dbname = myzf

phpSettings.display_startup_errors = 0

phpSettings.display_errors = 0

includePaths.library = APPLICATION_PATH “/../library”

bootstrap.path = APPLICATION_PATH “/Bootstrap.php”

bootstrap.class = “Bootstrap”

appnamespace = “Application”

resources.frontController.controllerDirectory = APPLICATION_PATH “/controllers”

resources.frontController.params.displayExceptions = 0

[staging : production]

[testing : production]

phpSettings.display_startup_errors = 1

phpSettings.display_errors = 1

[development : production]

phpSettings.display_startup_errors = 1

phpSettings.display_errors = 1

resources.frontController.params.displayExceptions = 1

[root@www conf]# mysql –user=root –password=123
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.90 Source distribution

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> show databases;
+——————–+
| Database           |
+——————–+
| information_schema |
| mysql              |
| test               |
+——————–+
13 rows in set (0.14 sec)
mysql> create database myzf;
Query OK, 1 row affected (0.00 sec)
mysql> use myzf
Database changed
mysql> create table myzf ( id int(10) not null auto_increment, name varchar(100),status varchar(100),primary key (id) );
Query OK, 0 rows affected (0.00 sec)

mysql> insert into myzf (name,status) values (‘ada’,’top’), (‘c’,’second’),(‘java’,’third’);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select *from myzf;
+—-+——+——–+
| id | name | status |
+—-+——+——–+
|  1 | ada  | top    |
|  2 | c    | second |
|  3 | java | third  |
+—-+——+——–+
3 rows in set (0.00 sec)

[root@www myzf]# zf create db-table Myzf myzf
Creating a DbTable at /var/www/html/myzf/application/models/DbTable/Myzf.php
Updating project profile ‘/var/www/html/myzf/.zfproject.xml’

class Application_Model_DbTable_Myzf extends Zend_Db_Table_Abstract
{

protected $_name = ‘myzf’;

// view
public function getMyzf($id){
$id = (int) $id;
$row = $this->fetchRow(‘id = ‘ . $id);

return $row->toArray();

}

// insert
public function addMyzf($name,$status){
$data = array(‘name’ =>$name, ‘status’=>$status);
$this->insert($data);
}

// edit
public function updateMyzf($id,$name,$status){

}

// del
public function deleteMyzf($id){
$this->delete(‘id=’ . (int)$id );
}

}

a. Database connection string > application/configs/application.ini > added those bold lines

b. make sure you have database myzf or create it > make table > insert  some data

c. create the model for myzf

d. how to use?

now from  controllers we can use it like:

$myzf = new Application_Model_DbTable_Myzf();
$this->view->myzf = $myzf->fetchAll();

Advertisements
Categories: centOS, Fedora 12, PHP, Unix/Linux, Zend
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: