public function getColumnValue($model, $column)
 {
     if ($column == 'classID') {
         $mapper = Orm::getMapper($model);
         $primaryKey = $mapper->getAdapter()->getPrimaryKey();
         return $model->{$primaryKey};
     }
     return parent::getColumnValue($model, $column);
 }
Exemple #2
0
 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 getSourceMapper()
 {
     if (!$this->sourceMapper) {
         $source = $this->getSource();
         if (!$source) {
             throw new \Exception('Unable to load source mapper as no source exists');
         }
         $this->sourceMapper = Orm::getMapper(get_class($source));
     }
     return $this->sourceMapper;
 }
 /**
  * Returns an instance of the adapter with the default options
  *
  * @return ZealOrm\Adapter\AdapterInterface
  */
 public static function getStaticAdapter()
 {
     $adapter = Orm::getDefaultAdapter();
     $adapter->setOptions(static::getDefaultAdapterOptions());
     return $adapter;
 }