Zend framework 1 article

Home >> Zend framework 1 >>

  Zend framework 1 CRUD operations part - 1

This article will explain you about the CRUD operations of Zend framework 1 in details include topics such as Create a layout, XHTML DocType declaration and Database configuration.

Let's take an example of "Blog", where user will create, update and delete the topics of blog and display the all created blog at one place. Which will be the complete exercise of our CRUD operation.

Note:- I will use here Zend console commands and all commands need to be executed using cmd prompt.

Step 1:- Create a layout and XHTML DocType declaration
Layout
We need layout to keep common header, footer and other stuff in our application and using laout no need to call layout file with each and every action view. 

In Zend Framework, Zend_Layout combines the ideas behind these patterns. Instead of each action view script needing to include header, footer and other stuff in our application, they can simply focus on their own responsibilities.

To get started using Zend_Layout, first we need to inform our bootstrap to use the Layout resource. This can be done using the zf enable layout command:

C:\wamp\www\zf1app>zf enable layout
A layout entry has been added to the application config file.
A default layout has been created at C:\wamp\www\zf1app/application/layouts/scripts/layout.phtml
Updating project profile 'C:\wamp\www\zf1app/.zfproject.xml'

As noted by the command, application/configs/application.ini is updated, and now contains the following within the production section:

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

This directive tells your application to look for layout view scripts in application/layouts/scripts. You can navigate to application/layouts/scripts and see the newly created files.

Now let's open layout file application/layouts/scripts/layout.phtml, which will look like below.

echo $this->layout()->content;

XHTML DocType declaration
We also want to ensure we have an XHTML DocType declaration for our application. To enable this, we need to add a resource to our bootstrap.

The simplest way to add a bootstrap resource is to simply create a protected method beginning with the phrase _init. In this case, we want to initialize the doctype, so we'll create an _initDoctype() method within our bootstrap class application/Bootstrap.php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Init the doctype.
     */
    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }
}

We need to initialize the view resource, To initialize the view resource, add the following line to your application/configs/application.ini file, in the section marked production:

resources.view[] =

Step 2:- Create a Model and Database Table

Database configuration
Let's create one database named as "zf1app_db".

// Database SQL
CREATE DATABASE zf1app_db;

We need to initialize our DB resource. We can do this with the zf configure db-adapter command, db-adapter command complete syntax is like below syntax.

zf configure db-adapter dsn section-name[=production]
// Where dsn will consist of DB Adapter, HOST, Username, Password, Database name and charset.
// In last we need to mention section name for which section we are going to configure the db-adapter

Now let's create db-adapter for production

C:\wamp\www\zf1app>zf configure db-adapter "adapter=PDO_MySQL&host=localhost&username=root&password=&dbname=zf1app_db" production
A db configuration for the production section has been written to the application config file.

As noted by the command, application/configs/application.ini is updated, and now contains the following within the production section and manually you can also do this.

resources.db.adapter = "PDO_MySQL"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "zf1app_db"

If you want db-adapter for stagingtesting and development then you can chnage the last parameter like below example.

C:\wamp\www\zf1app>zf configure db-adapter "adapter=PDO_MySQL&host=localhost&username=root&password=&dbname=zf1app_db" development
A db configuration for the development section has been written to the application config file.

As noted by the command, application/configs/application.ini is updated, and now contains the following within the development section and manually you can also do this.

resources.db.adapter = "PDO_MySQL"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "zf1app_db"

Note:- In this example I am considering on production section, development section db-adapter is just to show case you how you can create db-adapter for different - different sections.

Database configuration is completed, now let's move to models, But before moving to models let's see the complete configuration file application/configs/application.ini of production section, which will look like below.

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =

resources.db.adapter = "PDO_MySQL"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "zf1app_db"

Write your comment now