Giter VIP home page Giter VIP logo

zf2-doctrine-example's Introduction

Zend Framework 2 with Doctrine example application using ZendForm

Introduction

This is an example application that encompasses the benefits of ZF2 with Doctrine but with clearly demonstrative code of how to interact Doctrine entity relationships with Zend Form. There are many guides you can follow to integrate Doctrine with ZF2 such as James Grimes' guide which is also mentioned on Rob Allen's blog, however I found any documentation on utilising both of these technologies with Zend Form hard to find, so I've written a small application that everyone can hopefully use to get them going with building their application using the wonders of an ORM.

Assuming you've followed James Grimes guide, you will have a small application that will add, edit and delete objects. However how about if you wish to use Doctrine relations inside your Zend Form object?

Lets get stuck in

Firstly, you need to create your relationships in your entities as usual, then in your Zend Form file, you will need to specify the element name as the same name as the entity property name you gave you relationship, then ensure your type is of DoctrineModule\Form\Element\ObjectSelect, DoctrineModule\Form\Element\ObjectRadio or DoctrineModule\Form\Element\ObjectMultiCheckbox. For this to work, you will need to specify at least an object_manager, the target_class to use and a property of the class to use as the Label.

As many tutorials build the form in the Form constructor, I'm following this method but have passed the entity manager object instance as a dependency into the Form constructor to then use as the object_manager value. The target_class will be the class name including the namespace of the class to find elements form, an example of this can be found in the Post Form

Once you've added all the necessary properties of the Form element, you will then need to add all the getters and setters for the Entity you've created the Form based upon, this is an essential part of Doctrine working alongside Zend Form. In addition to this, you will need to change the Hydrator of the Form object to the Doctrine instance in your controller, this will populate all elements of the Form including any relationship properties used.

$form->setHydrator(new DoctrineEntity($this->getEntityManager(),'path\to\my\entity'));

Once you've added the correct hydrator, you will now see all your saved values appear in the form and thats it, you've successfully integrated Doctrine with Zend Form :-)

Versions Used

  • doctrine/common 2.3.0
  • doctrine/dbal 2.3.2
  • doctrine/doctrine-module 0.7.1
  • doctrine/doctrine-orm-module 0.7.0
  • doctrine/orm 2.3.2
  • symfony/console v2.2.0
  • zendframework/zendframework 2.1.3

zf2-doctrine-example's People

Contributors

psamatt avatar

Watchers

Laurent Declercq avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.