public function register(Application $app) { $default = ['wrapperClass' => 'Pagekit\\Component\\Database\\ConnectionWrapper', 'defaultTableOptions' => []]; $app['dbs'] = function ($app) use($default) { $dbs = []; foreach ($app['config']['database.connections'] as $name => $params) { $params = array_replace($default, $params); foreach (['engine', 'charset', 'collate'] as $option) { if (isset($params[$option])) { $params['defaultTableOptions'][$option] = $params[$option]; } } $events = $app['config']['database.default'] === $name ? $app['events'] : null; $dbs[$name] = new Connection($params, $events); } return $dbs; }; $app['db'] = function ($app) { return $app['dbs'][$app['config']['database.default']]; }; $app['db.em'] = function ($app) { EntityEvent::setApplication($app); return new EntityManager($app['db'], $app['db.metas'], 'Pagekit\\Framework\\Database\\Event\\EntityEvent'); }; $app['db.metas'] = function ($app) { $manager = new MetadataManager($app['db']); $manager->setLoader(new AnnotationLoader()); $manager->setCache($app['cache.phpfile']); return $manager; }; $app['db.debug_stack'] = function ($app) { return new DebugStack($app['profiler.stopwatch']); }; }
/** * @PreSave */ public function preSave(EntityEvent $event) { if (!$this->id) { $this->setPriority($event->getConnection()->fetchColumn('SELECT MAX(priority) + 1 FROM @system_role')); } }
/** * @PreDelete */ public function preDelete(EntityEvent $event) { $event->getEntityManager()->getRepository(get_class($this))->where(['parent_id = :old_parent'], [':old_parent' => $this->id])->update(['parent_id' => $this->parent_id]); }
/** * @PreSave */ public function preSave(EntityEvent $event) { if (!$this->id) { $this->setPriority($event->getConnection()->fetchColumn('SELECT MAX(priority) + 1 FROM @system_menu_item WHERE menu_id=? AND DEPTH=0', [$this->getMenuId()]) ?: 0); } }
/** * Delete all orphaned user role relations. * * @PostDelete */ public function postDelete(EntityEvent $event) { $event->getConnection()->delete('@system_user_role', ['user_id' => $this->getId()]); }
/** * @PreDelete */ public function preDelete(EntityEvent $event) { $event->getConnection()->delete('@blog_comment', ['post_id' => $this->getId()]); }