I have these values in my application.ini
[production]
; Database;
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "user1"
resources.db.params.password = "password1"
resources.db.params.dbname = "projects__01"
;Names
website.settings.websiteName = "My website 1"
website.settings.websiteUrl = "http://www.mydomain1.com"
website.settings.title = "mydomain.com - mydomain"
website.settings.titleSeperator = " - "
[staging : production]
; Database;
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "user2"
resources.db.params.password = "password2"
resources.db.params.dbname = "projects__02"
;Exceptions
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
;Title and url
website.settings.websiteName = "My website 2"
website.settings.websiteUrl = "http://www.mydomain2.com"
[development : staging]
;Database
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "user3"
resources.db.params.password = "password3"
resources.db.params.dbname = "projects__03"
;Title and url
website.settings.websiteName = "My website 3"
website.settings.websiteUrl = "http://www.mydomain3.com"
The problem is all database and exception values work properly, meaning that they are inheriting properly as they are supposed to
But the values I have set for Title and url do not inherit properly, only the first defined ones are used.
Why is this? is this by design? are only predefined/standard environment values such as database and exceptions are inherited?
Or am I making a mistake somewhere?
Okay, from your comment it sounds like you are creating a new Zend_Config object in the bootstrap, putting this in the registry, and it's this that's not returning what you'd expect. If this is the case I'm guessing that you've omitted the second parameter on the config object, so you have something like this:
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini');
but what you should have is more like this:
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);
the second param tells it what section of the config file to use, without that it will always get the same value.
However, you don't actually need to re-parse the config file since Zend Application has done this already. Instead you can access the options from the bootstrap class and use these to create an object (or just store the options in their existing array format):
protected function _initConfig()
{
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
return $config;
}
and this should work as you expect.
If my guess was wrong, please can you edit your question to include the relevant part of your bootstrap where the config object is created and stored in the registry.
Related
Following is my project structure,
In the bootstrap file, am loading modules like this
protected function _initAutoload() {
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace'=>'',
'basepath'=>APPLICATION_PATH
)
);
return $autoloader;
}
When I try to access controller, following error is thrown,
Fatal error: Uncaught exception 'Zend_Loader_Exception' with message 'Resource loader requires both a namespace and a base path for initialization' in
Any idea on this?
APPLICATION.INI
[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view.doctype = "XHTML1_STRICT"
resources.db.adapter = PDO_MYSQL;
resources.db.params.host = localhost;
resources.db.params.username = root2;
resources.db.params.password = password;
resources.db.params.dbname = zfalbums;
settings.publicFoderPathRelativeToApplicationPath = "../public_html";
settings.skin.name = "default";
settings.cache.enable = false;
Thanks
The namespace should be the name of the module. As you don't show any modules listed, you may not even need the module autoloader. However, try "default" as the namespace, though you may need to prefix everything with this (i.e. class Default_IndexController extends Zend_Controller_Action).
First: if you are using a reasonably current version of ZF this is redundant code. Comment it out and things should work.
Second: the only module you are showing in your structure would be 'default'.
It looks like you might be working with Rob Allens ZF 1.x tutorial, if so make sure you have the current version.
To enable modules check this out (from Rob Allen)
[EDIT]
To fix your database issue make sure you have at least these lines in your application.ini:
;Database Settings
;*****************
resources.db.adapter = "pdo_Mysql" //your database adapter
resources.db.params.username = "your_username"
resources.db.params.password = "your_password"
resources.db.params.dbname = "your_db_name"
remember this database needs to exist before you try to connect to it (or you could create it with a script). PHPmyadmin works well for easily managing mysql.
Your getting following error
Fatal error: Uncaught exception 'Zend_Loader_Exception' with message 'Resource loader requires both a namespace and a base path for initialization' in
Because in your bootstrap you've loading base path as 'basepath'=>APPLICATION_PATH this should to be 'basePath' => APPLICATION_PATH
I had the previous version of Bisna (before July 22nd), with Doctrine 2.0 working good, according to the instructions in http://www.zendcasts.com/unit-testing-doctrine-2-entities/2011/02/.
Yesterday I decided to upgrade to Doctrine 2.1, and had to upgrade Bisna as well, as it was incompatible with Doctrine 2.1.
So I deleted the entire Doctrine and Bisna directories from my library directory, brought in the new version of each of them, added the autoload init method to the bootstrap, and updated my application.ini file with the relevant changes.
However, when I try to access the application (which worked before fine with Doctrine 2.0 and the previous version of Bisna), I get the following exception:
Notice: Undefined index: annotationRegistry in /home/doron/workspace/myapp/library/Bisna/Doctrine/Container.php on line 569
Advanced information:
That line (#569) is:
$this->startAnnotationRegistry($config['annotationRegistry']);
When I print the contents of $config, I get the following:
array
0 =>
array
'annotationRegistry' =>
array
'annotationFiles' =>
array
0 => string '/home/doron/workspace/myapp/application/../library/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php' (length=103)
'adapterClass' => string 'Doctrine\ORM\Mapping\Driver\AnnotationDriver' (length=44)
'mappingNamespace' => string 'MyApp\Entity' (length=13)
'mappingDirs' =>
array
0 => string '/home/doron/workspace/myapp/application/../library/MyApp/Entity' (length=65)
'annotationReaderClass' => string 'Doctrine\Common\Annotations\AnnotationReader' (length=44)
'annotationReaderCache' => string 'default' (length=7)
So I can see that the annotationRegistry property is inside an array, where the code expects it to not be inside an array.
But this is not the end, as I get the following exception as well (below the previous exception):
Notice: Undefined index: drivers in /home/doron/workspace/myapp/library/Bisna/Doctrine/Container.php on line 571
So I guess something is not configured properly, or there's a bug with Bisna.
This is my application.ini:
[production]
; --------------------------
; PHP Specific Configuration
; --------------------------
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpSettings.date.timezone = "EST"
includePaths.library = APPLICATION_PATH "/../library"
includePaths.resource = APPLICATION "/../library/Bisna/Application/Resource"
; ----------------------------------------
; Zend Framework Application Configuration
; ----------------------------------------
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
autoloaderNamespaces[] = "MyApp"
autoloaderNamespaces[] = "Bisna"
autoloaderNamespaces[] = "Symfony"
autoloaderNamespaces[] = "Doctrine"
pluginPaths.Bisna\Application\Resource\ = "Bisna/Application/Resource"
; ------------------------------
; Front Controller Configuration
; ------------------------------
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.view[] =
resources.view.encoding = "UTF-8"
resources.view.doctype = "XHTML1_STRICT"
resources.view.contentType = "text/html; charset=UTF-8"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
; ------------------------------------------------------------------------------
; Doctrine Class Loader Configuration
; ------------------------------------------------------------------------------
resources.doctrine.classLoader.loaderClass = "Doctrine\Common\ClassLoader"
resources.doctrine.classLoader.loaderFile = APPLICATION_PATH "/../library/Doctrine/Common/ClassLoader.php"
resources.doctrine.classLoader.loaders.doctrine_common.namespace = "Doctrine\Common"
resources.doctrine.classLoader.loaders.doctrine_common.includePath = APPLICATION_PATH "/../library"
resources.doctrine.classLoader.loaders.doctrine_dbal.namespace = "Doctrine\DBAL"
resources.doctrine.classLoader.loaders.doctrine_dbal.includePath = APPLICATION_PATH "/../library"
resources.doctrine.classLoader.loaders.doctrine_orm.namespace = "Doctrine\ORM"
resources.doctrine.classLoader.loaders.doctrine_orm.includePath = APPLICATION_PATH "/../library"
resources.doctrine.classLoader.loaders.symfony_console.namespace = "Symfony\Component\Console"
resources.doctrine.classLoader.loaders.symfony_console.includePath = APPLICATION_PATH "/../library"
resources.doctrine.classLoader.loaders.symfony_yaml.namespace = "Symfony\Component\Yaml"
resources.doctrine.classLoader.loaders.symfony_yaml.includePath = APPLICATION_PATH "/../library"
; ------------------------------------------------------------------------------
; Doctrine Cache Configuration
; ------------------------------------------------------------------------------
; Points to default cache instance to be used. Optional is only one cache is defined
resources.doctrine.cache.defaultCacheInstance = default
; Cache Instance configuration for "default" cache
resources.doctrine.cache.instances.default.adapterClass = "Doctrine\Common\Cache\MemcacheCache"
resources.doctrine.cache.instances.default.namespace = "Application_"
resources.doctrine.cache.instances.default.options.servers.0.host = localhost
resources.doctrine.cache.instances.default.options.servers.0.port = 11211
; ------------------------------------------------------------------------------
; Doctrine DBAL Configuration
; ------------------------------------------------------------------------------
; Points to default connection to be used. Optional if only one connection is defined
resources.doctrine.dbal.defaultConnection = default
; Database configuration
;resources.doctrine.dbal.connections.default.parameters.wrapperClass = ""
resources.doctrine.dbal.connections.default.parameters.driver = "pdo_mysql"
resources.doctrine.dbal.connections.default.parameters.dbname = "myapp"
resources.doctrine.dbal.connections.default.parameters.host = "localhost"
resources.doctrine.dbal.connections.default.parameters.port = 3306
resources.doctrine.dbal.connections.default.parameters.user = "myapp"
resources.doctrine.dbal.connections.default.parameters.password = "myapp"
resources.doctrine.dbal.connections.default.parameters.charset = "utf-8"
resources.doctrine.dbal.connections.default.parameters.driverOptions.1002 = "SET NAMES 'UTF8'"
; ------------------------------------------------------------------------------
; Doctrine ORM Configuration
; ------------------------------------------------------------------------------
; Points to default EntityManager to be used. Optional if only one EntityManager is defined
resources.doctrine.orm.defaultEntityManager = default
; EntityManager configuration for "default" manager
resources.doctrine.orm.entityManagers.default.connection = default
resources.doctrine.orm.entityManagers.default.proxy.autoGenerateClasses = false
resources.doctrine.orm.entityManagers.default.proxy.namespace = "MyApp\Entity\Proxy"
resources.doctrine.orm.entityManagers.default.proxy.dir = APPLICATION_PATH "/../library/MyApp/Entity/Proxy"
resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationFiles[] = APPLICATION_PATH "/../library/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.adapterClass = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.mappingNamespace = "MyApp\Entity"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.mappingDirs[] = APPLICATION_PATH "/../library/MyApp/Entity"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.annotationReaderCache = default
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
phpSettings.error_reporting = E_ALL | E_STRICT
resources.frontController.params.displayExceptions = 1
resources.doctrine.cache.instances.default.adapterClass = "Doctrine\Common\Cache\ArrayCache"
resources.doctrine.cache.instances.default.namespace = "Application_"
This is my mistake, I haven't noticed that the following lines:
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.adapterClass = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.mappingNamespace = "MyApp\Entity"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.mappingDirs[] = APPLICATION_PATH "/../library/MyApp/Entity"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.annotationReaderCache = default
were renamed to
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.adapterClass = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingNamespace = "MyApp\Entity"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingDirs[] = APPLICATION_PATH "/../library/MyApp/Entity"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderCache = default
(notice the drivers added before the0).
FYI,
Guilherme Blanco has updated Bisna. It now works with Doctrine 2.1.0 and 2.2.0
Source:
http://phphints.wordpress.com/2011/07/10/getting-bisna-to-work-with-doctrinecommon-2-1-0/
I want to have module based application.ini in my application.
Is it possible?
Basic requirement arises because I am having multiple databases depending on modules.
Please guide.
You can use multiple Db in application.ini:
resources.db.master.adapter = "PDO_MYSQL"
resources.db.master.default = false
resources.db.master.params.dbname = "db1"
resources.db.master.params.host = "127.0.0.1"
resources.db.master.params.username = "root"
resources.db.master.params.password = ""
resources.db.slave.adapter = "PDO_MYSQL"
resources.db.slave.default = true
resources.db.slave.params.dbname = "db2"
resources.db.slave.params.host = "127.0.0.1"
resources.db.slave.params.username = "root"
resources.db.slave.params.password = ""
And initialize in your bootstrap:
public function _initDatabase() {
$config = $this->getApplication()->getOption('resources');
$dbArrays = array();
foreach($config['db'] as $name => $dbConf){
// Set up database
$db = Zend_Db::factory($dbConf['adapter'], $dbConf['params']);
$db->query("SET NAMES 'utf8'");
$dbArrays[$name] = $db;
if((boolean)$dbConf['default']){
Zend_Db_Table::setDefaultAdapter($db);
}
unset($db);
}
Zend_Registry::set("db", $dbArrays);
}
In my case I always save each adapter in the registry so I can use them separately later.
I also made a new class which extend Zend_Db_table where I have My own getAdapter($name) like so:
public function getAdapter($name = null){
if($name !== null){
$dbAdapters = Zend_Registry::get('db');
return $dbAdapters[$name];
}
return parent::getAdapter();
}
With that in each model I can do:
$this->getAdapter('slave')->fecthAll($sql);
$this->getAdapter('master')->fecthAll($sql);
The application.ini is read long before the module is determined. I'd suggest you forget about application.ini and instead try and write a controller plugin that will load in some additional configuration depending on which module was selected.
I'm trying to connect to a mssql db with the Zend Framework, my application.ini looks like this:
resources.db.adapter = "sqlsrv"
resources.db.params.host = "localhost\SQLExpress"
resources.db.params.username = "*"
resources.db.params.password = "**"
resources.db.params.dbname = "Database"
resources.db.isDefaultTableAdapter = true
Is there anything wrong with the above?
Thanks.
resources.db.adapter = "sqlsrv"
resources.db.host = "localhost\SQLEXPRESS"
resources.db.dbname = "DatabaseName"
resources.db.isDefaultTableAdapter = true
resources.db.driver_options.ReturnDatesAsStrings = true
No need for Username or Password.
I always use ReturnDatesAsStrings as it is much easier to handle MsSQL in this way.
A short question for the PROs.
Is it possible to use Zend_Auth_Adapter_DbTable with ZendX_Db_Adapter?
I've tried this:
$adapter = new Zend_Auth_Adapter_DbTable(
Zend_Registry::get('db')
);
$adapter->setTableName('USERS')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setIdentity('FOO')
->setCredential('BAR');
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate();
But it doesn't work.
ErrorMsg:
Catchable fatal error: Argument 1 passed to Zend_Auth::authenticate() must implement interface Zend_Auth_Adapter_Interface, none given, called in D:\xampp\htdocs\liquisales-online\application\controllers\IndexController.php on line 35 and defined in D:\xampp\htdocs\liquisales-online\library\Zend\Auth.php on line 115
Any hints?
Btw. ZendX_Db_Adapter ist registerd in application.ini
resources.db.adapter = Firebird
resources.db.params.dbname = "/var/db/liquisales.FDB"
resources.db.params.host = "127.0.0.1"
resources.db.params.username = sysdba
resources.db.params.password = masterkey
resources.db.params.adapterNamespace = "ZendX_Db_Adapter"
Okay, here is the right way to use Firebird DB with Zend_Auth.
At first we had to use capitol letters for column names.
Furthermore I've forgotten to pass the adapter.
Here's the right code.
$adapter = new Zend_Auth_Adapter_DbTable(
Zend_Registry::get('db')
);
$adapter->setTableName('USERS')
->setIdentityColumn('USERNAME')
->setCredentialColumn('PASSWORD')
->setIdentity($loginForm->getValue('kunummer'))
->setCredential($loginForm->getValue('passwd'));
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);
it seems your adapter doesn't implement all the methods needed .... so you would have to code the authentication yourself.