امیر حسین
2 سال پیش توسط امیر حسین مطرح شد
0 پاسخ

بررسی متد crud

سلام، خسته نباشید
این کدی که من برای عملیات crud نوشتم بنظرتون میتونه کاربری باشه ؟

class database
{
    private $dsn = "mysql:host=localhost";
    private $username = 'root';
    private $password = 'root';
    private $options = [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_CASE => PDO::CASE_NATURAL,
        PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING
    ];
    public $conn;

    public function __construct()
    {
        try {

            $this->conn = new PDO($this->dsn, $this->username, $this->password, $this->options);
            $this->conn->query("
            create database IF NOT EXISTS php_project;
            use php_project;
            create table IF NOT EXISTS users (
            id int not null auto_increment,
            first_name varchar(150) not null,
            last_name varchar(150) not null,
            email varchar(250) not null,
            password varchar(350) not null,
            is_admin boolean default 0,
            created_at timestamp default current_timestamp,
            updated_at timestamp default current_timestamp on update current_timestamp,
            primary key (id),
            unique (email)
            );
            create table if not exists category (
            id int not null auto_increment,
            name varchar(255) not null,
            created_at timestamp default current_timestamp,
            updated_at timestamp default current_timestamp on update current_timestamp,
            primary key (id),
            unique (name)
            );
            create table if not exists post (
            id int not null auto_increment,
            cat_id int not null,
            user_id int not null,
            title varchar(400) not null,
            description varchar(700),
            body varchar(10000) not null,
            image varchar(300),
            created_at timestamp default current_timestamp,
            updated_at timestamp default current_timestamp on update current_timestamp,
            foreign key fk_post_cat_id (cat_id) references category(id) on delete cascade on update cascade,
            foreign key fk_post_user_id (user_id) references users(id) on delete cascade on update cascade,
            primary key (id)
            );
            ");

        } catch (Exception $e) {
            echo $e->getMessage();
        }

    }

    public function insert(string $table, array $feilds , $values) : bool
    {
        $count = count($feilds);
        $prepare_repeat = chop(str_repeat('?,',$count),',');
        $feilds = implode(',',$feilds);
        $stmt = $this->conn->prepare("insert into $table ($feilds) values ($prepare_repeat)");
        $result = $stmt->execute($values);
        return $result;
    }

    public function update(string $table, array $feilds, array $values, string $where_conditional,array $where_value) : bool
    {
        $feilds = array_map(fn($item)=>"$item = ? ,",$feilds);
        $feilds = chop(implode('',$feilds),',');
        $feilds = chop($feilds,',');
        $where_value = array_map(fn($item)=>"'$item'",$where_value);
        $where_value = implode(',',$where_value);
        $stmt = $this->conn->prepare("update $table set $feilds where $where_conditional = $where_value");
        $result = $stmt->execute($values);
        return $result;
    }

    public function delete(string $table, string $where_conditional,array $where_value) : bool
    {
        $where_value = array_map(fn($item)=>"'$item'",$where_value);
        $where_value = implode(',',$where_value);
        $result = $this->conn->query("delete from $table where $where_conditional = $where_value");
        return $result;
    }

    public function select(string $table, array $feilds, string $where_conditional = null, array $where_value = null)
    {
        $feilds = implode(',',$feilds);
        if ($where_value === null and $where_conditional === null){
        $result = $this->conn->query("select $feilds from $table");
        }
        if ($where_conditional !== null and $where_value !== null){
            $where_value = array_map(fn($item)=>"'$item'",$where_value);
            $where_value = implode(',',$where_value);
            $result = $this->conn->query("select $feilds from $table where $where_conditional = $where_value");
        }
        if (isset($result)){
        return $result->fetchAll(PDO::FETCH_OBJ);
        }
    }

}

ثبت پرسش جدید

به همدیگه کمک کنیم

به امیر حسین کمک کنید تا مشکل خودش را حل کند؛ این‌طور می‌توانیم با هم پیشرفت کنیم.

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام