۲ هادی قاسمی
افزودن prepare به متد ها
امیر صالحی حل شده توسط امیر صالحی

سلام 

 

منظور از اضافه کردن prepare به متد‌ها تقریبا یه همچین چیزی میشه ؟ 

# Read
    public function find ($id) : object
    {
        $sql = $this->connection-> get($this->table,'*', [$this->primary_key => $id]);
        $result = $this->connection->pdo->prepare($sql);
        return (object)$result ;
    }

 

و برای متد‌های دیگه - چون  string برنمیگردونن و متد prepare باید string دریافت کنه - اول تبدیل میکنیم به استرینگ بعد میدیم به متد prepare ؟

درود

این متد تو دل خودش queryها رو prepare میکنه لازم نیست همچین کار کنید

امیر صالحی ۰۸ شهریور ۱۴۰۰، ۱۷:۲۳

این متد خود exec درون همین پکیجیه که دارید استفاده میکنید

public function exec(string $statement, array $map = [], callable $callback = null): ?PDOStatement
    {
        $this->statement = null;
        $this->errorInfo = null;
        $this->error = null;
        if ($this->testMode) {
            $this->queryString = $this->generate($statement, $map);
            return null;
        }
        if ($this->debugMode) {
            if ($this->debugLogging) {
                $this->debugLogs[] = $this->generate($statement, $map);
                return null;
            }
            echo $this->generate($statement, $map);
            $this->debugMode = false;
            return null;
        }
        if ($this->logging) {
            $this->logs[] = [$statement, $map];
        } else {
            $this->logs = [[$statement, $map]];
        }
        $statement = $this->pdo->prepare($statement);
        $errorInfo = $this->pdo->errorInfo();
        if ($errorInfo[0] !== '00000') {
            $this->errorInfo = $errorInfo;
            $this->error = $errorInfo[2];
            return null;
        }
        foreach ($map as $key => $value) {
            $statement->bindValue($key, $value[0], $value[1]);
        }
        if (is_callable($callback)) {
            $this->pdo->beginTransaction();
            $callback($statement);
            $execute = $statement->execute();
            $this->pdo->commit();
        } else {
            $execute = $statement->execute();
        }
        $errorInfo = $statement->errorInfo();
        if ($errorInfo[0] !== '00000') {
            $this->errorInfo = $errorInfo;
            $this->error = $errorInfo[2];
            return null;
        }
        if ($execute) {
            $this->statement = $statement;
        }
        return $statement;
    }

داخلش prepare استفاده شده و نیازی نیست خودتون prepare کنید 

 

بهترین پاسخ
امیر صالحی ۰۹ شهریور ۱۴۰۰، ۱۵:۳۴