Before diving into the code, review the tables you’ll work with throughout this chapter. If you aren’t familiar with them, don’t worry; you’ll take another quick look at the entity relationship diagram (ERD). If you don’t need a refresher, skip ahead to the “Installing PDO” section later in this chapter. The application contains three tables, as shown in below. The accounts table contains all the accounts in the system. User details are stored here: e-mail, password, username, and so on.
The accounts_artists join table allows users to identify themselves as fans of one or more artists stored in the artists table. Using the ID of the user stored in the accounts table along with the ID of the artist stored in the artists table, you can associate an account with an artist and store the date when the user became a fan of the artist.
The artists table contains a list of artists in the system. Users enter each of the artists in this table, which cannot have any duplicates, and an artist can belong to only one genre. The table contains the artist name, genre, date the record was created, and unique ID of the record.
You now have an overall understanding of the database that powers the next couple of examples. Letfs head over to the next section and get to work.
Out of the box, Zend Frameworkfs database support works, but it requires an additional piece to function properly when using it with PHPs PDO.
Whats PDO, how do you get it, and how can you install it? PDO, which stands for PHP Data Object, is an extension that requires and is included with all PHP 5 installations. PDO allows a PHP developer using different relational database management systems (RDBMSs) to access data in a universal way. If the system used MySQL without the PDO extension, you would execute a query by calling the PHP function mysql_query(). On the other hand, if you used an Oracle database you would need to call the equivalent query execution function oracle_execute(). Using PDO, the call you make to query a table in MySQL is the same for Oracle and Postgres. But donft get it confused with a data abstraction layer; it does not write SQL in any way. Youfll leave that piece of the puzzle up to Zend Framework, as youfll see later in the chapter.
There are two ways to install the PDO extension. You can use PECL or you can pull up your sleeves and get dirty editing the php.ini file, which youfll do. Because you want to become an expert at this, take the php.ini path. For starters, you need to make sure that the .dll or.so files that you need were installed during the initial installation process. Open the directory PHP_HOME/ext and see if the following files are present:
If you do not see the files, don’t panic. Open the PHP installation zip file, or download it again from the PHP site, and extract the files from the ext drectory to PHP_HOME/ext on the computer. After all the files are extracted to the location, you’re one step closer to installing PDO.
Open the php.ini file and search for the following lines:
Uncomment the lines (if you are not using MySQL, uncomment the proper extension) by removing the ; from the front of the line. Save your changes and restart Apache.
Congratulations, you now have PDO!
Connecting to a Database
You should now have both PDO and Zend Framework installed. Let’s open a direct connection to the database to get things started. Create or open the Account Controller .php file located in application/ controllers and create a new action: testConnAction. The new action will demonstrate how to connect to the database for the first time and will use ViewRenderer setNoRender() to halt the use of a view.
The code to create an instance of a database connection uses one of many Zend_ Db_ Adapter classes shown as follows:
Depending on the RDBMS you are using, you can instantiate the proper adapter. In this case, use the Zend _Db _Adapter _Pdo _Mysql class because you are using the MySQL RDBMS.
The Zend _Db _Adapter _Pdo _Mysql class indirectly extends the Zend_ Db_ Adapter_ Abstract class and pulls in much of its functionality to execute queries on a database. The class accepts one parameter within its constructor: a key-value pair array or a Zend_Config object. You’re using the array that contains key-value pairs. The keys in the array are any of the parameters found in Table, and the value portion contains the desired value for the parameter.
In this example, you use five parameters: host, port, username, password, and dbname. The host parameter accepts only IP addresses, but makes an exception when using the keyword localhost, which will resolve to the IP address 127.0.0.1. You also set the port number to 3306 because MySQL runs on port 3306 by default. You might want to check which port number the installation is running on. The username parameter was also set to the username you used to access the database, the password parameter to the password you used to access the database, and the database name to loudbite.
Store the connection information into the $connParams variable and instantiate a Zend_ Db_ Adaptor_ Pdo_ Mysql object by passing in the $connParams variable into the constructor. Store the object into the $db variable and you’re done. Now any time within the connection scope you can refer to the $db object and can quickly utilize the database.
The example created an instance of the database, not a connection; it is not until you query the database that the connection is created, as Figure demonstrates. When you instantiate a Zend_Db object, it’s like arriving at the door to the database’s house and waiting for a cue to open the door. In this case, the cue is a database query. It is then that any errors connecting to the database appear.
At this point, create a reusable database connection file, a model that will allow you to set the database information in a single location and instantiate a Zend_ Db_ Adapter_ Pdo_ Mysql object throughout the application.
Create or add the application/models/Db directory and create a new file called Db_Db.php. The file contains a single conn() method that will return a Zend_Db_Adapter object, as shown in Listing.
Listing Db_Db. Php
With the reusable database model and an introduction on how to connect to a database, you’ve arrived at the door to the database’s house. Knock on the door by creating queries to insert data into the database.
Zend Related Interview Questions
|CSS3 Interview Questions||HTML Interview Questions|
|PHP Interview Questions||MySQL Interview Questions|
|Linux Interview Questions||Drupal Interview Questions|
|Magento Interview Questions||MVC Framework Interview Questions|
|CSS Interview Questions||WordPress Interview Questions|
|CakePHP Interview Questions||CodeIgniter Interview Questions|
Getting Started With Zend Framework
Writing Controllers Using Zend_controller
Views, Forms, Filters, And Validators
Database Communication, Manipulation, And Display
Sending And Receiving E-mail
Web Services And Feeds
Creating A Search Engine Using Zend_search_lucene
Caching With Zend Framework
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.