php / php/php-src

Extends PDO, sqliteCreateFunction() and magic method __call()

Đang mở
#7,803 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Extension: pdo (core) Extension: pdo_sqlite Feature
Ngôn ngữ chính
C
Star
40.4k
Fork
8.1k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

Description

The following code:

<?php
declare(strict_types=1);

class DBStatement extends PDOStatement
{
    protected $db;

    protected function __construct(PDO $db)
    {
        $this->db = $db;
    }
}
class db extends PDO {
    public function __construct(string $dsn, string $username = null, string $password = null, array $options = [])
    {
        $options += [
            self::ATTR_DEFAULT_FETCH_MODE => self::FETCH_ASSOC,
            self::ATTR_EMULATE_PREPARES   => false,
            self::ATTR_STRINGIFY_FETCHES  => false,
            self::ATTR_ERRMODE            => self::ERRMODE_EXCEPTION,
            self::ATTR_STATEMENT_CLASS    => [DBStatement::class, [$this]],
        ];
        parent::__construct($dsn, $username, $password, $options);
        $this->sqliteCreateFunction('CONCAT', function (...$args) {return \implode('', $args);});
    }

    public function __call(string $name, array $args) /* : mixed */
    {
        // Here is the loading of a class with rarely used methods that are different in implementation for different databases.
        // In a real project, an exception is displayed: 
        // PHP OTHER_ERROR: Method 'sqliteCreateFunction' not found in DB driver. in ...\app\Core\DB\Sqlite.php:[70]
    }
}

$db = new db('sqlite:datadb', '', '', []);
var_dump($db->query('SELECT CONCAT(\'123\', \'456\', \'789\')')->fetch());
exit;

Resulted in this output:

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1 no such function: CONCAT in C:\laragon\www\1.php:33 Stack trace: #0 C:\laragon\www\1.php(33): PDO->query('SELECT CONCAT('...') #1 {main} thrown in C:\laragon\www\1.php on line 33

But I expected this output instead:

array(1) { ["CONCAT('123', '456', '789')"]=> string(9) "123456789" } 

It turns out that the call to the sqliteCreateFunction() method is intercepted by __call()?

P.S. In my project, I extended PDO, extended PDOStatement and use _call () to call rare methods to change the structure of the database. Now I got such an incident when using SQLite.

PHP Version

PHP 8.1.1-Win32-vs16-x64, 8.1.0, 8.0.8, 7.4.5, 7.3.33

Operating System

Windows 7 x64

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Chạy reproducer PHP được cung cấp và bắt đầu với các entry point PDO subclass, sqliteCreateFunction() và __call() được mô tả trong issue. So sánh hàm CONCAT bị thiếu với kết quả truy vấn mong đợi; hoàn thành có nghĩa là hành vi method dispatch được giải quyết hoặc giới hạn của nó được xác lập rõ ràng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
php, sqlite
Lĩnh vực
databases
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.