سلام
منظور از اضافه کردن 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 ؟
این متد خود 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 کنید
درود
این متد تو دل خودش queryها رو prepare میکنه لازم نیست همچین کار کنید