Jump to content

Recommended Posts

Rather than create a db connection for each model I imagine there is an easy way to bootstrap one persistant connection across all models that need it.  I've already registered the db resource in the ini file.  that portion of the file looks like this:

 

resources.db.adapter = "PDO_MYSQL"

resources.db.isdefaulttableadapter = true

resources.db.params.dbname = "dbname"

resources.db.params.username = "username"

resources.db.params.password = "secure"

resources.db.params.hostname = "localhost"

resources.db.params.charset = "UTF8"

 

So, I'm hoping this will automatically set up the db connection and if so how do I access it from the front controller?

 

Any thoughts on this specifically would be appreciated.  I'd also be very interested to see different ways to solve this problem.  Thank you!

Link to comment
https://forums.phpfreaks.com/topic/186716-global-connection-for-all-models/
Share on other sites

It looks like I found most of what I'm looking for.  It turns out that when you extend Zend_Db_Table_Abstract you also inherit the adapter that is automatically created in the master .ini file on creation (see original post).  This means that I don't need to write a single line of additional code in order to access the Databse.  Pretty sweet. 

 

So, here's an example.  Let's say that I have a model that extends Zend_Db_Table_Abstract like so:

 

 

[pre]'application/models/Tablename.php'

class Tablename extends Zend_Db_Table_Abstract

{

        /*

            Assuming the table name matches 'Tablename' (the name of the class)

            and the table id is 'id' you don't need to do anything else in order to

            take advantage of the basic methods built in to Zend_Db_Table_Abstract.  This is pretty

            sweet.

        */

// Otherwise you can change set the table name and the primary id like so:

        protected $_name = 'theActualTableName';

        protected $_id = 'tablename_ID';

}[/pre]

 

 

So, I just store my models like this in the models folder.  Currently there is no great way to auto load these models.  I have my own little way of doing things but for now I'll just make sure that my  'models' folder is in my include_path and include the class directly before calling it.

 

So, when I'm ready to call the functionality of working with that table I just include the file and create the class.  Let's say I want to work with that table in an action controller.  I imagine I can also do this in an action helper or view helper, even the view or another model.  It's probably best to keep the controllers slim by accessing this in another model but for now I'll just access it in the index action controller like so:

 

 

[pre]class IndexController extends Zend_Controller_Action

{

    public function indexAction()

    {

 

        include_once 'Tablename.php';

        $tableName = new Tablename();

       

        // The result is stored in a multidimensional associative array.

        $results = $tableName->fetchAll(); // 'fetchAll()' is one of several inherited methods, awesome!

       

        // Access the data and send to view.

        foreach($results as $row){

        $toView .=  $row['fieldKey'].'<br />';

        }

       

        $this->view->foo = $toView; // Send the information to be displayed by the view.

    }

 

}[/pre]

 

 

For more on the specifics of Zend_Db_Table_Abstract go to: http://framework.zend.com/manual/en/zend.db.table.html.

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.