1 | <?php |
||
8 | |||
9 | trait Model |
||
10 | { |
||
11 | /** |
||
12 | * The model's attributes. |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $_attributes = []; |
||
16 | |||
17 | /** |
||
18 | * The model's database connection. |
||
19 | * @var Opeyemiabiodun\PotatoORM\Connections\Connection |
||
20 | */ |
||
21 | protected $_connection; |
||
22 | |||
23 | /** |
||
24 | * The primary key of the model's database table. |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $_primaryKey; |
||
28 | |||
29 | /** |
||
30 | * The model's database table. |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $_table; |
||
34 | |||
35 | /** |
||
36 | * The model's constructor method. |
||
37 | * @param Connection|null $connection An Opeyemiabiodun\PotatoORM\Connections\Connection instance or null |
||
38 | * @param string $table The name of the model's table in the database |
||
39 | */ |
||
40 | public function __construct(Connection $connection = null, $table = null) |
||
41 | { |
||
42 | if (is_null($connection)) { |
||
43 | $this->setConnection(new MySqlConnection()); |
||
44 | } else { |
||
45 | $this->setConnection($connection); |
||
46 | } |
||
47 | |||
48 | if (is_null($table)) { |
||
49 | $this->setTable(get_class($this).'-table'); |
||
50 | } else { |
||
51 | $this->setTable($table); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * The getter method for the model's properties. |
||
57 | * @param string $property The particular property |
||
58 | * @return int|float|string|bool The value of the property |
||
59 | */ |
||
60 | public function __get($property) |
||
61 | { |
||
62 | if (array_key_exists($property, $_attributes)) |
||
63 | { |
||
64 | return $_attributes[$property]; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * The setter method for the model's properties. |
||
70 | * @param string $property The particular property |
||
71 | * @param int|float|string|bool $value The value of the property |
||
72 | */ |
||
73 | public function __set($property, $value) |
||
74 | { |
||
75 | if (! is_scalar($value)) |
||
76 | { |
||
77 | throw new Exception("Error Processing Request", 1); |
||
78 | } |
||
79 | |||
80 | if (array_key_exists($property, $_attributes)) |
||
81 | { |
||
82 | $_attributes[$property] = $value; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Deletes a specified instance of the model in the database. |
||
88 | * @param int $number Specifies which model instance to delete; the 1st, 2nd, 3rd, ..... |
||
89 | * @return bool Returns boolean true if the instance was successfully deleted or else it returns false. |
||
90 | */ |
||
91 | public static function destroy($number) |
||
92 | { |
||
93 | return $this->_connection->deleteRecord($this->_table, $number - 1); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Finds a specified instance of the model in the database. |
||
98 | * @param int $number Specifies which model instance to find; the 1st, 2nd, 3rd, ..... |
||
99 | * @return array Returns the particular instance of the model. |
||
100 | */ |
||
192 |