简体   繁体   English

PDO :: FETCH_CLASS参数

[英]PDO::FETCH_CLASS Arguments

It's possible to create another instance to send as parameters on the current instance in PDO::fetchAll()? 是否可以在PDO :: fetchAll()中创建另一个实例作为当前实例上的参数发送?

if \\PDO::FETCH_PROPS_LATE is set it will FIRSTLY call the __construct and SECONDLY set the properties. 如果设置了\\ PDO :: FETCH_PROPS_LATE,它将首先调用__construct并第二次设置属性。 so the values you have passed to the __construct via the array will be overwritten. 因此您通过数组传递给__construct的值将被覆盖。 But, when it overwritte the values, it doesn't understand the Voucher instance. 但是,当它覆盖值时,它不理解Voucher实例。

It is better illustrate 更好地说明
eg 例如

<?php
class Voucher {
    private $CD_ID, $CD_VOUCHER, $NR_EXPIRATION, $DT_VOUCHER, $IE_STATUS;
    public function __construct($CD_ID, $CD_VOUCHER, $NR_EXPIRATION, $DT_VOUCHER, $IE_STATUS) {
        $this->CD_ID = $CD_ID;
        $this->CD_VOUCHER = $CD_VOUCHER;
        $this->NR_EXPIRATION = $NR_EXPIRATION;
        $this->DT_VOUCHER = $DT_VOUCHER;
        $this->IE_STATUS = $IE_STATUS;
    }
}
class Authentication {
    private $CD_AUTH, $DT_AUTH, Voucher $CD_VOUCHER, $CD_IP, $CD_MAC, $CD_LOG;
    public function __construct($CD_AUTH, $DT_AUTH, Voucher $CD_VOUCHER, $CD_IP, $CD_MAC, $CD_LOG) {
        $this->CD_AUTH = $CD_AUTH;
        $this->DT_AUTH = $DT_AUTH;
        $this->CD_VOUCHER = $CD_VOUCHER;
        $this->CD_IP = $CD_IP;
        $this->CD_MAC = $CD_MAC;
        $this->CD_LOG = $CD_LOG;
    }
}
public function getAuthentications() {
    try {
        $sth = $this->db->prepare("SELECT `TB_AUTENTICACAO`.`CD_AUTH`, `TB_AUTENTICACAO`.`DT_AUTH`, `TB_VOUCHER`.`CD_ID`, `TB_VOUCHER`.`CD_VOUCHER`, `TB_VOUCHER`.`NR_EXPIRATION`, `TB_VOUCHER`.`DT_VOUCHER`, `TB_VOUCHER`.`IE_STATUS`, `TB_AUTENTICACAO`.`CD_IP`, `TB_AUTENTICACAO`.`CD_MAC`, `TB_AUTENTICACAO`.`CD_LOG` FROM `TB_AUTENTICACAO` INNER JOIN `TB_VOUCHER` ON `TB_VOUCHER`.`CD_ID` = `TB_AUTENTICACAO`.`CD_VOUCHER`;");
        $sth->execute();
        return $sth->fetchAll(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, "Models\\Authentication", array("TB_AUTENTICACAO.CD_AUTH", "TB_AUTENTICACAO.DT_AUTH", new \Models\Voucher("TB_VOUCHER.CD_ID", "TB_VOUCHER.CD_VOUCHER", "TB_VOUCHER.NR_EXPIRATION", "TB_VOUCHER.DT_VOUCHER", "TB_VOUCHER.IE_STATUS"), "TB_AUTENTICACAO.CD_IP", "TB_AUTENTICACAO.CD_MAC", "TB_AUTENTICACAO.CD_LOG"));
        } catch (Exception $exc) {
        die($exc->getMessage());
    }
}

The result must be: 结果必须是:
eg 例如

Models\Authentication Object (
    [CD_AUTH:Models\Authentication:private] => 2
    [DT_AUTH:Models\Authentication:private] => 2016-03-22 10:44:00
    [CD_VOUCHER:Models\Authentication:private] => Models\Voucher Object (
            [CD_ID:Models\Voucher:private] => 1
            [CD_VOUCHER:Models\Voucher:private] => xYgPB5
            [NR_EXPIRATION:Models\Voucher:private] => 720
            [DT_VOUCHER:Models\Voucher:private] => 2016-03-18 17:00:00
            [IE_STATUS:Models\Voucher:private] => 0
    )
    [CD_IP:Models\Authentication:private] => 10.10.10.10
    [CD_MAC:Models\Authentication:private] => abc
    [CD_LOG:Models\Authentication:private] => 1
)

but I get: 但我得到:
eg 例如

Models\Authentication Object (
    [CD_AUTH:Models\Authentication:private] => 2
    [DT_AUTH:Models\Authentication:private] => 2016-03-22 10:44:00
    [CD_VOUCHER:Models\Authentication:private] => xYgPB5
    [CD_IP:Models\Authentication:private] => 10.10.10.10
    [CD_MAC:Models\Authentication:private] => abc
    [CD_LOG:Models\Authentication:private] => 1
    [CD_ID] => 1
    [NR_EXPIRATION] => 720
    [DT_VOUCHER] => 2016-03-18 17:00:00
    [IE_STATUS] => 0
)

I would just keep it simple and define the variable as a class in the class itself. 我只是保持简单,并将变量定义为类本身中的一个类。 However, I am not sure why you have this issue, perhaps its because you're not attaching a variable to the new class? 但是,我不确定为什么会有这个问题,也许是因为您没有将变量附加到新类上?

If you're using a PHP 5.6+ you can make use of variable-length argument lists . 如果您使用的是PHP 5.6+,则可以使用变长参数列表 Seeing your code, I think it will boost read-ability by a-lot but the downside is that you need to make sure the correct number of variables are parsed into the constructor. 看到您的代码,我认为它将大量提高可读性,但缺点是您需要确保将正确数量的变量解析到构造函数中。

class Authentication {
    private $CD_AUTH, $DT_AUTH, $CD_VOUCHER, $CD_IP, $CD_MAC, $CD_LOG;
    public function __construct($CD_AUTH, $DT_AUTH, array $CD_VOUCHER, $CD_IP, $CD_MAC, $CD_LOG) {
        ...
        $this->CD_VOUCHER = new \Models\Voucher(...$CD_VOUCHER);
        ...
        # or go anonymous in php 7
        $this->CD_VOUCHER = new class(...$CD_VOUCHER){
          function __construct($CD_ID, $CD_VOUCHER, $NR_EXPIRATION, $DT_VOUCHER, $IE_STATUS){
            ...
          }
        }
    }
}

public function getAuthentications() {
    try {
        $sth = $this->db->prepare("...");
        $sth->execute();
        return $sth->fetchAll(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, "Models\\Authentication", array("TB_AUTENTICACAO.CD_AUTH", "TB_AUTENTICACAO.DT_AUTH", ["TB_VOUCHER.CD_ID", "TB_VOUCHER.CD_VOUCHER", "TB_VOUCHER.NR_EXPIRATION", "TB_VOUCHER.DT_VOUCHER", "TB_VOUCHER.IE_STATUS"], "TB_AUTENTICACAO.CD_IP", "TB_AUTENTICACAO.CD_MAC", "TB_AUTENTICACAO.CD_LOG"));
        } catch (Exception $exc) {
        die($exc->getMessage());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM