| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace app\common\traits;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- trait ReplaceableModelTrait
- {
- /**
- * Performs a 'replace' query with the data
- * @param array $attributes
- * @return bool t/f for success/failure
- */
- public static function replace(array $attributes = [])
- {
- return static::executeQuery('replace', $attributes);
- }
- /**
- * performs an 'insert ignore' query with the data
- * @param array $attributes
- * @return bool t/f for success/failure
- */
- public static function insertIgnore(array $attributes = [])
- {
- $model = new static();
- $driver = $model->GetConnection()->GetDriverName();
- switch ($driver) {
- case 'sqlite':
- return static::executeQuery('insert or ignore', $attributes);
- break;
- default:
- return static::executeQuery('insert ignore', $attributes);
- break;
- }
- }
- protected static function executeQuery($command, array $attributes)
- {
- if (!count($attributes)) {
- return true;
- }
- $model = new static();
- if ($model->fireModelEvent('saving') === false) {
- return false;
- }
- $attributes = collect($attributes);
- $first = $attributes->first();
- if (!is_array($first)) {
- $attributes = collect([$attributes->toArray()]);
- }
- // Check for timestamps
- // Note that because we are actually deleting the record in the case of replace, we don't have reference to the original created_at timestamp;
- // If you need to retain that, you shouldn't be using this package and should be using the standard eloquent system.
- if ($model->timestamps) {
- foreach ($attributes as $key=>$set) {
- if (empty($set[static::CREATED_AT])) {
- $set[static::CREATED_AT] = Carbon::now()->timestamp;
- }
- if (! is_null($model::UPDATED_AT) && empty($set[static::UPDATED_AT])) {
- $set[static::UPDATED_AT] = Carbon::now()->timestamp;
- }
- $attributes[$key] = $set;
- }
- }
- $keys = collect($attributes->first())->keys()
- ->transform(function ($key) {
- return "`".$key."`";
- });
- $bindings = [];
- $query = $command . " into " . DB::connection($model->getConnectionName())->getTablePrefix() . $model->getTable()." (".$keys->implode(",").") values ";
- $inserts = [];
- foreach ($attributes as $data) {
- $qs = [];
- foreach ($data as $value) {
- $qs[] = '?';
- $bindings[] = $value;
- }
- $inserts[] = '('.implode(",", $qs).')';
- }
- $query .= implode(",", $inserts);
- DB::connection($model->getConnectionName())->insert($query, $bindings);
- $model->fireModelEvent('saved', false);
- }
- }
|