ProductConfiguration.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (26.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\ProxmoxAddon\App\Models;
  20. use ModulesGarden\ProxmoxAddon\Core\Models\ExtendedEloquentModel;
  21. /**
  22. * @property string $setting
  23. * @property int $product_id
  24. * @property string $value
  25. * @method static ProductConfiguration ofProductId($productId)
  26. * @method static ProductConfiguration ofSetting($productId)
  27. */
  28. class ProductConfiguration extends ExtendedEloquentModel
  29. {
  30. protected $table = 'ProductConfiguration';
  31. protected $primaryKey = ['product_id', 'setting'];
  32. public $incrementing = false;
  33. /**
  34. * The attributes that should be cast to native types.
  35. *
  36. * @var array
  37. */
  38. protected $casts = [
  39. 'value' => 'array',
  40. ];
  41. /**
  42. * Eloquent fillable parameters
  43. * @var array
  44. */
  45. protected $fillable = ['product_id', 'setting', 'value'];
  46. public $timestamps = false;
  47. public function scopeOfProductId($query, $productId)
  48. {
  49. return $query->where("product_id", $productId);
  50. }
  51. public function scopeOfSetting($query, $setting)
  52. {
  53. return $query->where("setting", $setting);
  54. }
  55. public function scopeOfValue($query, $value)
  56. {
  57. return $query->where("value", $value);
  58. }
  59. /**
  60. * Set the keys for a save update query.
  61. *
  62. * @param \Illuminate\Database\Eloquent\Builder $query
  63. * @return \Illuminate\Database\Eloquent\Builder
  64. */
  65. protected function setKeysForSaveQuery( $query)
  66. {
  67. $keys = $this->getKeyName();
  68. if(!is_array($keys)){
  69. return parent::setKeysForSaveQuery($query);
  70. }
  71. foreach($keys as $keyName){
  72. $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
  73. }
  74. return $query;
  75. }
  76. /**
  77. * Get the primary key value for a save query.
  78. *
  79. * @param mixed $keyName
  80. * @return mixed
  81. */
  82. protected function getKeyForSaveQuery($keyName = null)
  83. {
  84. if(is_null($keyName)){
  85. $keyName = $this->getKeyName();
  86. }
  87. if (isset($this->original[$keyName])) {
  88. return $this->original[$keyName];
  89. }
  90. return $this->getAttribute($keyName);
  91. }
  92. }