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.
Related
I am creating my second Zend Framework project (using 1.12) and have set it up just like the first (uses 1.11), but for some reason I am having problems connecting to my database using my class extending Zend_Db_Table. I have my database configuration in an ini file like this:
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "myUsername"
resources.db.params.password = "myPassword"
resources.db.params.dbname = "myDbname"
resources.db.isDefaultTableAdapter = true
Then I have my table class:
class Application_Model_DbTable_TestData extends Zend_Db_Table_Abstract {
protected $_name = 'testdata'
}
and then in my controller I try:
$testdata = new Application_Model_DbTable_TestData();
and then when I go to the view for that controller I just get the message Application Error with no other info.
If I do the following in the controller:
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => 'localhost',
'username' => 'myUsername',
'password' => 'myPassword',
'dbname' => 'myDbname'
));
$test = $db->select()->from(array('test' => 'testdata'));
I can get the data. Any ideas why I can't do it with the table class? As far as I can tell it's exactly the same as my other set up which works just fine.
It turned out that the problem was where I had the database configuration data placed in my ini file. It was directly under [development : production] so development was the only thing that was getting it. Just one of those stupid oversights where I had to step away for a while and look again later to see it.
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 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".
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