Now that we have created our helloworld database. lets have some fun with it
models
Model is the data layer of the zend framework. it is use to deal with database. all the data logic will go in the model. so as we have users table in our database, We will create a model with the similar name in the model directory of the project.
open your command prompt, go to your project directory and type
zf create db-table {name of db-table object } {name of actual table}
zf create db-table users users
its actually a dbTable object that we created, here we provide the name of the table and that the name of the file that we want to create as model for the table.
now our project has new file called Users.php in the model directory. which look like this;
The file has a class and in the class there is a protected variable name $_name = users;
that is actually the name of the table.
now lets first select some data from the table by calling the method fetchAll();
here is the code now in the Users.php file;
<?php
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
public function getUsers(){
return $this->fetchAll();
}
}
getUsers() function return values of all the users table. when we call this method from the controller and will get all the data of the users table from the database;
but there is one thing before that
we will first connect to our database, simply go to your project directory and type;
zf configure db-adapter "adapter=PDO_MYSQL&host=localhost&username=root&password=&dbname=helloworld"
now the application.ini file has been made some changes to. that is
models
Model is the data layer of the zend framework. it is use to deal with database. all the data logic will go in the model. so as we have users table in our database, We will create a model with the similar name in the model directory of the project.
open your command prompt, go to your project directory and type
zf create db-table {name of db-table object } {name of actual table}
zf create db-table users users
its actually a dbTable object that we created, here we provide the name of the table and that the name of the file that we want to create as model for the table.
now our project has new file called Users.php in the model directory. which look like this;
The file has a class and in the class there is a protected variable name $_name = users;
that is actually the name of the table.
now lets first select some data from the table by calling the method fetchAll();
here is the code now in the Users.php file;
<?php
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
public function getUsers(){
return $this->fetchAll();
}
}
getUsers() function return values of all the users table. when we call this method from the controller and will get all the data of the users table from the database;
but there is one thing before that
we will first connect to our database, simply go to your project directory and type;
zf configure db-adapter "adapter=PDO_MYSQL&host=localhost&username=root&password=&dbname=helloworld"
now the application.ini file has been made some changes to. that is
now go to the indexController.php and make some changes to the userAction() to get the table data
public function userAction()
{
$user = new Application_Model_DbTable_Users();
$results = $user->getUsers();
$this->view->results = $results;
}
here we create an object of the DbTable_users object and then call its getUser() method that will fetch all the data contained in the table. than we send the results the data to the view called user.phtml
now lets print the data in the user.phtml
here is the code;
<?php foreach ($this->results as $item):?>
<h1><?php echo $item['firstname'];?></h1>
<h3><?php echo $item['lastname'];?></h3>
<span><?php echo $item['email'];?></span><br/><br/>
<?php endforeach;?>
now to to localhost/helloworld/public/index/user and all the results from the database is shown as ;
next we will see how to insert,udate and delete from the database
No comments:
Post a Comment