1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Faulancer\ORM; |
4
|
|
|
|
5
|
|
|
use Faulancer\Controller\Controller; |
6
|
|
|
use Faulancer\Exception\InvalidArgumentException; |
7
|
|
|
use Faulancer\ServiceLocator\ServiceLocator; |
8
|
|
|
use ORM\EntityManager; |
9
|
|
|
use ORM\Exception\IncompletePrimaryKey; |
10
|
|
|
use ORM\Exception\InvalidConfiguration; |
11
|
|
|
use ORM\Exception\InvalidName; |
12
|
|
|
use ORM\Exception\NoConnection; |
13
|
|
|
use ORM\Exception\NoEntity; |
14
|
|
|
use ORM\Exception\NoEntityManager; |
15
|
|
|
use ORM\Exception\NotScalar; |
16
|
|
|
use ORM\Exception\UnsupportedDriver; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Entity |
20
|
|
|
* |
21
|
|
|
* @property int $id |
22
|
|
|
* |
23
|
|
|
* @category Faulancer\ORM |
24
|
|
|
* @package Faulancer\ORM |
25
|
|
|
* @author Florian Knapp <[email protected]> |
26
|
|
|
* @license MIT License |
27
|
|
|
* @link No link provided |
28
|
|
|
*/ |
29
|
|
|
abstract class Entity extends \ORM\Entity |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Return data as array |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
* |
36
|
|
|
* @codeCoverageIgnore |
37
|
|
|
*/ |
38
|
|
|
public function getDataAsArray() |
39
|
|
|
{ |
40
|
|
|
return $this->getData(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Save entity to database and add error handling for duplicate keys |
45
|
|
|
* |
46
|
|
|
* @param EntityManager $dbManager The custom entity manager |
47
|
|
|
* @param bool $redirectOnDup If redirect should be initiated |
48
|
|
|
* on duplicate key error |
49
|
|
|
* |
50
|
|
|
* @return Entity|\ORM\Entity|bool |
51
|
|
|
* |
52
|
|
|
* @throws NoEntity |
53
|
|
|
* @throws NoConnection |
54
|
|
|
* @throws NotScalar |
55
|
|
|
* @throws UnsupportedDriver |
56
|
|
|
* @throws IncompletePrimaryKey |
57
|
|
|
* @throws InvalidConfiguration |
58
|
|
|
* @throws InvalidName |
59
|
|
|
* @throws NoEntityManager |
60
|
|
|
* @throws IncompletePrimaryKey |
61
|
|
|
* @throws InvalidArgumentException |
62
|
|
|
*/ |
63
|
|
|
public function save(EntityManager $dbManager = null, $redirectOnDup = false) |
64
|
|
|
{ |
65
|
|
|
if ($dbManager !== null) { |
66
|
|
|
$this->setEntityManager($dbManager); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
|
71
|
|
|
parent::save(); |
72
|
|
|
|
73
|
|
|
} catch (\PDOException $e) { |
74
|
|
|
|
75
|
|
|
if ($e->getCode() === "23000") { |
76
|
|
|
|
77
|
|
|
if ($redirectOnDup) { |
78
|
|
|
|
79
|
|
|
/** @var Controller $controller */ |
80
|
|
|
$controller = ServiceLocator::instance()->get( |
81
|
|
|
Controller::class |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$uri = $controller->getRequest()->getPath(); |
85
|
|
|
|
86
|
|
|
$controller->getSessionManager()->setFlashMessage( |
|
|
|
|
87
|
|
|
'db.duplicate.key', 'Eintrag bereits vorhanden!' |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$controller->redirect($uri); |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return false; |
|
|
|
|
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: