Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

php - PDO using PDO::FETCH_PROPS_LATE and __construct() call?

I'm trying to create a new instance of Setting object calling __construct() method with PHP PDO and constrain PDO::FETCH_PROPS_LATE. Unfortunatly i'm getting this warning (and binding doesn't work).

How can pass column values to the constructor method?

Warning: Missing argument 1 for Setting::__construct() in pdo.php.

Notice: Undefined variable: key in pdo.php.

class Setting
{

    protected $key, $value, $displayable;

    public function __construct($key, $value = null, $displayable = 1)
    {
        $this->key         = $key;
        $this->value       = $value;
        $this->displayable = $displayable > 0;
    }

}

while($mashup = current($mashups))
{
    $stmt = $dbh->prepare('SELECT `key`, value, displayable
        FROM setting WHERE mashup_id = :id');

    $stmt->bindParam(':id', $mashup->id, PDO::PARAM_INT);
    $stmt->execute();

    $settings = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
       'Setting');
}

$stmt->closeCursor();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a non defaulted parameter $key in your constructor:

public function __construct($key, $value = null, $displayable = 1)

So, when you are doing this:

$settings = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,'Setting');

Error: warning: Missing argument 1 for Setting::__construct() in pdo.php is thrown only for parameter $key because it is not defaulted.

The correct use of fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,... is like this:

$variable = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
                           'classname', 
                            <array of parameter names(in order) used in constructor>);

So, in your case:

$variable = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
                            'Setting', 
                             array('key', 'value', 'displayable');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...