8th February 2012
Let's say that you want to set up a one-to-many relationship between two tables: Artists and Albums because you've refactored my ZF1 tutorial.
Let's assume that an artist has many albums. These are the basic table definitions:
artists table: id, artist
albums table: id, artist_id, title
When you list the albums, you obviously want to see the artist name rather than the id, so clearly you use a join!
Assuming you're using Zend_Db_Table, the easiest way is to turn off the integrity check and do a join in a mapper or table method.
Something like this:
class AlbumTable extends Zend_Db_Table_Abstract
{
protected $_name = 'album';
public function fetchAllWithArtistName($order = array('title ASC'))
{
$select = $this->select();
$select->setIntegrityCheck(false);
$select->from($this->_name);
$select->joinLeft('artist', 'album.artist_id = artist.id',
array('artist_name' => 'name'));
$select->order($order);
$rows = $this->fetchAll($select);
return $rows;
}
}
The row set returned will have all the columns from the albums table and one additional column called artist_name which is an alias of the name column from the artists table.
Posted in Zend Framework | 5 Comments »
30th January 2012
Jason Grimes has posted an article showing how to use Doctrine 2 with Zend Framework 2.
He uses my tutorial as the starting point which enables him to concentrate on the Doctrine integration rather than the irrelevant details about setting a ZF2 application which is excellent.
He walks through 6 steps in order to do the integration:
This article shows how to set up and use Doctrine 2 in Zend Framework 2, by extending Rob’s Getting Started tutorial to use Doctrine instead of Zend_Db.
- Start with Akrabat’s tutorial
- Install Doctrine modules
- Configure the Album module to use Doctrine
- Create the Album entity
- Update the Album controller to use Doctrine instead of Zend_Db
- That’s it!
I highly recommend having a read if you're at all interested in using Doctrine 2 with Zend Framework 2.
Posted in Zend Framework | 2 Comments »
21st December 2011
Zend Framework 2, Beta 2 has been released!
The key new features are:
- Refactored Mail component
- Refactored Cache component
- MVC updates
Check out Matthew's blog post for the full details.
I've also updated my tutorial. This is a good time to get involved, try it out and let us know what you like/dislike.
Posted in Zend Framework | 9 Comments »
18th October 2011
With the announcement of Zend Framework beta 1, I have updated my venerable tutorial to work with it!
Getting started with Zend Framework 2 (beta1), creates the same application as my ZF1 tutorial, so it should be very familiar, but this time, it's in the context of Zend Framework 2. As usual, it's a PDF too.
Please download it, try it out and let me know if you find any typos!
Posted in Zend Framework | 44 Comments »
20th June 2011
One thing that is different between Zend_Config_Xml and Zend_Config_Ini is that with Zend_Config_Xml you can pass in an XML string as the first parameter of the constructor and it will work. This doesn't work with Zend_Config_Ini as we use parse_ini_file() under the hood.
With PHP 5.3 however there is is a new function called parse_ini_string() which will allow us to load arbitrary ini string into Zend_Config objects. This can't go into Zend Framework 1 though due to our PHP 5.2.4 minimum version requirement.
As I needed this for a project, I extended Zend_Config_Ini to support this feature, which means simply overloading a single method
class App_Config_Ini extends Zend_Config_Ini
{
/**
* Load the INI file from disk using parse_ini_file(). Use a private error
* handler to convert any loading errors into a Zend_Config_Exception
*
* @param string $filename
* @throws Zend_Config_Exception
* @return array
*/
protected function _parseIniFile($filename)
{
set_error_handler(array($this, '_loadFileErrorHandler'));
if (substr($filename, -4) == '.ini') {
$iniArray = parse_ini_file($filename, true);
} else {
$iniArray = parse_ini_string($filename, true);
}
restore_error_handler();
// Check if there was a error while loading file
if ($this->_loadFileErrorStr !== null) {
/**
* @see Zend_Config_Exception
*/
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception($this->_loadFileErrorStr);
}
return $iniArray;
}
}
The actual change is to see if the last 4 characters of the filename are ".ini" and if they aren't then use parse_ini_string() instead of parse_ini_file(). The rest of the code is just error handling.
This is one area where I really like it when a class implements methods that done just one thing.
Posted in Zend Framework | 5 Comments »