This is the script I'm trying to convert in to a standard application.ini zend_db connection:
require_once 'Zend/Db/Adapter/Db2.php';
$config = array( 'dbname' => '*LOCAL',
'username' => '',
'password' => '',
'os'=>'i5',
'driver_options'=> array("i5_commit" =>DB2_I5_TXN_READ_UNCOMMITTED,
"autocommit"=>DB2_AUTOCOMMIT_OFF,
"i5_lib"=>'WEBLIB'));
I don't know how to address the driver_options in the application.ini dot syntax. If it was just a regular connection string, I wouldn't have difficulty.
I would appreciate any help or direction.
i use this application.ini config:
resources.db.adapter = "Db2"
resources.db.params.dbname = "*LOCAL"
resources.db.params.ip = "192.168.1.100"
resources.db.params.os = "i5"
resources.db.params.persistent = true
resources.db.params.driver_options.autocommit = DB2_AUTOCOMMIT_ON
resources.db.params.driver_options.i5_naming = DB2_I5_NAMING_ON
resources.db.params.driver_options.i5_libl = "WEBLIB"
resources.db.isDefaultTableAdapter = true
Related
I was wondering if there anyway to move this configuration from the INI into the bootstrap.php in a way it would be the default globally. If so, how should I do it? I'm with zend 1.11.
thanks in advance.
;resources.mail.transport.type = smtp
;resources.mail.transport.host = "smtp.gmail.com"
;resources.mail.transport.auth = login
;resources.mail.transport.ssl = ssl
;resources.mail.transport.port = 465
;resources.mail.transport.username = email#example.com
;resources.mail.transport.password = pass
;resources.mail.transport.register = true ; True by default
;resources.mail.defaultFrom.email = email#example.com
;resources.mail.defaultFrom.name = "Foo"
;resources.mail.defaultReplyTo.email = email#example.com
;resources.mail.defaultReplyTo.name = "Foo"
[SOLUTION]
Actually it worked for me adding:
$config = array(
'ssl' => $transport_ssl,
'port' => $transport_port,
'auth' => $transport_auth,
'username' => $transport_username,
'password' => $transport_password,
'register' => $transport_register,
);
$transport = new \Zend_Mail_Transport_Smtp($transport_host, $config);
// var_dump($transport);exit;
\Zend_Mail::setDefaultTransport($transport);
when i want to connect database in zend framework with zend_Db, in each controller or model must write this code:
$params = array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test',
'charset' => 'utf8'
);
but when use Doctrine it's enough to write this code in application.ini
doctrine.dsn = "mysql://user:pass#localhost/dbase"
how i can use zend_Db with out set connection setting in each file?
open
application/configs/application.ini
add following lines
resources.db.adapter = PDO_MYSQL
resources.db.isDefaultAdapter = true
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password =
resources.db.params.dbname = foo
and then inside your Bootstrap.php
do
public function _initSetup()
{
$this->bootstrap('db');
}
and then whereever or whenever you need db instance simply do
$db = Zend_Db_Table::getDefaultAdapter();
there are two ways one by using your configuration.ini file
by this code :
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = ''
resources.db.params.dbname = dbname
resources.db.params.driver_options.1002 = "SET NAMES utf8;"
or by using hte adapter class
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
and dont put any function in your bootstrap.php file
I want to access to database "mysql", I read that we can define the db adapter and db configurations by writing these lines in application.ini file
resources.db.adapter = MYSQLI
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password =123456
resources.db.params.dbname = visits_db
I want to get the $db object in order to use in for executing sql statements
$db->insert("inspection_visits_tb",
$insepctionVisitData = array(
'day' => $visit->getDay(),
'date' => $visit->getDate(),
'target' => $visit->getTarget()
));
I want to get it from the application.ini file not like this
require_once 'Zend/Db.php';
$db = Zend_Db::factory("MYSQLI",
array(
"host" => "localhost",
"dbname" => "visits_db",
"username" => "root",
"password" => "123456")
);
because I want to define the db adapter in one place only. What should I do to get the $db object??
Set up the db resource in application.ini like you were. Add the isDefaultAdapter option.
resources.db.adapter = "MYSQLI"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "123456"
resources.db.params.dbname = "visits_db"
resources.db.isDefaultAdapter = true
Then to get the $db object...
In Bootstrap.php
protected function _initDb()
{
$this->bootstrap('db');
$db = $this->getResource('db');
Zend_Registry::set('db', $db);
}
From Somewhere else w/out using Registry
$db = Zend_Db_Table::getDefaultAdapter();
Using Zend_Db_Table
$table = new Zend_Db_Table('bugs');
$select = $table->select()...
See also ZF Quickstart, Zend_Db_Table, Zend_Db_Select for more examples.
i am new to zend framework i wana know why we are using this code for connection to database although we can use the below code also which is simple and not including class what is the advantages of including Zend_config( )
require_once 'Zend/Config.php';
$arrConfig = array(
'webhost'=>'localhost',
'appName'=>'My First Zend',
'database'=>array(
'dbhost'=>'localhost',
'dbname'=>'zend',
'dbuser'=>'root',
'dbpass'=>'admin'
)
);
$config = new Zend_Config($arrConfig);
$params = array('host' =>$config->database->dbhost,
'username' =>$config->database->dbuser,
'password' =>$config->database->dbpass,
'dbname' =>$config->database->dbname
);
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
$DB->setFetchMode(Zend_Db::FETCH_OBJ);
if i can do like this
include_once 'Zend/Db/Adapter/Pdo/Mysql.php';
$params = array('host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'zend'
);
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
$DB->setFetchMode(Zend_Db::FETCH_OBJ);
in the way you're using Zend_Config it's in fact doesn't help you a lot to have the settings in a config object.
Usually, in ZF applications, there's a separate application.ini file which contains all the settings:
$config = new Zend_Config_Ini('/path/to/config.ini',
'production');
Then it's convenient to separate environments (production & development for example) into different sections:
; Production site configuration data
[production]
database.adapter = pdo_mysql
database.params.host = db.example.com
database.params.username = dbuser
database.params.password = secret
database.params.dbname = dbname
; Development site configuration data inherits from production and
; overrides values as necessary
[development : production]
database.params.host = dev.example.com
database.params.username = devuser
database.params.password = devsecret
meaning loading the config with:
$config = new Zend_Config_Ini('/path/to/config.ini',
'development');
wil return the development config.
http://framework.zend.com/manual/en/zend.config.adapters.ini.html
I use select queries as by following code:
$params = array(
'username' => 'root',
'password' => '',
'dbname' => 'mst2');
$db = Zend_Db::factory('pdo_mysql', $params);
$select = $db->select()
->from(array('dc' => 'delivery_center'))
->join(array('r' => 'region'), 'dc.region_id = r.region_id');
$stmt = $select->query();
$result = $stmt->fetchAll();
Here $db is the credentials of the database that I am using.But I have specified the credentials in application.ini already by following lines:
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = ''
resources.db.params.dbname = mst2
Now logically I should not provide these credentials again.But I have to use select queries.So how $db should be initialized without proving database credentials again??Thanks in advance.
Db resource is available by default and is initialized automatically whilst bootstraping.
If you want to get database adapter in your application you can get it as plugin resource from bootstrap:
$resource = $bootstrap->getPluginResource('db');
$db = $resource->getDbAdapter();
If you do not have reference to bootstrap you always can retrieve it from FrontController:
$front = Zend_Controller_Front::getInstance();
$bootstrap = $front->getParam('bootstrap');
You can use
Zend_Db::factory($zend_config_object->resources->db)
but i think if you have specified this in application.ini, then zend create db object for you automaticlly and you can get it through
Zend_Register and key "db".