getConnection() public method

Get the database connection for the model.
public getConnection ( ) : Connection
return Illuminate\Database\Connection
コード例 #1
0
ファイル: Index.php プロジェクト: menthol/Flexible
 /**
  * Import an Eloquent
  *
  * @param Model $model
  * @param array $relations
  * @param int $batchSize
  * @param callable $callback
  * @internal param $type
  */
 public function import(Model $model, $relations = [], $batchSize = 750, callable $callback = null)
 {
     $batch = 0;
     $asQueryLoggind = $model->getConnection()->logging();
     $model->getConnection()->disableQueryLog();
     while (true) {
         // Increase the batch number
         $batch += 1;
         // Load records from the database
         $records = $model->newInstance()->with($relations)->skip($batchSize * ($batch - 1))->take($batchSize)->get();
         // Break out of the loop if we are out of records
         if (count($records) == 0) {
             break;
         }
         // Call the callback function to provide feedback on the import process
         if ($callback) {
             $callback($batch);
         }
         // Transform each record before sending it to Elasticsearch
         $data = [];
         foreach ($records as $record) {
             $data[] = ['index' => ['_id' => $record->getEsId()]];
             $data[] = $record->transform(!empty($relations));
         }
         // Bulk import the data to Elasticsearch
         $this->bulk($data);
     }
     if ($asQueryLoggind) {
         $model->getConnection()->enableQueryLog();
     }
 }
コード例 #2
0
 /**
  * Set mock connection.
  *
  * @param  \Illuminate\Database\Eloquent\Model  $model
  */
 protected function addMockConnection(Model $model)
 {
     $resolver = m::mock('\\Illuminate\\Database\\ConnectionResolverInterface');
     $model->setConnectionResolver($resolver);
     $resolver->shouldReceive('connection')->andReturn(m::mock('\\Illuminate\\Database\\Connection'));
     $model->getConnection()->shouldReceive('getQueryGrammar')->andReturn(m::mock('\\Illuminate\\Database\\Query\\Grammars\\Grammar'));
     $model->getConnection()->shouldReceive('getPostProcessor')->andReturn(m::mock('\\Illuminate\\Database\\Query\\Processors\\Processor'));
 }
コード例 #3
0
ファイル: UseToScope.php プロジェクト: windqyoung/utils
 /**
  * {@inheritDoc}
  * @see \Illuminate\Database\Eloquent\ScopeInterface::apply()
  */
 public function apply(Builder $builder, Model $model)
 {
     /**
      * @var $model Model
      */
     $name = $model::getBootUseToTraitProperyName();
     $value = $model::${$name};
     $value = $model->getConnection()->getPdo()->quote($value);
     $builder->where($model->getQualifiedUseToFieldName(), new Expression($value));
     $this->addWithTrashed($builder);
 }
コード例 #4
0
 /**
  * Create fields for type.
  *
  * @return void
  */
 protected function schemaFields()
 {
     $table = $this->model->getTable();
     $schema = $this->model->getConnection()->getSchemaBuilder();
     $columns = collect($schema->getColumnListing($table));
     $columns->each(function ($column) use($table, $schema) {
         if (!$this->skipField($column)) {
             $this->generateField($column, $schema->getColumnType($table, $column));
         }
     });
 }
コード例 #5
0
 /**
  * @param Closure $closure
  * @return mixed
  * @throws Exception
  */
 protected function transaction(Closure $closure)
 {
     $connection = $this->model->getConnection();
     $connection->beginTransaction();
     try {
         $result = $closure();
         $connection->commit();
         return $result;
     } catch (Exception $e) {
         $connection->rollBack();
         throw $e;
     }
 }
コード例 #6
0
 public function performTransaction($amount, $sender_id, $recipient_id)
 {
     $connection = Eloquent::getConnection();
     try {
         $connection->getPdo()->beginTransaction();
         $sender = Balance::where('character_id', $sender_id)->first();
         $recipient = Balance::where('character_id', $recipient_id)->first();
         $sender->update(['amount' => $sender->amount - $amount]);
         $recipient->update(['amount' => $recipient->amount + $amount]);
         $connection->getPdo()->commit();
     } catch (\Exception $e) {
         $connection->getPdo()->rollback();
         return false;
     }
     return true;
 }
