Пример #1
0
 /**
  * Ensures provided datasource is containing data sets required for backing
  * roles management.
  *
  * @param db $source
  * @param boolean $force true to force validating provided source
  * @return db validated datasource provided in $source
  * @throws \RuntimeException
  */
 public static function validateDatasource(db $source = null, $force = false)
 {
     if ($source === null) {
         $source = datasource::getDefault();
     }
     if (!self::$validated || $force) {
         if (!$source->exists('role')) {
             if (false === $source->createDataset('role', array('name' => 'CHAR(32) NOT NULL UNIQUE', 'label' => 'CHAR(64) NOT NULL'))) {
                 throw new \RuntimeException('failed to prepare role set');
             }
         }
         if (!$source->exists('user_role')) {
             if (false === $source->createDataset('user_role', array('id' => null, 'user_uuid' => 'CHAR(36) NOT NULL', 'role_id' => 'INT UNSIGNED NOT NULL'), array('user_uuid', 'role_id'))) {
                 throw new \RuntimeException('failed to prepare role/user mapping set');
             }
         }
         self::$validated = true;
     }
     return $source;
 }
Пример #2
0
 /**
  * Creates query for browsing model's items stored in provided datasource.
  *
  * @param connection $source datasource containing model's items, omit for using default datasource
  * @param string $alias optional name of alias to explicitly use on model's data set in retrieved query
  * @return query query for listing items of current model
  */
 public static function browse(connection $source = null, $alias = null)
 {
     if ($source === null) {
         $source = datasource::getDefault();
     }
     if (!$source instanceof connection) {
         throw new \InvalidArgumentException(\de\toxa\txf\_L('missing link to datasource'));
     }
     static::updateSchema($source);
     $setName = preg_replace('/\\s+/', ' ', trim(static::$set_prefix . static::$set));
     if ($alias) {
         $parts = explode(' ', $setName);
         if (!is_string($alias)) {
             throw new \InvalidArgumentException('invalid type of alias');
         }
         $alias = explode(' ', preg_replace('/\\s+/', ' ', trim($alias)));
         $setName = $parts[0] . ' ' . $alias[0];
     }
     return $source->createQuery($setName);
 }