Adds the ability for custom relations to be added to a model during runtime. These relations behave in the same way that you would expect; they can be queried, eager loaded, and accessed as an attribute.
Наследование: extends Illuminate\Database\Eloquent\Model
Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function boot()
 {
     if ($this->app->isInstalled()) {
         AbstractModel::setConnectionResolver($this->app->make('Illuminate\\Database\\ConnectionResolverInterface'));
         AbstractModel::setEventDispatcher($this->app->make('events'));
     }
 }
Пример #2
0
 protected function install()
 {
     try {
         $this->settings = $this->dataSource->getSettings();
         $this->adminUser = $admin = $this->dataSource->getAdminUser();
         if (strlen($admin['password']) < 8) {
             throw new Exception('Password must be at least 8 characters.');
         }
         if ($admin['password'] !== $admin['password_confirmation']) {
             throw new Exception('The password did not match its confirmation.');
         }
         if (!filter_var($admin['email'], FILTER_VALIDATE_EMAIL)) {
             throw new Exception('You must enter a valid email.');
         }
         if (!$admin['username'] || preg_match('/[^a-z0-9_-]/i', $admin['username'])) {
             throw new Exception('Username can only contain letters, numbers, underscores, and dashes.');
         }
         $this->runMigrations();
         $this->writeSettings();
         $this->application->register('Flarum\\Core\\CoreServiceProvider');
         $resolver = $this->application->make('Illuminate\\Database\\ConnectionResolverInterface');
         AbstractModel::setConnectionResolver($resolver);
         AbstractModel::setEventDispatcher($this->application->make('events'));
         $this->seedGroups();
         $this->seedPermissions();
         $this->createAdminUser();
         $this->enableBundledExtensions();
         $this->publishAssets();
     } catch (Exception $e) {
         @unlink($this->getConfigFile());
         throw $e;
     }
 }
Пример #3
0
 /**
  * Boot the model.
  *
  * @return void
  */
 public static function boot()
 {
     parent::boot();
     static::deleted(function (Group $group) {
         $group->raise(new GroupWasDeleted($group));
         $group->permissions()->delete();
     });
 }
Пример #4
0
Файл: Tag.php Проект: cmaas/tags
 /**
  * {@inheritdoc}
  */
 public static function boot()
 {
     parent::boot();
     static::deleted(function ($tag) {
         $tag->discussions()->detach();
         Permission::where('permission', 'like', "tag{$tag->id}.%")->delete();
     });
 }
Пример #5
0
 /**
  * Boot the model.
  *
  * @return void
  */
 public static function boot()
 {
     parent::boot();
     static::deleted(function ($discussion) {
         $discussion->raise(new DiscussionWasDeleted($discussion));
         // Delete all of the posts in the discussion. Before we delete them
         // in a big batch query, we will loop through them and raise a
         // PostWasDeleted event for each post.
         $posts = $discussion->posts()->allTypes();
         foreach ($posts->get() as $post) {
             $discussion->raise(new PostWasDeleted($post));
         }
         $posts->delete();
         // Delete all of the 'state' records for all of the users who have
         // read the discussion.
         $discussion->readers()->detach();
     });
 }
Пример #6
0
 /**
  * Boot the model.
  *
  * @return void
  */
 public static function boot()
 {
     parent::boot();
     // Don't allow the root admin to be deleted.
     static::deleting(function (User $user) {
         if ($user->id == 1) {
             throw new DomainException('Cannot delete the root admin');
         }
     });
     static::deleted(function (User $user) {
         $user->raise(new UserWasDeleted($user));
         // Delete all of the posts by the user. Before we delete them
         // in a big batch query, we will loop through them and raise a
         // PostWasDeleted event for each post.
         $posts = $user->posts()->allTypes();
         foreach ($posts->get() as $post) {
             $user->raise(new PostWasDeleted($post));
         }
         $posts->delete();
         $user->read()->detach();
         $user->groups()->detach();
         $user->accessTokens()->delete();
         $user->notifications()->delete();
     });
     static::$dispatcher->fire(new ConfigureUserPreferences());
 }
Пример #7
0
 /**
  * Create a new model instance according to the post's type.
  *
  * @param array $attributes
  * @param string|null $connection
  * @return static|object
  */
 public function newFromBuilder($attributes = [], $connection = null)
 {
     $attributes = (array) $attributes;
     if (!empty($attributes['type']) && isset(static::$models[$attributes['type']]) && class_exists($class = static::$models[$attributes['type']])) {
         /** @var Post $instance */
         $instance = new $class();
         $instance->exists = true;
         $instance->setRawAttributes($attributes, true);
         $instance->setConnection($connection ?: $this->connection);
         return $instance;
     }
     return parent::newFromBuilder($attributes, $connection);
 }
Пример #8
0
 protected function install()
 {
     try {
         $this->dbConfig = $this->dataSource->getDatabaseConfiguration();
         $validation = $this->getValidator()->make($this->dbConfig, ['driver' => 'required|in:mysql', 'host' => 'required', 'database' => 'required|string', 'username' => 'required|string', 'prefix' => 'alpha_dash|max:10']);
         if ($validation->fails()) {
             throw new Exception(implode("\n", call_user_func_array('array_merge', $validation->getMessageBag()->toArray())));
         }
         $this->baseUrl = $this->dataSource->getBaseUrl();
         $this->settings = $this->dataSource->getSettings();
         $this->adminUser = $admin = $this->dataSource->getAdminUser();
         if (strlen($admin['password']) < 8) {
             throw new Exception('Password must be at least 8 characters.');
         }
         if ($admin['password'] !== $admin['password_confirmation']) {
             throw new Exception('The password did not match its confirmation.');
         }
         if (!filter_var($admin['email'], FILTER_VALIDATE_EMAIL)) {
             throw new Exception('You must enter a valid email.');
         }
         if (!$admin['username'] || preg_match('/[^a-z0-9_-]/i', $admin['username'])) {
             throw new Exception('Username can only contain letters, numbers, underscores, and dashes.');
         }
         $this->storeConfiguration();
         $resolver = $this->application->make('Illuminate\\Database\\ConnectionResolverInterface');
         AbstractModel::setConnectionResolver($resolver);
         AbstractModel::setEventDispatcher($this->application->make('events'));
         $this->runMigrations();
         $this->writeSettings();
         $this->application->register('Flarum\\Core\\CoreServiceProvider');
         $this->seedGroups();
         $this->seedPermissions();
         $this->createAdminUser();
         $this->enableBundledExtensions();
         $this->publishAssets();
     } catch (Exception $e) {
         @unlink($this->getConfigFile());
         throw $e;
     }
 }