Permalink
Browse files

Expanded readme.

Updated configuration.
  • Loading branch information...
1 parent 843b435 commit cf70fbe17c606ae7447407de75f770f9339ac3a4 Kyle Spraggs committed Oct 26, 2011
Showing with 166 additions and 17 deletions.
  1. +0 −1 Module.php
  2. +0 −5 README
  3. +61 −0 README.md
  4. 0 TODO → TODO.md
  5. +9 −3 configs/module.config.php
  6. +96 −8 src/SpiffyDoctrine/Service/Service.php
View
@@ -2,7 +2,6 @@
namespace SpiffyDoctrine;
use Doctrine\Common\Annotations\AnnotationRegistry,
- InvalidArgumentException,
Zend\Config\Config;
class Module
View
@@ -1,5 +0,0 @@
-1. Follow the setup in module.config.php and copy relative portions to application.config.php to overwrite.
-2. You can setup multiple entity manager alises by following the 'em-default' alias.
-
-In Controller:
-$entityManager = $this->getLocator()->get('em-default');
View
@@ -0,0 +1,61 @@
+# SpiffyDoctrine Module for Zend Framework 2
+
+The SpiffyDoctrine module intends to integrate Doctrine 2 with the Zend Framework quickly and easily.
+The following features are intended to work out of the box:
+
+ - Configuration and creation of multiple entity managers, cache instance, connections, and event managers.
+ - Specifying separate cache instances for metadata, query, and result caches.
+ - Using a SQL Logger.
+ - Configuration of annotations via registry files and/or namespaces (such as Gedmo DoctrineExtensions).
+
+## Installation
+
+The simplest way to install is to clone the repository into your /modules directory add the
+SpiffyDoctrine key to your modules array.
+
+ 1. cd my/project/folder
+ 2. git clone https://SpiffyJr@github.com/SpiffyJr/SpiffyDoctrine.git modules/SpiffyDoctrine --recursive
+ 3. open my/project/folder/configs/application.config.php and add 'SpiffyDoctrine' to your 'modules' parameter.
+ 4. Alter the configuration (most likely your connection) by adding the required changes to
+ my/project/folder/modules/Application/module.config.php.
+
+## Example standard configuration
+
+ // modules/Application/module.config.php
+ 'di' => array(
+ 'instance' => array(
+ 'doctrine-container' => array(
+ 'parameters' => array(
+ 'connection' => array(
+ 'default' => array(
+ 'evm' => 'default',
+ 'dbname' => 'mydb',
+ 'user' => 'root',
+ 'password' => '',
+ 'host' => 'localhost',
+ 'driver' => 'pdo_mysql'
+ )
+ )
+ )
+ )
+ )
+ )
+
+
+## Usage
+
+### Accessing the default, pre-configured, entity-manager instance
+A default EntityManager instance has been configured for you and is called em-default. You can access
+it from an ActionController using the locator as follows:
+
+ $em = $this->getLocator()->get('em-default');
+
+If for some reason you want access to additional objects such as the EVM, Cache, or Connection instances
+you can get them from the SpiffyDoctrine\Container\Container.
+
+ $container = $this->getLocator()->get('doctrine-container');
+
+
+## Doctrine CLI
+The Doctrine CLI has been pre-configured and is available in SpiffyDoctrine\bin. It should work as
+is without any special configuration required.
View
File renamed without changes.
@@ -8,7 +8,7 @@
'methods' => array(
'create' => array(
'name' => array(
- 'type' => 'array',
+ 'type' => false,
'required' => true
),
'container' => array(
@@ -17,12 +17,13 @@
)
),
)
- )
+ ),
),
),
'instance' => array(
'alias' => array(
'doctrine-container' => 'SpiffyDoctrine\Container\Container',
+ 'doctrine-service' => 'SpiffyDoctrine\Service\Service',
'em-default' => 'SpiffyDoctrine\Factory\EntityManager'
),
'em-default' => array(
@@ -31,12 +32,17 @@
'container' => 'doctrine-container'
)
),
+ 'doctrine-service' => array(
+ 'parameters' => array(
+ 'container' => 'doctrine-container'
+ )
+ ),
'doctrine-container' => array(
'parameters' => array(
'connection' => array(
'default' => array(
'evm' => 'default',
- 'dbname' => 'blitzaroo',
+ 'dbname' => 'mydb',
'user' => 'root',
'password' => '',
'host' => 'localhost',
@@ -1,26 +1,114 @@
<?php
namespace SpiffyDoctrine\Service;
-use Doctrine\ORM\EntityManager;
+use Doctrine\DBAL\Types\Type,
+ SpiffyDoctrine\Container\Container;
class Service
{
/**
- * @var EntityManager
+ * @var SpiffyDoctrine\Container\Container
*/
- protected $_em;
+ protected $_container;
- public function __construct(EntityManager $em)
+ /**
+ * Constructor.
+ *
+ * @param SpiffyDoctrine\Container\Container $container
+ */
+ public function __construct(Container $container)
{
- $this->_em = $em;
+ $this->_container = $container;
}
/**
- * Get the EntityManager.
+ * Get the Container.
*
+ * @return Container
+ */
+ public function getContainer()
+ {
+ return $this->_container;
+ }
+
+ /**
+ * Wrapper for Container::getEntityManager();
+ *
* @return EntityManager
*/
- public function getEntityManager()
+ public function getEntityManager($emName = Container::DEFAULT_KEY)
+ {
+ return $this->getContainer()->getEntityManager($emName);
+ }
+
+ /**
+ * Render this entity as an array.
+ *
+ * @param object $entity The entity to convert to an array.
+ * @param string $emName EntityManager name to use.
+ *
+ * @return array
+ */
+ public function toArray($entity, $emName = Container::DEFAULT_KEY)
+ {
+ $em = $this->getEntityManager($emName);
+
+ $result = array();
+ foreach($em->getClassMetadata(get_class($entity))->getFieldNames() as $field) {
+ $result[$field] = $this->_getFieldValue($entity, $field, $emName);
+ }
+
+ return $result;
+ }
+
+ /**
+ * Gets a field mapping from metadata information.
+ *
+ * @param object $entity
+ * @param string $field
+ * @param string $emName
+ *
+ * @return null|array
+ */
+ public function getFieldMapping($entity, $field, $emName = Container::DEFAULT_KEY)
+ {
+ $mdata = $this->getEntityManager($emName)->getClassMetadata(get_class($entity));
+
+ if (isset($mdata->fieldMappings[$field])) {
+ return $mdata->getFieldMapping($field);
+ }
+
+ if (isset($mdata->associationMappings[$field])) {
+ return $mdata->getAssociationMapping($field);
+ }
+
+ return null;
+ }
+
+ /**
+ * Get an entity field value.
+ *
+ * @param object $entity The entity to retrieve the field from.
+ * @param string $field Field to retrieve value for.
+ * @param string $emName EntityManager name to use.
+ *
+ * @return mixed
+ */
+ private function _getFieldValue($entity, $field, $emName = Container::DEFAULT_KEy)
{
- return $this->_em;
+ $getter = 'get' . ucfirst($field);
+ if (method_exists($entity, $getter)) {
+ return $this->$getter();
+ } else if (
+ ($mapping = $this->getFieldMapping($entity, $field, $emName)) &&
+ ($mapping['type'] == Type::BOOLEAN)
+ ) {
+ $isser = 'is' . ucfirst($field);
+ if (method_exists(entity, $isser)) {
+ return $this->$isser();
+ }
+ }
+
+ $em = $this->getEntityManager($emName);
+ return $em->getClassMetadata(get_class($entity))->reflFields[$field]->getValue($entity);
}
}

0 comments on commit cf70fbe

Please sign in to comment.