如何将 PDO::FETCH_CLASS 与 __set() 方法一起使用



我想在从数据库中获取数据后立即创建一个对象。

为此,我正在使用 PDO::FETCH_CLASS 和我放入特征的重写的 __set(( 方法。

但是,我不知道为什么我有一个错误提到:"致命错误:未捕获的参数计数错误:参数太少,无法运行 Banner::__construct((,0 已通过,在第 34 行的 D:\Logiciels\wamp64\www\projet-4\writer-blog\entity\Banner.php中预期正好为 1">

这是我的横幅类(省略了二传手和getter(:

<?php
require_once(__DIR__.'Set.php');
/**
 * This class represents a banner that will be displayed on the homepage of the FrontEnd
 */
class Banner 
{
    use Set;
    protected   $errors=[],
                $id,
                $displayOrder,
                $title,
                $caption,
                $image,
                $buttonTitle,
                $buttonLink,
                $creationDate,
                $modificationDate;
    /**
     * Constants for errors management during the execution of the methods
     */
    const INCORRECT_IMAGE_LINK = 1;
    const INCORRECT_TITLE = 2;
    const INCORRECT_CAPTION = 3;
    /**
     * Class constructor which assign values that have been passed in parameters to matching attributes 
     *
     * @param array $donnees The values to allocate
     * @return void
     */
    public function __construct(array $data)
    {
        $this->hydrate($data);
    }
    /**
     * Method that allocates specified valued to the matching attributes
     *
     * @param array $donnees The values to allocate
     * @return void
     */
    public function hydrate($data)
    {
      foreach ($data as $key => $value)
      {
        $method = 'set'.ucfirst($key);
        if (method_exists($this, $method))
        {
          $this->$method($value);
        }
      }
    }

我的神奇方法__set((具有以下特征:

<?php
trait Set {
    public function __set($property, $value)
    {
        $explodedProperty = explode("_", $property);
        for ($i = 1 ; $i < count($explodedProperty); $i++) {
            ucfirst($explodedProperty[$i]);
        }
        $rightPropertyName = ucfirst(implode($explodedProperty));
        if (property_exists(__CLASS__, $rightPropertyName))
        {
            $this->$rightPropertyName = $value;
        }
        else
        {
            throw new Exception('La propriété n'a pas pu se voir assigner une valeur car elle n'existe pas!');
        }
    }
}

这是我的横幅管理器的开头,我在其中获取数据:

<?php 
require_once("model/Manager.php");
require_once("entity/Banner.php");
class BannerManager extends Manager
{
    public function getBanners()
    {
        $db = $this->dbConnect();
        $req = $db->query('SELECT id, title, caption, image, button_title, button_link, 
        DATE_FORMAT(creation_date, '%d/%m/%Y à %Hh%imin%ss') AS creation_date_fr, DATE_FORMAT(modification_date, '%d/%m/%Y à %Hh%imin%ss') AS modification_date_fr 
        FROM banners ORDER BY display_order LIMIT 5'); 
        $req->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Banner');
        $banners = $req->fetchAll();
        return $banners;
    }

您的实际问题是如何将PDO::FETCH_CLASS与需要构造函数参数的类一起使用,因为这就是错误消息所说的。

答案是setFetchMode的第三个参数。

$req->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Banner',[[]]);

应该做这个技巧,为构造函数提供一个空数组,从而使您的代码到达实际调用__set()的位置。

相关内容

最新更新