Skip to content

Commit f326653

Browse files
committed
Create DbModel.php and implement save
Rename Register.php into User.php and extend from DbModel
1 parent 3004e8c commit f326653

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

controllers/SiteController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use app\core\Controller;
1212
use app\core\Request;
13-
use app\models\Register;
13+
use app\models\User;
1414

1515
/**
1616
* Class SiteController
@@ -35,10 +35,10 @@ public function login()
3535

3636
public function register(Request $request)
3737
{
38-
$registerModel = new Register();
38+
$registerModel = new User();
3939
if ($request->getMethod() === 'post') {
4040
$registerModel->loadData($request->getBody());
41-
if ($registerModel->validate() && $registerModel->register()) {
41+
if ($registerModel->validate() && $registerModel->save()) {
4242
return 'Show success page';
4343
}
4444

core/DbModel.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* User: TheCodeholic
4+
* Date: 7/10/2020
5+
* Time: 9:19 AM
6+
*/
7+
8+
namespace app\core;
9+
10+
11+
/**
12+
* Class DbModel
13+
*
14+
* @author Zura Sekhniashvili <[email protected]>
15+
* @package app\core
16+
*/
17+
abstract class DbModel extends Model
18+
{
19+
abstract public static function tableName(): string;
20+
21+
abstract public function attributes(): array;
22+
23+
public function primaryKey(): string
24+
{
25+
return 'id';
26+
}
27+
28+
public function save()
29+
{
30+
$tableName = $this->tableName();
31+
$attributes = $this->attributes();
32+
$params = array_map(fn($attr) => ":$attr", $attributes);
33+
$statement = $this->prepare("INSERT INTO $tableName (" . implode(",", $attributes) . ")
34+
VALUES (" . implode(",", $params) . ")");
35+
foreach ($attributes as $attribute) {
36+
$statement->bindValue(":$attribute", $this->{$attribute});
37+
}
38+
$statement->execute();
39+
return true;
40+
}
41+
42+
public function prepare($sql): \PDOStatement
43+
{
44+
return Application::$app->db->pdo->prepare($sql);
45+
}
46+
}

models/Register.php renamed to models/User.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace app\models;
99

1010

11+
use app\core\DbModel;
1112
use app\core\Model;
1213

1314
/**
@@ -16,14 +17,24 @@
1617
* @author Zura Sekhniashvili <[email protected]>
1718
* @package app\models
1819
*/
19-
class Register extends Model
20+
class User extends DbModel
2021
{
2122
public string $firstname = '';
2223
public string $lastname = '';
2324
public string $email = '';
2425
public string $password = '';
2526
public string $passwordConfirm = '';
2627

28+
public static function tableName(): string
29+
{
30+
return 'users';
31+
}
32+
33+
public function attributes(): array
34+
{
35+
return ['firstname', 'lastname', 'email', 'password'];
36+
}
37+
2738
public function rules()
2839
{
2940
return [

0 commit comments

Comments
 (0)