Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.5 kB
1
Indexable
Never
<?php
    namespace controller;

    include_once (dirname(__DIR__) . "/Autoload.php");    

    use PDO;

    use api\Autoload;

    use controller\Controller;
    
    use database\Connection;

    final class CompanyController extends Controller
    {
        public function __construct()
        {   
            parent::__construct((new Connection()), "company");
        }

        public function create(object $model)
        {}

        public function read(?int $id = null) : array
        {
            $q = "SELECT * FROM `{$this->getTable()}`" . (isset($id) ? " WHERE `id` = :id" : "");

            $stmt = $this->getConnection()->prepare($q);

            if(isset($id))
            {
                $stmt->bindValue(":id", $id);

                $stmt->execute();

                return $stmt->fetch(PDO::FETCH_ASSOC);
            }   
            
            $stmt->execute();

            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        }        

        public function readByCode(string $code)
        {
            $q = "SELECT `id` FROM `company` WHERE `code` = :c";

            $stmt = $this->getConnection()->prepare($q);

            $stmt->bindValue(":c", $code);

            $stmt->execute();

            $fetch = $stmt->fetch(PDO::FETCH_ASSOC);

            return $this->read($fetch["id"]);
        }

        public function readByUserId(int $user_id)
        {
            $q = "SELECT `company_id` AS `id` FROM `company_user_id` WHERE `user_id` = :user";

            $stmt = $this->getConnection()->prepare($q);

            $stmt->bindValue(":user", $user_id);

            $stmt->execute();

            $fetch = $stmt->fetch(PDO::FETCH_ASSOC);

            return $this->read($fetch["id"]);
        }

        public function readUsers(int $id)
        {
            $q = "SELECT `user_id` AS `id` FROM `company_user_id` WHERE `company_id` = :c";

            $stmt = $this->getConnection()->prepare($q);

            $stmt->bindValue(":c", $id);

            $stmt->execute();

            $fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);

            $result = [];

            foreach($fetch as $row)
            {
                $result[] = $row["id"];
            }

            return $result;
        }

        public function update(int $id, object $model)
        {}

        public function delete(int $id)
        {}        
    }

    Autoload::unload(__FILE__);
?>