本文整理汇总了PHP中Illuminate\Database\Eloquent\Model::getConnection方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::getConnection方法的具体用法?PHP Model::getConnection怎么用?PHP Model::getConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Model
的用法示例。
在下文中一共展示了Model::getConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: import
/**
* 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: addMockConnection
/**
* 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: apply
/**
* {@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: schemaFields
/**
* 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: transaction
/**
* @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: performTransaction
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: database
protected function database(Model $model)
{
return $model->getConnection()->table($model->getTable());
}
示例8: getPropertiesFromTable
/**
* 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: getTableColumns
private function getTableColumns(Model $model)
{
return $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
}
示例10: __construct
/**
* @param \Illuminate\Database\Eloquent\Model $model
*/
public function __construct(Model $model)
{
$this->model = $model;
$this->db = $model->getConnection();
}
示例11: delete
/**
* 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: getConnection
/**
* {@inheritdoc}
*/
protected function getConnection()
{
return $this->model->getConnection();
}
示例13: getNullable
/**
* 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: getPropertiesFromTable
/**
* 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: getPropertiesFromTable
/**
* 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'));
}
}
}