On 14/08/12 21:52, Jurian Sluiman wrote:
> 2012/8/14 Marco Pivetta <ocram...@gmail.com>
> 
>> This is something that cannot really be done with application resources...
>> You'd have to override the application resource itself to allow that.
> 
> I guess you might have more luck with php array configuration since you
> could store variables and use them at several places. Then the reuse of
> database credentials is not requiring duplicate values anymore.

Hope I haven't misunderstood the problem, but aren't you using the
registry to store the database credentials?  That might be worth a look...

If you're not doing that/tempted, how about instead just grabbing those
database credentials into a function and calling that?

// Database connection function
function connect_db() {

        // Include() the Zend config so we can grab the db vars
        $path = realpath($_SERVER['DOCUMENT_ROOT'].'/../library/');
        // we have already updated the include path...
        require_once 'library/Zend/Config/Ini.php';
        $db_config = new 
Zend_Config_Ini('../config/databases.ini','production');
        
    // Database Credentials
    $db_config_username     = $db_config->db->mydb->config->username;
    $db_config_password     = $db_config->db->mydb->config->password;;
    $db_config_dbname       = $db_config->db->mydb->config->dbname;
    $db_config_host         = $db_config->db->mydb->config->host;

    // use MySQL by default
    $cmsLink =
mysql_connect($db_config_host,$db_config_username,$db_config_password);

    if($cmsLink)
        $dbConn = mysql_select_db($db_config_dbname,$cmsLink);

    return $dbConn;

}

This example assumes a functions file is sitting outside of (above) the
docroot. You may need to adjust the paths as necessary.

The corresponding config file (config/databases.ini) would look
something like this:

[production]
;; My database
db.mydb.adapter            = "PDO_MYSQL"
db.mydb.config.host        = "localhost"
db.mydb.config.username    = "username"
db.mydb.config.password    = "password"
db.mydb.config.dbname      = "your-db"
db.mydb.config.port        =  3306


You could then include() the functions file in your code.  A call to
connect_db() function would either return the database object or MySQL
error.

Then you could keep your db credentials in the one ZF config file but
use them across the application.

Kind regards,
Steve

-- 
Steve Dowe

Warp Universal
http://warp2.me/sd

-- 
List: fw-general@lists.zend.com
Info: http://framework.zend.com/archives
Unsubscribe: fw-general-unsubscr...@lists.zend.com


Reply via email to