-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.php
More file actions
120 lines (96 loc) · 3.67 KB
/
Module.php
File metadata and controls
120 lines (96 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Zeal ORM
*
* @link http://github.com/tfountain
* @copyright Copyright (c) 2010-2013 Tim Fountain (http://tfountain.co.uk/)
* @license http://tfountain.co.uk/license New BSD License
*/
namespace Zeal\Orm;
use Zend\Mvc\MvcEvent;
use Zeal\Orm\Adapter\Zend\Db;
use Zeal\Orm\Orm;
use Zend\EventManager\SharedEventManager;
use Zeal\Orm\Identity\Map as IdentityMap;
use Zeal\Orm\Listener\IdentityMapListener;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$serviceLocator = $e->getApplication()->getServiceManager();
Orm::setServiceLocator($serviceLocator);
$identityMap = $serviceLocator->get('ZealOrm\Identity\Map');
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach(new IdentityMapListener($identityMap));
$events = $eventManager->getSharedManager();
// if an auto incrementing primary key is being used, ensure it is
// populated after creation when using the DB adapter
$events->attach('mapper', 'create.post', function ($e) {
$mapper = $e->getTarget();
$params = $e->getParams();
$object = $params['object'];
$adapter = $mapper->getAdapter();
if ($adapter instanceof Db) {
$primaryKey = $mapper->getAdapterOption('primaryKey');
if ($primaryKey && $mapper->getAdapterOption('autoIncrement', true)) {
$id = $mapper->getAdapter()->getTableGateway()->getAdapter()->getDriver()->getLastGeneratedValue();
if (is_scalar($id)) {
$object->$primaryKey = $id;
}
}
}
}, 999);
// save associated data
$events->attach('mapper', array('create.post', 'update.post'), function ($e) {
$mapper = $e->getTarget();
$params = $e->getParams();
$object = $params['object'];
$associationsToSave = $object->getAssociationsWithUnsavedData();
if ($associationsToSave) {
foreach ($associationsToSave as $shortname => $association) {
$associationMapper = $association->getTargetMapper();
$adapter = $associationMapper->getAdapter();
$association->saveData($object, $adapter);
}
}
}, 900);
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getServiceConfig()
{
return array(
'factories' => array(
'ZealOrm\Adapter\Zend\Db' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$adapter = new Db($dbAdapter);
return $adapter;
},
'ZealOrm\Identity\Map' => function ($sm) {
$map = new IdentityMap();
return $map;
}
),
'abstract_factories' => array(
'ZealOrm\Service\AbstractMapperFactory',
'ZealOrm\Service\AbstractModelFactory',
),
'aliases' => array(
'zeal_default_adapter' => 'ZealOrm\Adapter\Zend\Db'
),
'shared' => array(
// the DB adapter includes mapper-specific options, so we always
// want a new instance of this
'ZealOrm\Adapter\Zend\Db' => false,
),
);
}
}