toArray() with Doctrine 2 and Zend Forms.

Based on a couple of assumptions (like ‘NS’ is your library that handles the Doctrine Entity Manager) ..in your abstract class, will need 2 methods:

/**
 *  A way to force eager loading.
 */
public function forceEagerLoad() {
    return true;
}

/**
 * Returns the object and its properties as an array.
 */
public function toArray() {
    $tmpMergedMappings = array();
    $tmpFieldMappings = array();
    $tmpAssocMappings = array();
    if(!$this->em) { $this->em = NS::em(); }
    $testObj = $this->em->find(get_class($this), $this->id);
    $testJob = $testObj->job;
    $tmpFieldMappings = $this->em->getClassMetadata(get_class($this))->fieldMappings;
    $tmpAssocMappings = array_keys($this->em->getClassMetadata(get_class($this))->associationMappings);
    foreach($tmpFieldMappings as $fmKey => $fmValue) {
        if(is_object($this->$fmKey)) {
            if (get_class($this->$fmKey) == "DateTime" ) {
                switch ($tmpFieldMappings[$fmKey]["type"]) {
                    case "sndatetype":
                        $tmpMergedMappings[$fmKey] = $this->$fmKey->format('m/d/Y');
                        break;
                    // handle any custom types..
                    default:
                        $tmpMergedMappings[$fmKey] = $this->$fmKey->format('Y-m-d H:i:s');
                        break;
                }
            } else {
                // presume the default _id mapping...
                $key_id = $fmKey."_id";
                $tmpMergedMappings[$key_id] = $this->$key_id->id;
            }
        } else {
            $tmpMergedMappings[$fmKey] = $this->$fmKey;
        }
    }
    foreach($tmpAssocMappings as $amKey => $amValue) {
        $tmpKey = $amValue."_id";
        switch (get_class($this->$amValue)) {
            case "Doctrine\ORM\PersistentCollection":
                // dont do anything with these right now..
                break;
            default:
                // Trigger the loading via the proxy.
                if(method_exists($this->$amValue, 'forceEagerLoad')) {
                    $forced = $this->$amValue->forceEagerLoad();
                } else {
                    // Note: these classes dont have/inherit a forceEagerLoad() method,
                    // or we are trying to call it on something not set yet.
                    //var_dump(get_class($this->$amValue));
                    //var_dump($amValue);
                }
                if($this->$amValue) {
                    if($this->$amValue->id != null) {
                        $tmpMergedMappings[$tmpKey] = $this->$amValue->id;
                    }
                }
            break;
        }
    }
    return $tmpMergedMappings;

}

..and then in your Zend Controller action, say for editing:

/**
 *
 */
public function editAction() {
    $id = $this->getRequest()->getParam('id');
    $role = $this->em->find('NS\Role', $id);
    if(empty($role)) {
        // handle error
        return $this->_helper->redirector->gotoUrl('/role');
    } else {
        $this->view->role = $role;
        $this->view->form = $this->roleForm($role->toArray());
    }
}

..and your form can look something like this:

/**
*
*/
public function roleForm($data = null) {
    $form = new Zend_Form();
    $form->setAction($this->view->baseUrl().'/role/create')->setMethod('post');
    // id (hidden)
    $id = new Zend_Form_Element_Hidden('id');
    $id->removeDecorator('Label');
    $id->removeDecorator('HtmlTag');
    $form->addElement($id);
    // name
    $name = new Zend_Form_Element_Text('name');
    $name->setLabel('Name');
    $name->setRequired(true)->addValidator('NotEmpty');
    $form->addElement($name);
    // description
    $description = new Zend_Form_Element_Text('description');
    $description->setLabel('Description');
    $description->setRequired(true)->addValidator('NotEmpty');
    $form->addElement($description);
    // submit button
    $submit = new Zend_Form_Element_Submit('Save');
    $form->addElement($submit);
    if($data) {
        $form->setDefaults($data);
        $form->populate($data);
    }
    return $form;
}

Notes from ZendCon 2010 #zendcon #zc10

Brain dumping ZendCon 2010..  If you find any broken links or have links to slides/people I wasn’t able to find, please let me know! I’m aware that some presenters are holding back their slide decks. And some folks (mostly from the IBM-i sessions) don’t seem to have blogs or twitter accounts?

Overall Impressions:

This was my first ZendCon, and my overall impression is a positive one. I walked away with a much better understand of the community and Zend’s involvement in it. I also was very fortunate to meet many new people from all over the world and of wide skillset and experience. The networking and conversations that took place outside of the sessions and at restaurants or poolside over drinks is where the real connections are made.

Some Highlights:

