- selezionare le migration presenti nella migration table
- per ogni migration non presente nel DB, creare un'istanza ed esegue il metodo "up()"
+*** Database model class
+La classe DbModel si basa sulla base Database class ma viene trattata come l'effettiva SQL table.
+
** Migrations
È importante che le migration class siano ordinate e che seguano tutte lo stesso stile di nomeclatura.
email VARCHAR(255) NOT NULL,
firstname VARCHAR(255) NOT NULL,
lastname VARCHAR(255) NOT NULL,
- status TINYINT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=INNODB;";
--- /dev/null
+<?php
+
+use app\core\Application;
+
+class m_1650284365_addUsername
+{
+ public function up()
+ {
+ $db = Application::$app->db;
+ $sql = "ALTER TABLE users ADD COLUMN username VARCHAR(255) NOT NULL;";
+ $db->pdo->exec($sql);
+ }
+
+ public function down()
+ {
+ $db = Application::$app->db;
+ $sql = "ALTER TABLE users DROP COLUMN username;";
+ $db->pdo->exec($sql);
+ }
+}
+?>
use app\core\BaseController;
use app\core\Request;
-use app\models\RegisterModel;
+use app\models\User;
class AuthController extends BaseController{
public function login() {
public function register(Request $req) {
// $this->setLayout("auth");
$errors = [];
- $registerModel = new RegisterModel;
+ $registerModel = new User;
if ($req->getMethod() == "post") {
$registerModel->loadData($req->getBody());
if ($ruleName == self::RULE_MATCH && $value != $this->{$rule["match"]}) {
$this->addError($attribute, self::RULE_MATCH, $rule);
}
+ if ($ruleName == self::RULE_UNIQUE) {
+ $className = $rule["class"];
+ $uniqueAttr = $rule["attribute"] ?? $attribute;
+ $tableName = $className::tableName();
+
+ $statement = Application::$app->db->pdo->prepare("select * from $tableName where $uniqueAttr = :attr");
+ $statement->bindValue(":attr", $value);
+ $statement->execute();
+
+ if($statement->fetchObject()) {
+ $this->addError($attribute, self::RULE_UNIQUE, ["field" => $attribute]);
+ }
+ }
}
}
self::RULE_MIN => "Min length of this field must be {min}",
self::RULE_MAX => "Max length of this field must be {max}",
self::RULE_MATCH => "This field must be the same as {match}",
+ self::RULE_UNIQUE => "Record with this {field} already exist"
];
}
--- /dev/null
+<?php
+
+namespace app\core;
+
+abstract class DbModel extends BaseModel
+{
+ abstract public function tableName(): string;
+ abstract public function attributes(): array;
+
+ public function save()
+ {
+ $tableName = $this->tableName();
+ $attributes = $this->attributes();
+ $params = array_map(fn($attr) => ":$attr", $attributes);
+
+ $statement = self::prepare("INSERT INTO $tableName (".implode(',', $attributes).") VALUES (".implode(',', $params).")");
+
+ foreach ($attributes as $attr) {
+ $statement->bindValue(":$attr", $this->{$attr});
+ }
+
+ $statement->execute();
+ return true;
+ }
+
+ public static function prepare(string $sql) {
+ return Application::$app->db->pdo->prepare($sql);
+ }
+}
public function __construct(BaseModel $model, string $attribute) {
$this->model = $model;
- $this->attribute = $attribute;
+ $this->attribute = strtolower($attribute);
$this->type = self::TYPE_TEXT;
}
<input name="%s" type="%s" value="%s" class="form-control%s"/>
<div class="invalid-feedback">%s</div>
</div>',
- $this->attribute,
+ ucfirst($this->attribute),
$this->attribute,
$this->type,
$this->model->{$this->attribute},
+++ /dev/null
-<?php
-namespace app\models;
-
-use app\core\BaseModel;
-
-class RegisterModel extends BaseModel {
- public string $Firstname = "";
- public string $Lastname = "";
- public string $Email = "";
- public string $Username = "";
- public string $Password = "";
- public string $Confirm = "";
-
- public function register() {
- echo "Creating new user";
- }
-
- public function rules(): array {
- return [
- "Firstname" => [self::RULE_REQUIRED],
- "Lastname" => [self::RULE_REQUIRED],
- "Username" => [self::RULE_REQUIRED],
- "Email" => [self::RULE_REQUIRED, self::RULE_EMAIL],
- "Password" => [self::RULE_REQUIRED, [ self::RULE_MIN, "min" => 20 ], [ self::RULE_MAX, "max" => 100 ]],
- "Confirm" => [self::RULE_REQUIRED, [self::RULE_MATCH, "match" => "Password" ]]
- ];
- }
-}
-?>
--- /dev/null
+<?php
+
+namespace app\models;
+
+use app\core\DbModel;
+
+class User extends DbModel
+{
+ public string $firstname = "";
+ public string $lastname = "";
+ public string $email = "";
+ public string $username = "";
+ public string $password = "";
+ public string $confirm = "";
+
+ public function tableName(): string
+ {
+ return "users";
+ }
+
+ public function attributes(): array {
+ return [ "firstname", "lastname", "email", "username", "password" ];
+ }
+
+ public function register()
+ {
+ $this->password = password_hash($this->password, PASSWORD_DEFAULT);
+ return $this->save();
+ }
+
+ public function rules(): array
+ {
+ return [
+ "firstname" => [self::RULE_REQUIRED],
+ "lastname" => [self::RULE_REQUIRED],
+ "username" => [self::RULE_REQUIRED],
+ "email" => [self::RULE_REQUIRED, self::RULE_EMAIL, [self::RULE_UNIQUE, "class" => self::class ]],
+ "password" => [self::RULE_REQUIRED, [self::RULE_MIN, "min" => 20], [self::RULE_MAX, "max" => 100]],
+ "confirm" => [self::RULE_REQUIRED, [self::RULE_MATCH, "match" => "password"]]
+ ];
+ }
+}