コード例 #1
0
 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot()
 {
     CmfDbModel::setDbConnectionConfig(DbConnectionConfig::create()->setDriver(env('DB_CONNECTION', 'pgsql'))->setHost(env('DB_HOST', 'localhost'))->setDbName(env('DB_DATABASE'))->setUserName(env('DB_USERNAME'))->setPassword(env('DB_PASSWORD')));
     DbColumnConfig::registerType('password', DbColumnConfig::DB_TYPE_VARCHAR, PasswordField::class);
     /*if (app()->offsetExists('debugbar') && debugbar()->isEnabled()) {
           $timeCollector = (debugbar()->hasCollector('time')) ? debugbar()->getCollector('time') : null;
           $pdoCollector = new PDOCollector(null, $timeCollector);
           $pdoCollector->setRenderSqlWithParams(true);
           debugbar()->addCollector($pdoCollector);
           Db::setConnectionWrapper(function (Db $db, \PDO $pdo) {
               $pdoTracer = new TraceablePDO($pdo);
               if (debugbar()->hasCollector('pdo')) {
                   debugbar()->getCollector('pdo')->addConnection($pdoTracer, $db->getDbName());
               }
               return $pdoTracer;
           });
       }*/
 }
コード例 #2
0
 /**
  * Bootstrap any application services.
  *
  * @return void
  * @throws \DebugBar\DebugBarException
  * @throws \PeskyORM\Exception\DbConnectionConfigException
  */
 public function boot()
 {
     $driver = config('database.default');
     CmfDbModel::setDbConnectionConfig(DbConnectionConfig::create()->setDriver($driver)->setHost(config("database.connections.{$driver}.host"))->setDbName(config("database.connections.{$driver}.database"))->setUserName(config("database.connections.{$driver}.username"))->setPassword(config("database.connections.{$driver}.password")));
     DbColumnConfig::registerType('password', DbColumnConfig::DB_TYPE_VARCHAR, PasswordField::class);
     if (config('app.debug', false) && app()->offsetExists('debugbar') && debugbar()->isEnabled()) {
         $timeCollector = debugbar()->hasCollector('time') ? debugbar()->getCollector('time') : null;
         $pdoCollector = new PDOCollector(null, $timeCollector);
         $pdoCollector->setRenderSqlWithParams(true);
         debugbar()->addCollector($pdoCollector);
         Db::setConnectionWrapper(function (Db $db, \PDO $pdo) {
             $pdoTracer = new PeskyOrmPdoTracer($pdo);
             if (debugbar()->hasCollector('pdo')) {
                 debugbar()->getCollector('pdo')->addConnection($pdoTracer, $db->getDbName());
             }
             return $pdoTracer;
         });
     }
 }
