One-to-many joins with Zend_Db_Table_Select

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 »

Two new Sublime Text 2 packages for PHP

6th February 2012

Stuart Herbert has written two new Sublime Text packages for PHP:

  • Additional PHP Snippets
  • PHPUnit

The best way to install these is to install Package Control first and then use shift+cmd+P -> install package.

Even better, Stuart has rolled my getter/setter creation snippet into Additional PHP Snippets, so you can now have it without any hassle!

Update: Also, Ben Selby has created a package for DocBlox!

Posted in Software | 1 Comment »

Jason Grimes: Using Doctrine 2 in Zend Framework 2

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 »

Sublime Text 2 Snippet for PHP getter and setter generation

3rd January 2012

I've been playing with Sublime Text 2 recently and have quite enjoyed how quiet my ageing laptop is when the fans aren't running due to a Java-based IDE.

As with a lot of editors, Sublime Text supports snippets which are essentially text expansions of a short phrase into more text. I needed to create a few getXxx() and setXxx() methods for some properties of a class and decided that the easiest way to do this would be with a snippet.

To create a snippet, go to Tools->New Snippet... and replace the code example provided with this:


<snippet>
    <content><![CDATA[public function get${1/(.*)/\u$1/}()
{
    return \$this->${1:$SELECTION};
}

public function set${1/(.*)/\u$1/}(\$$1)
{
    \$this->$= \$$1;
    return \$this;
}
]]></content>
    <!-- OptionalTab trigger to activate the snippet -->
    <tabTrigger>getset</tabTrigger>
    <!-- OptionalScope the tab trigger will be active in -->
    <scope>source.php</scope>
    <!-- OptionalDescription to show in the menu -->
    <description>Create getter and setter methods</description>
</snippet>

Save the file as getset.sublime-snippet and you're done.

To use, simply type getset followed by tab (in the latest dev builds, at least) and it will automatically expand. Alternatively, select some text and use shift+cmd+p -> getset to automatically replace the selected text with the get and set methods completed for the text that was selected.

Posted in Software | 6 Comments »

2011 wrap up

31st December 2011

As I have done in 2008, 2009 and last year, I thought I'd continue my tradition of recapping my year.

January

PHPBNL 2011 happened in January. Also, my Mac OS X application, Daily Jotter was released onto the Mac App Store.

spacer

February

The first PHPucEU happened in Manchester which was great fun and, of course, the PHPUK conference in London. The biggest news for us though, was that we moved house in February!

spacer

March

I went to Canada for the rather excellent Confoo conference and watched a snowball fight!

spacer

April

A relaxed month after 3 months of conferences and a house move. There was a Royal wedding in the UK which resulted in a lot of merchandising!

spacer

May

May was the busiest month ever. Both sons have their birthdays in May and DPC in Amsterdam and php|tek in Chicago are back to back conferences! So many good photographs this month, but I have gone with this one of Jeremy Kendall as he took many excellent photographs at tek!

spacer

June

Our trip to the zoo this year was in June when we went to Twycross Zoo.

spacer

July

We went to the beach in July.

spacer

August

In August we went to the Fleet Air Arm Museum.

spacer

September

My eldest son achieved his Grade 1 classical guitar certification this month. We also went to a friend's wedding.

spacer

October

October saw the very excellent PHPNW conference return for its forth year and I was very glad to be asked to do a ZF2 tutorial which seemed to go well.

spacer

November

November was the month that John Arnold held his creativityex project. I particularly liked my effort for negative space:

spacer

December

Last month of the year and as part of the Worcester Flickr group, I got to take a photograph in a studio!

spacer

All in all, a pretty good year. One interesting thing I did notice was that I have a lot more good photos sitting in Aperture than I published. I think this is because Aperture is so slow to use on my old Macbook Pro. I was better at publishing photos with Capture NX2 and I thought that was slow; Clearly it wasn't that bad!

Posted in Me | No Comments »

« Previous Entries
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.