Reputation: 97
I'm trying to use PDO to pull data from a database and into the current object. Code:
class Voucher {
//Database and object have EXACTLY the same fields/properties/columns
protected $voucher_id;
protected $voucher_code;
protected $voucher_value;
protected $voucher_product_type;
protected $voucher_status;
// still in the class declaration
public function load_voucher($voucher_code){
global $conn; //load database connection info
$stmt = $conn->prepare('select * from cart_voucher_table where voucher_code = :voucher_code');
$stmt->execute(array('transaction_id' => $this_transaction_id));
I HAVE NO IDEA, NO PDO statement works
// Current object (which existed but was empty) now contains values from first row of returned data from db
}
}
//usage
$this_voucher = new voucher(); //creates new voucher object
$this_voucher->load_voucher($voucher_code); //loads data from database into the current voucher object.
I've searched for quite some time, with no result. All of the solutions I've seen either have the function outside of the class, or they return a new object, NOT the current one.
Upvotes: 0
Views: 3946
Reputation: 2869
Using PDO inside of a model/value object to populate the properties of the object is a terrible design decision. I don't say that to knock your code, but to encourage you to use a different technique. The pain you'll avoid in maintainability alone will be worth the effort to choose a new design. I have a suggestion after the solution.
SOLUTION
This issue has been dealt with here before: "PDO's FETCH_INTO $this class does not working". Using that technique should take care of your issue.
SUGGESTION
My suggestion is to go another route entirely. Consider creating a Voucher DAO that would look something like this:
class VoucherDao
{
private $db;
public function __construct(\PDO $db)
{
$this->db = $db;
}
public function findVoucher($voucher_code)
{
$stmt = $this->db->prepare('select * from cart_voucher_table where voucher_code = :voucher_code');
$stmt->bindValue(':voucher_code', $voucher_code);
$stmt->setFetchMode(\PDO::FETCH_CLASS, 'Voucher');
$stmt->execute();
return $stmt->fetch();
}
}
Rather than creating a new voucher and then loading it, you'd use the DAO to create the voucher already filled with data.
$dao = new VoucherDao($conn);
$voucher = $dao->findVoucher(12345);
Of course, since I don't have the entire voucher code, I can't test this. If I remember correctly, you'll need to create setters in your Voucher class to allow PDO::FETCH_CLASS
to work properly. Test both with and without if you give my suggestion a try.
Upvotes: 2
Reputation: 169
"The keys from input_parameters must match the ones declared in the SQL. Before PHP 5.2.0 this was silently ignored."
From: http://www.php.net/manual/de/pdostatement.execute.php
In your MySQL, when preparing the statement, you have the named parameter ":voucher_code" but the key in the array for execution is "transaction_id" - so this might be your problem I guess, as your wrote a comment on getting the an error message concerning the parameters.
Also, I don't see $stmt->fetch() getting called anywhere in your code...?
Upvotes: 0