コード例 #7
0
ファイル: AdminTestCase.php プロジェクト: jaffle-be/framework
 protected function database(Model $model)
 {
     return $model->getConnection()->table($model->getTable());
 }
コード例 #8
0
 /**
  * Load the properties from the database table.
  *
  * @param \Illuminate\Database\Eloquent\Model $model
  */
 protected function getPropertiesFromTable($model)
 {
     $table = $model->getTable();
     $schema = $model->getConnection()->getDoctrineSchemaManager($table);
     $schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
     $columns = $schema->listTableColumns($table);
     if ($columns) {
         foreach ($columns as $column) {
             $name = $column->getName();
             $type = $column->getType()->getName();
             switch ($type) {
                 case 'string':
                 case 'text':
                 case 'date':
                 case 'time':
                 case 'guid':
                     $type = 'string';
                     break;
                 case 'integer':
                 case 'bigint':
                 case 'smallint':
                     $type = 'integer';
                     break;
                 case 'decimal':
                 case 'float':
                     $type = 'float';
                     break;
                 case 'boolean':
                     $type = 'boolean';
                     break;
                 case 'datetimetz':
                     //String or DateTime, depending on $dates
                 //String or DateTime, depending on $dates
                 case 'datetime':
                     $type = '\\Carbon\\Carbon';
                     break;
                 default:
                     $type = 'mixed';
                     break;
             }
             $this->setProperty($name, $type, true, true);
         }
     }
 }
コード例 #9
0
 private function getTableColumns(Model $model)
 {
     return $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
 }
 /**
  * @param \Illuminate\Database\Eloquent\Model $model
  */
 public function __construct(Model $model)
 {
     $this->model = $model;
     $this->db = $model->getConnection();
 }
コード例 #11
0
ファイル: Factory.php プロジェクト: dkulyk/eloquent-extra
 /**
  * Delete entity values.
  *
  * @throws \InvalidArgumentException
  */
 public function delete()
 {
     if (!in_array(SoftDeletes::class, class_uses_recursive(get_class($this->entity)), true) || $this->entity->forceDeleting) {
         $instance = new Value();
         $connection = $this->entity->getConnection();
         $connection->table($instance->getTable())->where('entity_id', $this->entity->getKey())->whereIn('property_id', $this->properties->pluck('id'))->delete();
     }
 }
コード例 #12
0
 /**
  * {@inheritdoc}
  */
 protected function getConnection()
 {
     return $this->model->getConnection();
 }
コード例 #13
0
ファイル: DbalHelpers.php プロジェクト: ThisVessel/Caravel
 /**
  * Check if column is nullable.
  *
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @param  string  $column
  * @return bool
  */
 public function getNullable($model, $column)
 {
     $schema = $model->getConnection()->getDoctrineSchemaManager($model->getTable());
     $columns = $schema->listTableColumns($model->getTable());
     return isset($columns[$column]) ? !$columns[$column]->getNotnull() : false;
 }
コード例 #14
0
 /**
  * Load the properties from the database table.
  *
  * @param \Illuminate\Database\Eloquent\Model $model
  */
 protected function getPropertiesFromTable($model)
 {
     $table = $model->getConnection()->getTablePrefix() . $model->getTable();
     $schema = $model->getConnection()->getDoctrineSchemaManager($table);
     $databasePlatform = $schema->getDatabasePlatform();
     $databasePlatform->registerDoctrineTypeMapping('enum', 'string');
     $platformName = $databasePlatform->getName();
     $customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", array());
     foreach ($customTypes as $yourTypeName => $doctrineTypeName) {
         $databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName);
     }
     $database = null;
     if (strpos($table, '.')) {
         list($database, $table) = explode('.', $table);
     }
     $columns = $schema->listTableColumns($table, $database);
     if ($columns) {
         foreach ($columns as $column) {
             $name = $column->getName();
             if (in_array($name, $model->getDates())) {
                 $type = 'datetime';
             } else {
                 $type = $column->getType()->getName();
             }
             if (!($model->incrementing && $model->getKeyName() === $name) && $name !== $model::CREATED_AT && $name !== $model::UPDATED_AT) {
                 if (!method_exists($model, 'getDeletedAtColumn') || method_exists($model, 'getDeletedAtColumn') && $name !== $model->getDeletedAtColumn()) {
                     $this->setProperty($name, $type);
                 }
             }
         }
     }
 }
