Menu

#4 OO and database design with PHPMVC

closed
nobody
None
5
2005-10-10
2005-09-09
Anonymous
No

Just trying to work out how I should reference the database
if I am using objects. Currently the database needs to be
instantiated in the scope of the action server.

But if I'm calling a class and want to build an object I want
the object to populate all the data itself.

So how do I reference the database within the object I've
created when it performs various actions on the object ie
save, load etc.

All the examples I've seen using PHPMVC and ADODB
define the database within the scope of the action which
means I have to make the database a global variable and
then reference it within the object - not a pretty thing.

I'd like to simply call the methods on an object and let the
methods access a database reference.

Thanks
Andrew

Discussion

  • John Wildenauer

    John Wildenauer - 2005-09-18

    Logged In: YES
    user_id=662357

    Andrew,

    Do you mean something like this:

    Q: How to access a DataSource reference in my business classes
    
    A:
    Within our Action class \(or ActionForm\) we can retrieve a
    reference to the data source instance. Then we can create a
    new instance of our business class, passing the data source
    reference to the business object as a constructor parameter,
    or using a setter method.
    
    When out business class processing is complete, we can
    safely disconnect from the database. If we did not retrieve
    the data source
    as a reference \("=&"\) but just used a copy \("="\) and then
    disconnected from the database, we could not reconnect again
    during this request as the original datasource instance will
    think it is still connected to the database.
    
    class MyAction extends Action \{
    ...
    function execute\($mapping, $form, &$request, &$response\) \{
    
    //////////
    // Call our business logic from here
    
    // Retrieve a reference \("=&"\) to the datasource
    $db =& $this->actionServer->getDataSource\($dsKey\);
    
    // Using the constructor
    $myApp = new MyBusinessClass\($db\);
    
    // Or using the setter method
    $myApp->setDataSource\($db\);
    
    // Do some business logic processing
    $myApp->processSomething\(\);
    
    //////////
    // Cleanup
    // We can disconnect here if we access $db as a
    reference \("=&"\)
    $db->disconnect\(\);
    
    \}
    \}
    
    MyBusinessClass \{
    
    // The datasource reference
    var $dataSource = NULL; 
    
    // Set the data source reference using a constructor
    function BusinessClass\($db\) \{
    $this->dataSource = $ds;
    \}
    
    // Or set the data source reference using a setter method
    function setDataSource\($db\) \{
    $this->dataSource = $ds;
    \}
    
    function processSomething\(...\) \{
    $ds = $this->dataSource;
    ...
    \}
    \}
    

    References relating to php.MVC/Database usage with Pear::DB
    or ADOdb

    Pear::DB database access in php.MVC
    http://phpmvc.net/docs/guides/guidesIdx.php?doc=pear-db

    ADOdb Database Access in php.MVC
    http://phpmvc.net/docs/guides/guidesIdx.php?doc=adodb-db

    John

     
  • John Wildenauer

    John Wildenauer - 2005-10-10
    • status: open --> closed
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.