|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Windstorm\Doctrine; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
|
6
|
|
|
use Rougin\Windstorm\UpdateInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Update Query |
|
10
|
|
|
* |
|
11
|
|
|
* @package Windstorm |
|
12
|
|
|
* @author Rougin Gutib <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class Update implements UpdateInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $initial; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Doctrine\DBAL\Query\QueryBuilder |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $builder; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Rougin\Windstorm\Doctrine\Query |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $query; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Initializes the query instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Rougin\Windstorm\Doctrine\Query $query |
|
35
|
|
|
* @param \Doctrine\DBAL\Query\QueryBuilder $builder |
|
36
|
|
|
* @param string $table |
|
37
|
|
|
* @param string|null $initial |
|
38
|
|
|
*/ |
|
39
|
15 |
|
public function __construct(Query $query, QueryBuilder $builder, $table, $initial) |
|
40
|
|
|
{ |
|
41
|
15 |
|
$this->builder = $builder->update($table, $initial); |
|
42
|
|
|
|
|
43
|
15 |
|
$this->query = $query; |
|
44
|
|
|
|
|
45
|
15 |
|
$this->initial = $initial; |
|
46
|
15 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Sets a new value for a column. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $key |
|
52
|
|
|
* @param mixed $value |
|
53
|
|
|
* @return self |
|
54
|
|
|
*/ |
|
55
|
15 |
|
public function set($key, $value) |
|
56
|
|
|
{ |
|
57
|
15 |
|
if ($this->initial && strpos($key, '.') === false) |
|
58
|
10 |
|
{ |
|
59
|
3 |
|
$key = $this->initial . '.' . $key; |
|
60
|
2 |
|
} |
|
61
|
|
|
|
|
62
|
15 |
|
$placeholder = $key[0] === ':' ? $key : ':' . $key; |
|
63
|
|
|
|
|
64
|
15 |
|
$placeholder = str_replace('.', '_', $placeholder); |
|
65
|
|
|
|
|
66
|
15 |
|
$this->builder->setParameter($placeholder, $value); |
|
67
|
|
|
|
|
68
|
15 |
|
$key = (string) $key . ' = ' . $placeholder; |
|
69
|
|
|
|
|
70
|
15 |
|
$this->builder->add('set', (string) $key, true); |
|
71
|
|
|
|
|
72
|
15 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Generates a WHERE query. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $key |
|
79
|
|
|
* @return \Rougin\Windstorm\WhereInterface |
|
80
|
|
|
*/ |
|
81
|
15 |
|
public function where($key) |
|
82
|
|
|
{ |
|
83
|
15 |
|
$this->query->builder($this->builder); |
|
84
|
|
|
|
|
85
|
15 |
|
return $this->query->where($key); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|