I've looked all around for a way to join multiple databases using doctrine 2, but all i've found until now are work arounds but i couldn't understood how to modify my config file to do so
//my config.php file
$isDevMode = true;
$path = array(ROOTPATH."/src");
$config = Setup::createAnnotationMetadataConfiguration($path, $isDevMode);
// database configuration parameters
$conn = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'test',
);
// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);
On this topic here, the accepted answer said that i have to create multiple entity managers,which it's logic , but when i got here, on the PHP tab(i'm using PHP as annotations), i was lost.Couldn't figure out how to change my already existing config file.
Then i found a documentation about joining databases, here , but that looks very different from what i have , probably because is an older version and i don't know if this still work, but i haven't tested that because i don't use yaml,and there's nowhere an explanation on how to do that without using a framework or yaml,xml annotations.
Related
I have now run out of ideas and Google is not helping either. I believe the issue is rather simple to solve, but currently I am not seeing why it happens. Everything works fine on my testing environment with dev mode.
But doesn't work on production where the dev mode is off and proxy files need to be generated manually.
The first time I generated proxy files on the production environment, everything worked fine. Now I needed to improve some of the entities and when I use the "doctrine orm:generate-proxies" command again, then all the proxy files get generated and when I look at the new entity proxy files, then I can see that the new column are mapped.
But when I try to save or load new object, then nothing gets saved or load to the new columns. It is as if doctrine 2 doesn't understand, that there are new proxy files that it should use. Instead, it keeps on using some other hidden old proxy files, cached away in some dark end.
Any idea why doctrine 2 doesn't use the newly generated proxy files?
For extra clarity, this the the Doctrine Initialisation code.
$paths = array(__DIR__ . '/../ORM/Definition/Doctrine2/Entity');
$isDevMode = false;
$autoGenerateProxyClasses = AbstractProxyFactory::AUTOGENERATE_NEVER;
if ($this->serverDevelopment()) {
$isDevMode = true;
$autoGenerateProxyClasses = AbstractProxyFactory::AUTOGENERATE_ALWAYS;
}
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'REMOVE',
'password' => 'REMOVE',
'dbname' => 'REMOVE',
'host' => 'REMOVE'
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$config->setProxyDir(__DIR__ . '/../ORM/Definition/Doctrine2/Entity/Proxy');
$config->setProxyNamespace('Database\ORM\Definition\Doctrine2\Entity\Proxy');
$config->setAutoGenerateProxyClasses($autoGenerateProxyClasses);
$this->entityManager = EntityManager::create($dbParams, $config);
Any help would be appreciated,
Hendrik
I am using following code to generate proxies:
$em = Zend_Registry::get('em');
$proxyFactory = $em->getProxyFactory();
$metadatas = $em->getMetadataFactory()->getAllMetadata();
$proxyFactory->generateProxyClasses($metadatas, APPLICATION_PATH . '/models/Proxies');
I have Proxies folder inside models folder.
Try generating proxies using this code.
Found out the issue. But I'm not sure, if it was the permissions or the user. But I changed the folder from 775 to 777 and also the user. Now it works :)
I'm working on a rewrite of a project from the ground up and figured I would try to learn MVC along the way. In this case, I've chosen Phalcon and am still working through the fundamentals of converting the tutorials to my own project.
I have two "configuration" settings that I need to account for. First, I need to read a configuration file that has the database credentials (this works properly).
require_once('../fileconfig.php'); // Read config file
$init = new Phalcon\Config\Adapter\Php("../fileconfig.php"); //Convert it to array
But once I have that, how do I actually connect to the database and add it to $di-> (which, if I understand correctly, is effectively the global class? Ultimately, I want to pull the contents of "select * from config" into an array and use that for the application configuration. In this case, var_dump($dbh) returns "null"
//Connect to database
$di->set('db', function() use ($init) {
$dbh = new \Phalcon\Db\Adapter\Pdo\Mysql([
"host" => $init->database->host,
"username" => $init->database->username,
"password" => $init->database->password,
"dbname" => $init->database->dbname
]);
return $dbh;
});
var_dump($dbh); //returns null
If I remove the $di-> section, the array returns the data that I need, but it still doesn't help me figure out how to connect to the database and have it available globally for other functions in the models:
$dbh = new \Phalcon\Db\Adapter\Pdo\Mysql([
"host" => $init->database->host,
"username" => $init->database->username,
"password" => $init->database->password,
"dbname" => $init->database->dbname
]);
Returns:
object(Phalcon\Db\Adapter\Pdo\Mysql)[28]
protected '_descriptor' =>
array (size=4)
'host' => string 'localhost' (length=9)
'username' => string 'testuser' (length=8)
'password' => string 'testpass' (length=8)
'dbname' => string 'testdb' (length=6)
This question seems to be close to what I'm asking, but was more about error handling than the actual connection and I didn't see an answer to my question there.
To resolve your database you need to resolve your di. You could resolve it the file you declared it in with
$di->getShared('db')
But note, you don't want to do that. You want your files seperated with their responsibilities.
Inside of a class that inherits \Phalcon\Mvc\Controller you can use
$this->db->
Please refer to http://docs.phalconphp.com/en/latest/reference/di.html in order to see why to use a DI, and all the nuances of accessing it
It really helps to go through other phalcon projects and look at how everything works together. Please refer to the source here and look at how projects are set up:
https://github.com/phalcon/invo
https://github.com/phalcon/vokuro
https://github.com/phalcon/forum
These are ranked by complexity so start with invo first and then move on
I have following database configuration in database.php file from my CakePHP app:
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'database',
'prefix' => '',
);
All is working fine, except of one queue shell script. The script is looping and waiting for commands to run (for example to update some reports). After a while 1-2 days database data is changing, but the script will still "see" the old data, and results of command is wrong. If I restart shell script, the results are OK... for few days.
I have to mention that I had "lost database connection" issue before in the script and I have solved it by runing every 10-15 min:
$user = $this->User->find('first');
Now I am affraid this is making the connection persistent somehow...
How can I reset the database connection ?
EDIT:
I was just refactoring the code to check if I can set $cacheQueries to false on the Model. But in few parts of the code I am using ConnectionManager directly, and only then I have "cache" problem. If I query database from Model->find results are ok. I need direct queries for performance reasons in few places...
$query = "SELECT COUNT(1) as result
FROM
......
";
$db = ConnectionManager::getDataSource('default');
$result = $db->query($query);
The property $cacheQueries which #burzum mentioned, seems not to be in use in any cake model method.
But I found another interesting fact in the source of the DboSource.
You need to use the second parameter of the DboSource::query() method to turn off the caching. Or the third if you want to provide additional parameters for the DboSource::fetchAll() method.
Eventhough this will fix your problem, you should write your queries with the Model::find() method that CakePHP offers.
You should only not use them if they are seriously impacting your performance.
func0der
Try to set these two model properties to false:
$cacheQuery http://api.cakephp.org/2.4/source-class-Model.html#265
$cacheSources http://api.cakephp.org/2.4/source-class-Model.html#499
I've got a dubious issue. I have a set of existing annotated Doctrine entities which have been successfully used in a Symfony2/Doctrine2 project. However, I'm currently isolating some core functionality of this project into it's own web framework independent library and I can't seem to get the entities to function properly.
At the moment my major concern is the fact that the Doctrine CLI utility is giving me mixed results.
When I do the following:
bin/doctrine orm:validate-schema
I get the following output:
[Mapping] OK - The mapping files are correct.
[Database] OK - The database schema is in sync with the mapping files.
But when I do:
bin/doctrine orm:info
I get this:
[Exception]
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have entities or mapping files you should check your mapping configuration for errors.
I have gone over my configuration a gazillion times now. I've even removed all my entities and left a most basic User entity in there giving me the same scenario.
What could possible be the source of these mixed results?
It turns out that the standard Doctrine config set up [1] doesn't work with my code base, or any code base I have tested with, maybe the docs are outdated. After ploughing through the Interwebs for hours, this is the configuration that finally made it work for me:
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Annotations\AnnotationReader;
$paths = array( realpath(__DIR__."/../src/My/Entity") );
$isDevMode = TRUE;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'myuser',
'password' => 's3cr3t',
'dbname' => 'mydb',
);
$cache = new \Doctrine\Common\Cache\ArrayCache();
$reader = new AnnotationReader();
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, $paths);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$config->setMetadataCacheImpl( $cache );
$config->setQueryCacheImpl( $cache );
$config->setMetadataDriverImpl( $driver );
$entityManager = EntityManager::create($dbParams, $config);
//-- This I had to add to support the Mysql enum type.
$platform = $entityManager->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
[1] http://docs.doctrine-project.org/en/latest/tutorials/getting-started.html
The accepted answer is ok but the same thing could be achieved in less verbose manner:
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array( realpath(__DIR__."/../src/My/Entity") );
$isDevMode = TRUE;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'myuser',
'password' => 's3cr3t',
'dbname' => 'mydb',
);
$config = Setup::createAnnotationMetadataConfiguration(
$paths, $isDevMode, null, null, false
);
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
$entityManager = EntityManager::create($dbParams, $config);
//-- This I had to add to support the Mysql enum type.
$platform = $entityManager->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
All here is about usage of simple annotation driver or not (last parameter of the Setup::createAnnotationMetadataConfiguration function. By default, a simple annotation driver is used which doesn't recognise the #ORM\Entity annotation.
I have PHP entity in Ressources/config/doctrine/Foo.orm.php
but I use annotation.
Too different way for orm = [Exception]
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have entities or mapping files you should check your mapping configuration for errors.
Just delete the bad entity file
For me it happened that I imported the vendor directory from a different system and it wasn't working properly.
A simple composer install or php composer.phar install fixed it.
I'm going to implement login functionality using zend framework 2.
My code as follows.
$dbAdapter = new DbAdapter(array(
'driver' => 'Mysqli',
'database' => 'db',
'username' => 'root',
'password' => 'password'
));
$authAdapter = new AuthAdapter($dbAdapter);
$authAdapter
->setTableName('admin_users')
->setIdentityColumn('user_name')
->setCredentialColumn('password')
;
$authAdapter
->setIdentity($username)
->setCredential($password)
;
$result = $authAdapter->authenticate();
It gives error message as follows.
The supplied parameters to DbTable failed to produce a valid sql statement, please check table and column names for validity.
I checked database configurations and column names. But they are correct. Can anyone give me a clue to check this out please.
Thanks.
There's no need to re-invent the wheel and struggle with this yourself. ZF2 has been created with modularity in mind so that plug in modules for this kind of thing can be written.
There are a few such modules available already at the ZF-Commons site. The one you are looking for is ZfcUser. Just follow the instructions to install and use it. There is even an excellent tutorial written by the author of the module that will take you through the whole process step by step. http://blog.evan.pro/getting-started-with-the-zf2-skeleton-and-zfcuser
There is also an excellent how-to page to tell you how to modify it for your own use.