コード例 #3
0
 /**
  * @param Request $request
  * @param CmfDbModel $model
  * @param string $inputNamePrefix - input name prefix
  *      For example if you use '_ids' instead of 'ids' - use prefix '_'
  * @return array|Response
  * @throws \PeskyCMF\Scaffold\ScaffoldException
  * @throws \PeskyCMF\PeskyCmfException
  * @throws \PeskyORM\Exception\DbQueryException
  * @throws \PeskyORM\Exception\DbException
  * @throws \PeskyORM\Exception\DbTableConfigException
  * @throws \PeskyORM\Exception\DbUtilsException
  * @throws \PeskyORM\Exception\DbModelException
  */
 private function getConditionsForBulkActions(Request $request, CmfDbModel $model, $inputNamePrefix = '')
 {
     $specialConditions = $this->getScaffoldConfig()->getFormConfig()->getSpecialConditions();
     $conditions = $specialConditions;
     $idsField = $inputNamePrefix . 'ids';
     $conditionsField = $inputNamePrefix . 'conditions';
     if ($request->has($idsField)) {
         $this->validate($request->data(), [$idsField => 'required|array', $idsField . '.*' => 'integer|min:1']);
         $conditions[$model->getPkColumnName()] = $request->data($idsField);
     } else {
         if ($request->has($conditionsField)) {
             $this->validate($request->data(), [$conditionsField => 'string|regex:%^[\\{\\[].*[\\}\\]]$%s']);
             $encodedConditions = $request->data($conditionsField) !== '' ? json_decode($request->data($conditionsField), true) : [];
             if ($encodedConditions === false || !is_array($encodedConditions) || empty($encodedConditions['r'])) {
                 return cmfJsonResponseForValidationErrors([$conditionsField => 'JSON expected']);
             }
             if (!empty($encodedConditions)) {
                 $dataGridConfig = $this->getScaffoldConfig()->getDataGridConfig();
                 $filterConditions = $this->getScaffoldConfig()->getDataGridFilterConfig()->buildConditionsFromSearchRules($encodedConditions);
                 if ($dataGridConfig->hasContains()) {
                     $subQueryConditions = array_merge(['CONTAIN' => $dataGridConfig->getContains()], $filterConditions, $specialConditions);
                     $subQuery = $model->builder()->fromOptions($model->resolveContains($subQueryConditions))->fields(['id'])->buildQuery(DbExpr::create("`{$model->getAlias()}`.`id`"), false, false);
                     $conditions = [DbExpr::create("`{$model->getPkColumnName()}` IN ({$subQuery})")];
                 } else {
                     $conditions = array_merge($filterConditions, $specialConditions);
                 }
             }
         } else {
             return cmfJsonResponseForValidationErrors([$idsField => 'List of items IDs of filtering conditions expected', $conditionsField => 'List of items IDs of filtering conditions expected']);
         }
     }
     return $conditions;
 }
コード例 #4
0
 public static function sendItemNotFoundResponse(CmfDbModel $model)
 {
     return response()->json(['_message' => CmfConfig::transBase('.error.resource_item_not_found'), 'redirect' => 'back', 'redirect_fallback' => route('cmf_items_table', ['table_name' => $model->getTableName()])], 404);
 }
コード例 #5
0
 /**
  * @param CmfDbModel $model
  * @param string $relationAlias
  * @param array $scannedModels
  * @param int $depth
  * @return bool|\PeskyCMF\Db\CmfDbModel
  * @throws \PeskyORM\Exception\DbUtilsException
  * @throws \PeskyORM\Exception\DbTableConfigException
  * @throws ScaffoldException
  * @throws \PeskyORM\Exception\DbModelException
  */
 protected function findRelatedModel(CmfDbModel $model, $relationAlias, array &$scannedModels = [], $depth = 0)
 {
     if ($model->hasTableRelation($relationAlias)) {
         return $model->getRelatedModel($relationAlias);
     }
     $scannedModels[] = $model->getTableName();
     foreach ($model->getTableRealtaions() as $alias => $relationConfig) {
         /** @var CmfDbModel $relModel */
         $relModel = $model->getRelatedModel($alias);
         if (!empty($scannedModels[$relModel->getTableName()])) {
             continue;
         }
         $modelFound = $this->findRelatedModel($relModel, $relationAlias, $scannedModels, $depth + 1);
         if ($modelFound) {
             return $modelFound;
         }
         $scannedModels[] = $relModel->getTableName();
     }
     if (!$depth === 0 && empty($modelFound)) {
         throw new ScaffoldException("Cannot find relation [{$relationAlias}] in model [{$model->getAlias()}] or among its relations");
     }
     return false;
 }
コード例 #6
0
ファイル: CmfConfig.php プロジェクト: swayok/PeskyCMF
 /**
  * Get ScaffoldSectionConfig instance
  * @param CmfDbModel $model - a model to be used in ScaffoldSectionConfig
  * @param string $tableName - table name passed via route parameter, may differ from $model->getTableName()
  *      and added here to be used in child configs when you need to use scaffolds with fake table names.
  *      It should be used together with static::getModelByTableName() to provide correct model for a fake table name
  * @return ScaffoldSectionConfig
  */
 public static function getScaffoldConfig(CmfDbModel $model, $tableName)
 {
     // $tableName is no tused by default and added here to be used in child configs
     $className = $model->getNamespace() . $model->getAlias() . static::scaffold_config_class_suffix();
     return new $className($model);
 }