瀏覽代碼

CRUD tables

bufon2211 6 年之前
父節點
當前提交
a5682f7eb1

+ 95 - 0
repository/LanguagesRepository.php

@@ -0,0 +1,95 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: mrs
+ * Date: 15.01.2018
+ * Time: 14:16
+ */
+
+namespace App\Repository;
+
+use App\Model\Languages;
+
+class LanguagesRepository extends RepositoryAbstract
+{
+
+    public function __construct()
+    {
+        parent::__construct();
+        $this->entityName = 'languages';
+    }
+
+    /**
+     * @param $id
+     * @return null|Languages
+     */
+    public function findById($id)
+    {
+        $stmt = $this->pdo->prepare("SELECT * FROM {$this->entityName} WHERE id = :id");
+        $stmt->execute(['id' => $id]);
+        foreach ($stmt as $row) {
+            return new Languages($row['name'], $row['id']);
+        }
+        return null;
+    }
+
+    /**
+     * @return Languages[] array
+     */
+    public function findAll(): array
+    {
+        $stmt = $this->pdo->prepare("SELECT * FROM {$this->entityName}");
+        $stmt->execute();
+        $students = [];
+        foreach ($stmt as $row) {
+            $students[] = new Languages($row['name'], $row['id']);
+        }
+        return $students;
+    }
+
+    /**
+     * @param Languages $languages
+     */
+    public function save(Languages $languages)
+    {
+        $stmt = $this->pdo->prepare("INSERT INTO {$this->entityName} (name) VALUES(:name)");
+        $stmt->execute([
+            'name' => $languages->name,
+
+        ]);
+    }
+
+    /**
+     * @param Languages $languages
+     */
+    public function update(Languages $languages)
+    {
+        $stmt = $this->pdo->prepare("UPDATE {$this->entityName} SET name = :name WHERE id = :id");
+        $stmt->execute([
+            'id' => $languages->id,
+            'name' => $languages->name,
+        ]);
+    }
+
+
+    /**
+     * @param int $id
+     */
+    public function delete(int $id)
+    {
+        $stmt = $this->pdo->prepare("DELETE FROM {$this->entityName} WHERE id = :id");
+        $stmt->execute(['id' => $id]);
+    }
+
+    /**
+     * @param
+     */
+
+    public function deleteAll()
+    {
+        $stmt = $this->pdo->prepare("TRUNCATE TABLE {$this->entityName}");
+        $stmt->execute();
+    }
+
+
+}

+ 32 - 0
repository/RepositoryAbstract.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Repository;
+
+use PDO;
+use PDOException;
+
+/**
+ * Created by PhpStorm.
+ * User: artem
+ * Date: 20.05.17
+ * Time: 23:51
+ */
+abstract class RepositoryAbstract
+{
+    protected $pdo;
+    protected $entityName;
+    protected $db1;
+    protected $db2;
+
+    public function __construct()
+    {
+        require_once('../config/DBconnect.php');
+
+        $config = Singleton::getInstance();
+        $this->pdo = $config->pdo;
+        $this->db1 = 'student';
+        $this->db2 = 'languages';
+    }
+
+    public abstract function findById($id);
+}

+ 88 - 0
repository/StudentLanguagesRepository.php

