ReplaceableModelTrait.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\common\traits;
  3. use Carbon\Carbon;
  4. use Illuminate\Support\Facades\DB;
  5. trait ReplaceableModelTrait
  6. {
  7. /**
  8. * Performs a 'replace' query with the data
  9. * @param array $attributes
  10. * @return bool t/f for success/failure
  11. */
  12. public static function replace(array $attributes = [])
  13. {
  14. return static::executeQuery('replace', $attributes);
  15. }
  16. /**
  17. * performs an 'insert ignore' query with the data
  18. * @param array $attributes
  19. * @return bool t/f for success/failure
  20. */
  21. public static function insertIgnore(array $attributes = [])
  22. {
  23. $model = new static();
  24. $driver = $model->GetConnection()->GetDriverName();
  25. switch ($driver) {
  26. case 'sqlite':
  27. return static::executeQuery('insert or ignore', $attributes);
  28. break;
  29. default:
  30. return static::executeQuery('insert ignore', $attributes);
  31. break;
  32. }
  33. }
  34. protected static function executeQuery($command, array $attributes)
  35. {
  36. if (!count($attributes)) {
  37. return true;
  38. }
  39. $model = new static();
  40. if ($model->fireModelEvent('saving') === false) {
  41. return false;
  42. }
  43. $attributes = collect($attributes);
  44. $first = $attributes->first();
  45. if (!is_array($first)) {
  46. $attributes = collect([$attributes->toArray()]);
  47. }
  48. // Check for timestamps
  49. // 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;
  50. // If you need to retain that, you shouldn't be using this package and should be using the standard eloquent system.
  51. if ($model->timestamps) {
  52. foreach ($attributes as $key=>$set) {
  53. if (empty($set[static::CREATED_AT])) {
  54. $set[static::CREATED_AT] = Carbon::now()->timestamp;
  55. }
  56. if (! is_null($model::UPDATED_AT) && empty($set[static::UPDATED_AT])) {
  57. $set[static::UPDATED_AT] = Carbon::now()->timestamp;
  58. }
  59. $attributes[$key] = $set;
  60. }
  61. }
  62. $keys = collect($attributes->first())->keys()
  63. ->transform(function ($key) {
  64. return "`".$key."`";
  65. });
  66. $bindings = [];
  67. $query = $command . " into " . DB::connection($model->getConnectionName())->getTablePrefix() . $model->getTable()." (".$keys->implode(",").") values ";
  68. $inserts = [];
  69. foreach ($attributes as $data) {
  70. $qs = [];
  71. foreach ($data as $value) {
  72. $qs[] = '?';
  73. $bindings[] = $value;
  74. }
  75. $inserts[] = '('.implode(",", $qs).')';
  76. }
  77. $query .= implode(",", $inserts);
  78. DB::connection($model->getConnectionName())->insert($query, $bindings);
  79. $model->fireModelEvent('saved', false);
  80. }
  81. }