How to extend the DB \ SQL class ?

82 views
Skip to first unread message

xMinaGrean

unread,
Mar 10, 2019, 1:53:04 PM3/10/19
to Fat-Free Framework
Here is an example of how to expand the class using Mapper
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();

I want to avoid this construction:
$db=new DB\SQL(
    'mysql:host=localhost;port=3306;dbname=mysqldb',
    'admin',
    'p455w0rD'
);

How to expand the usual sql?

ved

unread,
Mar 11, 2019, 11:29:46 AM3/11/19
to Fat-Free Framework
Hi, 

I don't think you explained your issue very well. At least I didn't quite understand what you're trying to achieve with this:

I want to avoid this construction:
$db=new DB\SQL(
    'mysql:host=localhost;port=3306;dbname=mysqldb',
    'admin',
    'p455w0rD'
);


May I ask why would you want to avoid this? It's your database connection initialization and parameters.

But anyway, have you tried just simply extending DB\SQL instead of extending  DB\SQL\Mapper as in your example?

Cheers.
Message has been deleted

xMinaGrean

unread,
Mar 11, 2019, 11:20:11 PM3/11/19
to Fat-Free Framework
I have a lot of modules (classes).
Each module has its connection to the database.

$db=new DB\SQL(
 $f3
->get('devdb'),
 $f3
->get('devdbusername'),
 $f3
->get('devdbpassword'),
 array
( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)
);


In the controller, I call a few modules.
Each module creates a connection to the database.
On some hosting there is a limit to the connection.
mysql begins to swear that the connection limit is too high.
You can close every open connection.
I want to expand each module \ DB \ SQL
to avoid multiple connections.
Maybe I'm wrong.
Therefore, I want to check this connection method

сlass MyClass extends DB\SQL{


 
function __construct(){
 
 $db
= parent::construct();
 
 $res
= $db->exec("SELECT * FROM mytable");
 
 
}


}



is mapper nosql?

Paul Herring

unread,
Mar 12, 2019, 3:15:48 AM3/12/19
to xMinaGrean via Fat-Free Framework, Fat-Free Framework
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 omitted

Everywhere else, use:

$f3->get('DB')->exec('...');

Or if you don't directly have $f3:

Base::instance()->get('DB')->exec('...');

--
-- 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.


--
PJH

ved

unread,
Mar 12, 2019, 1:55:33 PM3/12/19
to Fat-Free Framework
What @PJH recommended should fix that issue.

Alternatively, if you wish to use class inheritance you can, for example, create a parent class where you assign the database connection to some property and then have your modules extend that class and use the db object property.

For example:

// 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");
   
}
}

Cheers

xMinaGrean

unread,
Mar 13, 2019, 10:18:21 PM3/13/19
to Fat-Free Framework
Thank you so much

среда, 13 марта 2019 г., 0:55:33 UTC+7 пользователь ved написал:

xMinaGrean

unread,
Mar 13, 2019, 10:19:01 PM3/13/19
to Fat-Free Framework
Thank you so much

вторник, 12 марта 2019 г., 14:15:48 UTC+7 пользователь PJH написал:
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 omitted

Everywhere else, use:

$f3->get('DB')->exec('...');

Or if you don't directly have $f3:

Base::instance()->get('DB')->exec('...');

Reply all
Reply to author
Forward
0 new messages