@@ -0,0 +1,88 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: mrs
+ * Date: 15.01.2018
+ * Time: 14:37
+ */
+
+namespace App\Repository;
+
+
+class StudentLanguagesRepository extends RepositoryAbstract
+{
+
+    public function __construct()
+    {
+        parent::__construct();
+        $this->entityName = 'student_languages';
+
+
+    }
+
+    public function findById($id)
+    {
+        $stmt = $this->pdo->prepare("SELECT s.firstname,s.lastname,s.class, GROUP_CONCAT(l.name) AS name FROM {$this->db1} c
+            JOIN {$this->entityName} i ON (s.id = st.student_id) 
+            JOIN {$this->db2} m ON (l.id = st.languages_id) WHERE s.id = :id GROUP BY s.id;");
+        $stmt->execute(['id' => $id]);
+        foreach ($stmt as $row) {
+            return new Student_Languages ($row['firstname'], $row['lastname'], $row['class'], $row['name']);
+        }
+        return null;
+    }
+
+    public function findAll(): array
+    {
+        $stmt = $this->pdo->prepare("SELECT s.firstname,s.lastname,s.class, s.id, GROUP_CONCAT(l.name) AS name FROM {$this->db1} c 
+            JOIN student_languages i ON (s.id = st.student_id) 
+            JOIN languages m ON (l.id = st.languages_id) GROUP BY c.id;");
+        $stmt->execute();
+        $studlang = [];
+        foreach ($stmt as $row) {
+            $studlang[] = new Student_Languages($row['firstname'], $row['lastname'], $row['class'], $row['name'], $row['id']);
+        }
+        return $studlang;
+    }
+
+    public function getLanguages(): array
+    {
+        $stmt = $this->pdo->prepare("SELECT * FROM {$this->db2};");
+        $stmt->execute();
+        $getLang = [];
+        foreach ($stmt as $key => $row) {
+            $getLang[$row['id']] = $row['languages'];
+        }
+        return $getLang;
+    }
+
+    public function saveStudentLanguages(string $stringSave): void
+    {
+        $stmt = $this->pdo->prepare("INSERT INTO {$this->entityName} (student_id, languages_id) VALUES {$stringSave} ");
+        $stmt->execute();
+    }
+
+
+    public function update(Student_Languages $student_langueges)
+    {
+        $stmt = $this->pdo->prepare("SELECT * FROM {$this->entityName} WHERE id = :id");
+        $stmt->execute();
+        $student_languages = '';
+        foreach ($stmt as $row) {
+            $student_languages = new Student_Languages($row['firstname'], $row['lastname'], $row['class'], $row['name'], $row['id']);
+        }
+        $stmt = $this->pdo->prepare("UPDATE {$this->dbMain} SET firstname = :firstname WHERE id = :id");
+        $stmt->execute([
+            'firstname' => $student_langueges->firstname,
+            'languages' => $student_langueges->name,
+            'id' => $student_langueges->id,
+        ]);
+    }
+
+    public function delete(int $id)
+    {
+        $stmt = $this->pdo->prepare("DELETE s, i FROM student c JOIN student_languages i ON (s.id = st.student_languages) WHERE s.id = :id;");
+        $stmt->execute(['id' => $id]);
+    }
+}
+

+ 90 - 0
repository/StudentRepository.php

@@ -0,0 +1,90 @@
+<?php
+
+namespace App\Repository;
+
+use App\Model\Student;
+
+class StudentRepository extends RepositoryAbstract
+{
+
+    public function __construct()
+    {
+        parent::__construct();
+        $this->entityName = 'student';
+    }
+
+    /**
+     * @param $id
+     * @return null|Student
+     */
+    public function findById($id)
+    {
+        $stmt = $this->pdo->prepare("SELECT * FROM {$this->entityName} WHERE id = :id");
+        $stmt->execute(['id' => $id]);
+        foreach ($stmt as $row) {
+            return new Student($row['firstname'], $row['lastname'], $row['class'], $row['id']);
+        }
+        return null;
+    }
+
+    /**
+     * @return Student[] array
+     */
+    public function findAll(): array
+    {
+        $stmt = $this->pdo->prepare("SELECT * FROM {$this->entityName}");
+        $stmt->execute();
+        $students = [];
+        foreach ($stmt as $row) {
+            $students[] = new Student($row['firstname'], $row['lastname'], $row['class'], $row['id']);
+        }
+        return $students;
+    }
+
+    /**
+     * @param Student $student
+     */
+    public function save(Student $student)
+    {
+        $stmt = $this->pdo->prepare("INSERT INTO {$this->entityName} (firstname, lastname, class) VALUES(:firstName, :lastName, :class)");
+        $stmt->execute([
+            'firstName' => $student->firstName,
+            'lastName' => $student->lastName,
+            'class' => $student->class
+        ]);
+    }
+
+    /**
+     * @param Student $student
+     */
+    public function update(Student $student)
+    {
+        $stmt = $this->pdo->prepare("UPDATE {$this->entityName} SET firstname = :firstName, lastname = :lastName, class = :class WHERE id = :id");
+        $stmt->execute([
+            'id' => $student->id,
+            'firstName' => $student->firstName,
+            'lastName' => $student->lastName,
+            'class' => $student->class
+        ]);
+    }
+
+
+    /**
+     * @param int $id
+     */
+    public function delete(int $id)
+    {
+        $stmt = $this->pdo->prepare("DELETE FROM {$this->entityName} WHERE id = :id");
+        $stmt->execute(['id' => $id]);
+    }
+
+    /**
+     * @param
+     */
+
+    public function deleteAll()
+    {
+        $stmt = $this->pdo->prepare("TRUNCATE TABLE {$this->entityName}");
+        $stmt->execute();
+    }
+}