コード例 #15
0
ファイル: ModelsCommand.php プロジェクト: erpio/Reportula
 /**
  * Load the properties from the database table.
  *
  * @param \Illuminate\Database\Eloquent\Model $model
  */
 protected function getPropertiesFromTable($model)
 {
     $table = $model->getConnection()->getTablePrefix() . $model->getTable();
     $schema = $model->getConnection()->getDoctrineSchemaManager($table);
     $schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
     $columns = $schema->listTableColumns($table);
     if ($columns) {
         foreach ($columns as $column) {
             $name = $column->getName();
             if (in_array($name, $model->getDates())) {
                 $type = '\\Carbon\\Carbon';
             } else {
                 $type = $column->getType()->getName();
                 switch ($type) {
                     case 'string':
                     case 'text':
                     case 'date':
                     case 'time':
                     case 'guid':
                     case 'datetimetz':
                     case 'datetime':
                         $type = 'string';
                         break;
                     case 'integer':
                     case 'bigint':
                     case 'smallint':
                         $type = 'integer';
                         break;
                     case 'decimal':
                     case 'float':
                         $type = 'float';
                         break;
                     case 'boolean':
                         $type = 'boolean';
                         break;
                     default:
                         $type = 'mixed';
                         break;
                 }
             }
             $this->setProperty($name, $type, true, true);
             $this->setMethod(Str::camel("where_" . $name), '\\Illuminate\\Database\\Query\\Builder|\\' . get_class($model), array('$value'));
         }
     }
 }
コード例 #16
0
ファイル: ModelsCommand.php プロジェクト: pacho104/redbpim
 /**
  * Load the properties from the database table.
  *
  * @param \Illuminate\Database\Eloquent\Model $model
  */
 protected function getPropertiesFromTable($model)
 {
     $table = $model->getConnection()->getTablePrefix() . $model->getTable();
     $schema = $model->getConnection()->getDoctrineSchemaManager($table);
     $databasePlatform = $schema->getDatabasePlatform();
     $databasePlatform->registerDoctrineTypeMapping('enum', 'string');
     $platformName = $databasePlatform->getName();
     $customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", array());
     foreach ($customTypes as $yourTypeName => $doctrineTypeName) {
         $databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName);
     }
     $database = null;
     if (strpos($table, '.')) {
         list($database, $table) = explode('.', $table);
     }
     $columns = $schema->listTableColumns($table, $database);
     if ($columns) {
         foreach ($columns as $column) {
             $name = $column->getName();
             if (in_array($name, $model->getDates())) {
                 $type = '\\Carbon\\Carbon';
             } else {
                 $type = $column->getType()->getName();
                 switch ($type) {
                     case 'string':
                     case 'text':
                     case 'date':
                     case 'time':
                     case 'guid':
                     case 'datetimetz':
                     case 'datetime':
                         $type = 'string';
                         break;
                     case 'integer':
                     case 'bigint':
                     case 'smallint':
                         $type = 'integer';
                         break;
                     case 'decimal':
                     case 'float':
                         $type = 'float';
                         break;
                     case 'boolean':
                         $type = 'boolean';
                         break;
                     default:
                         $type = 'mixed';
                         break;
                 }
             }
             $comment = $column->getComment();
             $this->setProperty($name, $type, true, true, $comment);
             $this->setMethod(Str::camel("where_" . $name), '\\Illuminate\\Database\\Query\\Builder|\\' . get_class($model), array('$value'));
         }
     }
 }
コード例 #17
0
 /**
  * @param Model $model
  * @param array $expected
  * @return int
  */
 private function countModels(Model $model, array $expected)
 {
     return $model->getConnection()->table($model->getTable())->where($expected)->count();
 }