class Vendor extends DB\SQL\Mapper {
// Instantiate mapper
function __construct(DB\SQL $db) {
// This is where the mapper and DB structure synchronization occurs
parent::__construct($db,'vendors');
}
// Specialized query
function listByCity() {
return $this->select('vendorID,name,city',null,array('order'=>'city DESC'));
/*
We could have done the same thing with plain vanilla SQL:
return $this->db->exec(
'SELECT vendorID,name,city FROM vendors '.
'ORDER BY city DESC;'
);
*/
}
}
$vendor=new Vendor;
$vendor->listByCity();
$db=new DB\SQL(
'mysql:host=localhost;port=3306;dbname=mysqldb',
'admin',
'p455w0rD'
);
I want to avoid this construction:$db=new DB\SQL( 'mysql:host=localhost;port=3306;dbname=mysqldb', 'admin', 'p455w0rD' );
$db=new DB\SQL(
$f3->get('devdb'),
$f3->get('devdbusername'),
$f3->get('devdbpassword'),
array( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)
);
сlass MyClass extends DB\SQL{
function __construct(){
$db= parent::construct();
$res = $db->exec("SELECT * FROM mytable");
}
}
--
-- You've received this message because you are subscribed to the Google Groups group. To post to this group, send an email to f3-fra...@googlegroups.com. To unsubscribe from this group, send an email to f3-framework...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/f3-framework?hl=en
---
You received this message because you are subscribed to the Google Groups "Fat-Free Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to f3-framework...@googlegroups.com.
To post to this group, send email to f3-fra...@googlegroups.com.
Visit this group at https://groups.google.com/group/f3-framework.
To view this discussion on the web visit https://groups.google.com/d/msgid/f3-framework/2960fb54-4fee-450d-aba2-581cd45a86f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
// parent class
сlass Parent {
protected $db;
public function __construct(){
$this->db= new DB\SQL(
$f3->get('devdb'),
$f3->get('devdbusername'),
$f3->get('devdbpassword'),
array( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)
);
}
}
// your modules then go something like this
class Module extends Parent {
public function __construct(){
parent::construct()
}
public function someMethod() {
// you can now get the db object with $this->db.
$res = $this->db->exec("SELECT * FROM mytable");
}
}
If you just want to have the connection in one place, then in the file that calls $f3->run() (and before it) have:$f3->set('DB', $db=new DB\SQL(
$f3->get('devdb'),
$f3->get('devdbusername'),
$f3->get('devdbpassword'),
array( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION))); // error checking omittedEverywhere else, use:$f3->get('DB')->exec('...');Or if you don't directly have $f3:Base::instance()->get('DB')->exec('...');