I didn’t know what to think:

  • The food (breakfast and lunch) where about what you would expect from a conference of this caliber. I found myself more than once wanting to leave the venue at lunch and seek real food. Of exception is the dinner provided at the receptions in the evenings.
  • The vendor expo / floor. What a sad turnout (although I was told it was on-par with last years). All you had to do was walk by the Cloud Expo hall to catch a glimpse of what a real show looks like. I was also told the prize/swag ratio was higher at last year’s conference.
  • The constant fawning by some vendors to recruit the attendees. It was like watching a Jr. High School dance. I wish I could have worn a “got telecommute?” shirt. That would have started conversations with companies I’d be interested in.

Big Letdowns:

There were two main low points for me at the conference:

  • The scheduling snafu that caused Jonathan Wage’s sessions to get canceled. One of the main reasons I was looking forward to ZendCon was the sessions on Doctrine2. I’m not sure what lead to this, and I would hope it was a fluke.
  • The Keynotes and ‘The Cloud’. I’ve been to a few internet/tech conferences, and I’d like to think I can recognize when a presentation is not reaching it’s target audience. Most of the keynotes at ZendCon were no exception. If they were related to Zend products, I’d guess most of the attendees didn’t learn anything new. If the presentation at any point used “cloud” more than once, it instantly lost credibility with me (and I was not alone as many users in the #zendcon IRC channel chimed in similar skepticism). The CloudExpo conference was next door and I wondered more than once if the speaker had wandered into the wrong hall.

Books to read:

Completely Random:

  • Robotic Vacuum Overlords (via @naderman)
  • The Nikon to Canon Ratio – why do most php developers prefer Nikon? (the ratio was 5-1 at the conference by my count)
  • Need to follow-up with David Abdemoulaie (@hobodave) – Re: Doctrine2 pagination
  • My Tweets from the week of ZendCon.
  • I need to brush up on my German and French, learn Russian.

My joind.in Comments:

Sessions and Slides:

Nov 01, 2010

Nov 02, 2010

Nov 03, 2010

Nov 04, 2010

Uncons, etc..  ping me if you have more info on these or others:

Notes from PHP-Tek 10 #Tekx

PHP-Tek 10 was organized by PHP Architect and Blue Parabola and held in Chicago, IL from May 18th – 21st this year. This was my first “PHP conference” and I was surprised with the breadth of related topics covered. The was allot of buzz in the air, but a few topics seemed to be very prominent, including the challenges of scalability, nosql and mongodb, hip-hop, application security,  frameworks, and code testing and releasing management .

Below are links to the slides (if the presenters posted them online) and blog and twitter info for each presenter. Some of the presentations I haven’t been able to find online, so if you have the links, send them my way!

Building a Zend Framework application (Rob Allen [Twitter / Blog])

  - Slides: http://akrabat.com/wp-content/uploads/TekX-ZF-Tutorial.pdf

Converting Your MySQL App to NoSQL with MongoDB (Kristina Chodorow [Twitter / Blog])

Bad Guy For a Day A Websecurity hands-on tutorial (Arne Blankerts [Twitter])

   - Slides: http://www.slideshare.net/TheSeer/bad-guy-for-a-day-a-websecurity-handson-tutorial

Javascript for PHP Developers (Ed Finkler [Twitter / Blog])

   - Slides: http://funkatron.com/content/JSforPHPdevs-tekx.pdf

PHP Best Practices (Matthew Weier O’Phinney [Twitter / Blog]) / (Lorna Jane Mitchell [Twitter / Blog])

   - Slides: http://www.slideshare.net/lornajane/best-practices-tekx

PHP Code Review (Sebastian Bergmann [Twitter / Blog]) / (Arne Blankerts [Twitter])

   - Slides: http://www.slideshare.net/sebastian_bergmann/php-code-review-4142719

The Lost Art of Simplicity (Josh Holmes [Twitter / Blog])

   - Slides: http://www.slideshare.net/joshholmes/the-lost-art-of-simplicity

Anti-spam and anti-gaming (Eli White [Twitter / Blog])

- Slides: http://eliw.com/presentations/tek-2010/tek-2010-antispamgame.pdf

Apache Cookbook (Rich Bowen [Twitter / Blog])

- Slides: http://www.slideshare.net/rbowen/apache-cookbook-tekx-chicago-2010

Working with Zend_Form (Rob Allen [Twitter / Blog])

   - Slides: http://akrabat.com/wp-content/uploads/TekX-Zend-Form.pdf

Advanced Date/Time handling with PHP (Derick Rethans [Twitter / Blog])

   - Slides: http://derickrethans.nl/talks/time-tek10.pdf

PHP Essentials (Beth Tucker [Twitter / Blog])

Graphs, Edges & Nodes: Untangling the Social Web (Joël Perras [Twitter / Blog])

   - Slides: http://www.slideshare.net/jperras/graphs-edges-nodes-untangling-the-social-web

Large Scale Systems (David Strauss [Twitter / Blog])

Subversion in a Distributed World (Lorna Jane Mitchell [Twitter / Blog])

   - Slides: http://www.slideshare.net/lornajane/subversion-in-a-distributed-world

Flex + Flickr = Fleckr? Part 1 (Keith Casey [Twitter / Blog])

PHP Looking Into the Future (Scott MacVicar [Twitter / Blog])

  - Slides: http://talks.macvicar.net/tekx-php-future.pdf

Getting Git (Travis Swicegood)

Flex + Flickr = Fleckr? Part 2 (Keith Casey [Twitter / Blog])

SQL Injection Myths and Fallacies (Bill Karwin [Blog])

   - Slides: http://www.slideshare.net/billkarwin/sql-injection-myths-and-fallacies

Code & Release Management (Eli White [Twitter / Blog])

- Slides: http://eliw.com/presentations/tek-2010/tek-2010-coderelease.pdf

Best and Worst Practices Building Rich Internet Applications RIAs (Josh Holmes [Twitter / Blog])

Domain NoSQL: Next Generation Play-Doh (Matthew Weier O’Phinney [Twitter / Blog])

Continuous Inspection and Integration of PHP Projects (Sebastian Bergmann [Twitter / Blog])

  - Slides: http://www.slideshare.net/sebastian_bergmann/continuous-integration-of-php-projects-4159699

Desktop Apps with PHP and Titanium (Ben Ramsey [Twitter / Blog])

   - Slides: http://www.slideshare.net/benramsey/desktop-apps-with-php-and-titanium

10 Developer Trends in 2010 (Matthew Schmidt)

A Web Application Framework for People who Hate Frameworks – Lithium (Nate Abele [Twitter / Blog]) / (Joël Perras [TwitterBlog]) /

- Slides: http://www.slideshare.net/jperras/tekx-a-framework-for-people-who-hate-frameworks-lithium

Introduction to Testing with Selenium (Arne Blankerts [Twitter])

  - Slides: http://www.slideshare.net/TheSeer/intro-toselenium

XDebug (Derick Rethans [Twitter / Blog])

   - Slides: http://derickrethans.nl/talks/xdebug-tek10.pdf

Agile in a waterfall world (Jason Sweat [Twitter / Blog])

   - Slides: http://blog.casey-sweat.us/talks/tekx_AgileWaterfall.pdf

MongoDB for Mobile Applications (Kristina Chodorow [Twitter / Blog])

New SPL Features in PHP 5.3 (Matthew Turland [Twitter / Blog])

   - Slides: http://www.slideshare.net/tobias382/new-spl-features-in-php-53

Streams, Sockets and Filters – Oh My (Elizabeth Marie Smith [Twitter / Blog])

  - Slides: http://elizabethmariesmith.com/slides/Streams,%20Sockets%20and%20Filters%20Oh%20My!.pdf
  - Slides (Notes): http://elizabethmariesmith.com/slides/Streams,%20Sockets%20and%20Filters%20Oh%20My!%20-%20notes.pdf

Measuring Your Code (Nate Abele [Twitter / Blog])

Put down the Superglobals! Secure PHP Development with Inspekt (Ed Finkler [Twitter / Blog])

Models for Hierarchical Data with SQL and PHP (Bill Karwin [Blog])

   - Slides: http://www.slideshare.net/billkarwin/models-for-hierarchical-data

The Art of Message Queues (Mike Willbanks)

Tips & Tricks to get the most of PHP with IIS, Windows, and the Windows Azure Cloud (Sumit Chawla) / (Kanwaljeet Singla)

  - Slides: http://www.slideshare.net/ksingla/how-to-get-the-most-with-windows-and-windows-azure

MySQL Scalability (Ligaya Turmelle [Twitter / Blog])

   - Slides: http://www.slideshare.net/ligaya/mysql-55

Building Real-Time Applications with XMPP (Travis Swicegood)

HipHop for PHP (Scott MacVicar [Twitter / Blog])

Caching with Memcached and APC (Ben Ramsey [Twitter / Blog])

   - Slides: http://www.slideshare.net/benramsey/caching-with-memcached-and-apc

Lean Mean PHP Machine (Jason Austin [Twitter / Blog])

  - Slides: http://www.slideshare.net/jfaustin/lean-mean-php-machine

PHP Inside (Derick Rethans [Twitter / Blog])

   - Slides: http://derickrethans.nl/talks/phpinside-tek10.pdf

Open Source Your Career (Lorna Jane Mitchell [Twitter / Blog])

Turning Numbers into Stories (Ryan Stewart [Blog])

Design Patterns (Jason Sweat [Twitter / Blog])

  - Slides: http://blog.casey-sweat.us/talks/tekx_patterns.pdf

Cross Platform PHP (Elizabeth Marie Smith [Twitter / Blog])

   - Slides: http://elizabethmariesmith.com/slides/Cross%20Platform%20PHP.pdf

Replication with MySQL (Ligaya Turmelle [Twitter / Blog])

   - Slides: http://www.slideshare.net/ligaya/mysql-51-replication