/** * Save related user roles. * * @PostSave */ public function postSave(EntityEvent $event) { if (is_array($this->roles)) { $connection = $event->getConnection(); $connection->delete('@system_user_role', ['user_id' => $this->getId()]); if (!array_key_exists(Role::ROLE_AUTHENTICATED, $this->roles)) { $this->roles[Role::ROLE_AUTHENTICATED] = $event->getEntityManager()->find('Pagekit\\User\\Entity\\Role', Role::ROLE_AUTHENTICATED); } foreach ($this->roles as $role) { $connection->insert('@system_user_role', ['user_id' => $this->getId(), 'role_id' => $role->getId()]); } } }
/** * @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) { $this->modified = new \DateTime(); $repository = $event->getEntityManager()->getRepository(get_class($this)); $i = 2; $id = $this->id; while ($repository->query()->where('slug = ?', [$this->slug])->where(function ($query) use($id) { if ($id) { $query->where('id <> ?', [$id]); } })->first()) { $this->slug = preg_replace('/-\\d+$/', '', $this->slug) . '-' . $i++; } }