|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Laravel Platfourm package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\Text\Models\Eloquent; |
|
12
|
|
|
|
|
13
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
14
|
|
|
use Longman\Platfourm\Database\Eloquent\ActionLog\ActionLogTrait; |
|
15
|
|
|
use Longman\Platfourm\Database\Eloquent\Model; |
|
16
|
|
|
|
|
17
|
|
|
class Text extends Model |
|
18
|
|
|
{ |
|
19
|
|
|
use ActionLogTrait; |
|
20
|
|
|
/** |
|
21
|
|
|
* The attributes that are mass assignable. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $fillable = ['key', 'lang', 'value', 'scope']; |
|
26
|
|
|
|
|
27
|
|
|
public $incrementing = false; |
|
28
|
|
|
|
|
29
|
|
|
protected $primaryKey = ['key', 'lang', 'scope']; |
|
30
|
|
|
|
|
31
|
|
|
protected $searchableFields = ['key', 'value']; |
|
32
|
|
|
|
|
33
|
|
|
protected $sortableFields = ['key', 'value']; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Set the keys for a save update query. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
39
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
40
|
|
|
*/ |
|
41
|
|
View Code Duplication |
protected function setKeysForSaveQuery(Builder $query) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$key = $this->getKeyName(); |
|
44
|
|
|
|
|
45
|
|
|
if (is_array($key)) { |
|
46
|
|
|
foreach ($key as $k) { |
|
47
|
|
|
$query->where($k, '=', $this->getKeyValueForSaveQuery($k)); |
|
48
|
|
|
} |
|
49
|
|
|
} else { |
|
50
|
|
|
$query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $query; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the primary key value for a save query. |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function getKeyValueForSaveQuery($key) |
|
62
|
|
|
{ |
|
63
|
|
|
if (isset($this->original[$key])) { |
|
64
|
|
|
return $this->original[$key]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->getAttribute($key); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.