Zend Framework: Create a Model and Database Table

Here is another article based on Zend Framework's documentation about creating model and database table. I have noticed that I am on worst documentation ever I read. Yes, Zend Framework's doc about quickstart tutorial is very badly written. If I remember it correctly then Symfony document was far better. At least this many serious error was not there.

(For comments/solutions related to previous pages on Zend documentation about Zend Framework, read on zend tag. )

Now, let me revel you all the trouble I have faced and solution I could find out. Few things are not clear yet, but that is about theory. I will mention this at last.

Error #1. when I have used this command: zf configure db-adapter …

An Error Has Occurred
Action 'configure' is not a valid action.

Solution is:

At the C:xamppphp (where php.exe resides), there are already files for Zend command line tool (zf.bat, zf.sh and zf.php) and those would have been created by XAMPP. Just overwrite old files with ZendFrameworkbin*.

Error #2:

Finally after completing every steps, I got an error when I try to see the output:
Fatal error: Class 'Application_Model_GuestbookMapper' not found in C:xampphtdocsZendFrameworkdemosquickstartapplicationcontrollersGuestbookController.php on line ..
I can see GuestbookMapper file there at proper place, what to do?

Solution is:

In Bootstrap.php file, write this method:


protected function _initAutoload()
{
    $loader = new Zend_Application_Module_Autoloader(array(
      'namespace' => 'Application',
      'basePath' => APPLICATION_PATH
      ));
    return $loader;
}

Error #3: I am getting this error: Fatal error: Class 'Application_Model_DbTable_Guestbook' not found in … quickstartapplicationmodelsGuestbookMapper.php on line …

Solution is:

Error is in Guestbook.php file: It has created class = Model_DbTable_Guestbook,
where the required class name is = Application_Model_DbTable_Guestbook. So, how to solve it. It is error in the command name mentioned there. Someone in comment has correctly pointed out that this looks Microsoft documentation where nothing works. I remember Visual Basic documentation (MSDN) was good enough. After that I have no idea. Smile

I have also found few more lines very doubtful/not clear on the Zend documentation.

Point 1: These commands are not correct at the documentation. Here it is correct:

zf configure db-adapter "adapter=PDO_SQLITE&dbname=APPLICATION_PATH '/../data/db/guestbook.db'" production
zf configure db-adapter "adapter=PDO_SQLITE&dbname=APPLICATION_PATH '/../data/db/guestbook-testing.db'" testing
zf configure db-adapter "adapter=PDO_SQLITE&dbname=APPLICATION_PATH '/../data/db/guestbook-dev.db'" development

Point 2:

It is not very clear, where to create data/db folder. I had created in quickstart/application folder. But latter moved to quickstart/data. Also where to store the script files (data.sqlite.sql, schema.sqlite.sql and load.sqlite.php) was not clear.
It should go inside quickstart/scripts/.
Run command from:
C:xampphtdocsZendFrameworkdemosquickstartscripts>php load.sqlite.php --withdata

Point 3: Why DBName is not going to development entry in application.ini page?

I cannot see these lines at my Development section:
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../data/db/guestbook-dev.db"

I have tried the above zf configure db-adapter …. command for development also. Tried it many times but no use.

Point 4: I could not make good sense in these few lines there. I may need to decode it. If you know please clear it. Thanks!
1. (Naturally, this is not needed in production, but this script will help developers build out the database requirements locally so they can have the fully working application. )
2. (All that is truly necessary when extending Zend_Db_Table is to provide a table name and optionally the primary key (if it is not "id"). )

These all has taken many hours and effort. I am posting here so others can find the help easily. Please spread the word.

I have not mentioned the commands line by line as reading the documentation is good. Theory is important!

Here is a snap of command line tool:

quickstart.create-model-commands

  • # 1 - by Yoav

    Great fixes for the zf configure db-adapter, you have saved me many hours. Thanks Satya!

Comments are open for an year period. Please, write here on Facebook page.