Connect to Multiple data bases using Zend Framework 2 and DoctrineORMModule - database

Hello i'm using Zend Framework 2 and DoctrineORMModule. I need to access to different data bases connections and map two different set of schemas.
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDODblib\Driver',
'params' => array(
'host' => 'HOST',
'port' => '1433',
'user' => 'USER',
'password' => 'PASS',
'dbname' => 'DBNAME',
)
)
)
),
/////////////
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => '127.0.0.1',
'port' => '3306',
'user' => 'root',
'password' => 'root',
'dbname' => 'test',
)
)
),
),
I found this in the documentation:
https://github.com/doctrine/DoctrineORMModule/blob/master/docs/configuration.md#how-to-use-two-connections
But it is not very descriptive.
Can anyone help me?

use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration;
use Doctrine\DBAL\Connection;
/**
* #author Rafał Książek
*/
class DbFactory
{
/**
* #var array
*/
protected $config;
/**
* #return array
*/
public function getConfig()
{
return $this->config;
}
/**
* #param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
}
/**
* Create connection to database
*
* #param string $dbName
* #return \Doctrine\DBAL\Connection
* #throws \InvalidArgumentException
* #throws \Exception
*/
public function getConnectionToDatabase($dbName)
{
$config = $this->getConfig();
if (empty($config['doctrine']['connection']['orm_default']['params'])) {
throw new \InvalidArgumentException('There is insufficient data in the configuration file!');
}
$params = $config['doctrine']['connection']['orm_default']['params'];
$params['dbname'] = $dbName;
$params['driver'] = 'pdo_mysql';
if (!($dbConnection = DriverManager::getConnection($params)))
throw new \Exception('There was a problem with establish connection to client db');
return $dbConnection;
}
/**
*
* #param \Doctrine\DBAL\Connection $dbConnection
* #param \Doctrine\ORM\Configuration $config
* #return \Doctrine\ORM\EntityManager
*/
public function getEntityManager(Connection $dbConnection, Configuration $config)
{
return EntityManager::create($dbConnection, $config);
}
}
How to used:
$applicationConfig = $sm->get('config');
$em = $sm->get('Doctrine\ORM\EntityManager');
$emDefaultConfig = $em->getConnfiguration();
$dbFactory = new DbFactory($applicationConfig);
$anotherConnection = $dbFactory->getConnectionToDatabase('another_db');
$anotherEntityManager = $dbFactory->getEntityManager($anotherConnection, $emDefaultConfig);
$usersOnAnotherDb = $anotherEntityManager->getRepository('Application\Entity\User')->findAll();

Have a look at link and link. Unfortunately the use statements seems to be missing. These are the factories with FQDN:
'factories' => array(
'doctrine.connection.orm_alternative' => new \DoctrineORMModule\Service\DBALConnectionFactory('orm_alternative'),
'doctrine.configuration.orm_alternative' => new \DoctrineORMModule\Service\ConfigurationFactory('orm_alternative'),
'doctrine.entitymanager.orm_alternative' => new \DoctrineORMModule\Service\EntityManagerFactory('orm_alternative'),
'doctrine.driver.orm_alternative' => new \DoctrineModule\Service\DriverFactory('orm_alternative'),
'doctrine.eventmanager.orm_alternative' => new \DoctrineModule\Service\EventManagerFactory('orm_alternative'),
),
If you only want to access another database (on the same server, same user, etc.), you can easily define your tables in the entities like this:
#ORM\Table(name="database.table")
and save yourself a lot of trouble.

U can set this array config for every separate module in your module config file
return array(
'doctrine' => array(
'orm_autoload_annotations' => true,
'connection' => array(
'orm_default' => array(
'configuration' => 'orm_default',
'eventmanager' => 'orm_default',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'database',
)
),
),
'configuration' => array(
'orm_default' => array(
'driver' => 'orm_default',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineORMModule/Proxy',
'proxy_namespace' => 'DoctrineORMModule\Proxy',
'filters' => array()
)
),
'driver' => array(
'orm_default' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\DriverChain',
'drivers' => array()
)
),
'entitymanager' => array(
'orm_default' => array(
'connection' => 'orm_default',
'configuration' => 'orm_default'
)
),
'eventmanager' => array(
'orm_default' => array()
),
'sql_logger_collector' => array(
'orm_default' => array(),
),
'entity_resolver' => array(
'orm_default' => array()
),
'authentication' => array(
'orm_default' => array(
'objectManager' => 'doctrine.entitymanager.orm_default',
//'identityClass' => 'Application\Model\User',
//'identityProperty' => 'username',
//'credentialProperty' => 'password'
),
),
),
// zendframework/zend-developer-tools specific settings
'view_manager' => array(
'template_map' => array(
'zend-developer-tools/toolbar/doctrine-orm' => __DIR__ . '/../view/zend-developer-tools/toolbar/doctrine-orm.phtml',
),
),
'zenddevelopertools' => array(
'profiler' => array(
'collectors' => array(
'orm_default' => 'doctrine.sql_logger_collector.orm_default',
),
),
'toolbar' => array(
'entries' => array(
'orm_default' => 'zend-developer-tools/toolbar/doctrine-orm',
),
),
),
);

Related

Couldn't find Aco node identified by "Array ( [Aco0.model] => model [Aco0.foreign_key] => U ) "

I'm going through the following links to use Acl component in my application
http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html
http://code.tutsplus.com/tutorials/how-to-use-cakephps-access-control-lists--net-1345
in usersController i have a function to install aros and acos
public function install(){
$aro = $this->Acl->Aro;
$aco = $this->Acl->Aco;
$aro_groups = array(
0 => array(
'alias' => 'admin'
),
1 => array(
'alias' => 'operator'
),
2 => array(
'alias' => 'user'
),
);
$aco_groups = array(
0 => array(
'alias' => 'User'
),
1 => array(
'alias' => 'Supplier'
),
2 => array(
'alias' => 'Inventory'
),
3 => array(
'alias' => 'Invoice'
),
4 => array(
'alias' => 'Incentive'
),
5 => array(
'alias' => 'Promotion'
),
6 => array(
'alias' => 'Feedback'
),
7 => array(
'alias' => 'Message'
),
8 => array(
'alias' => 'History'
),
);
foreach($aro_groups as $data):
$aro->create();
$aro->save($data);
endforeach;
foreach($aco_groups as $data):
$aco->create();
$aco->save($data);
endforeach;
foreach($aco_groups as $data):
$this->Acl->allow('admin',$data);
$this->Acl->allow('operator',$data);
endforeach;
}
my user model is as follows:
public $belongsTo = array(
'Role' => array(
'className' => 'Role',
'foreignKey' => 'role_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public function beforeSave($options = array()){
if(isset($this->data['User']['password']))
{
$this->data['User']['password']= AuthComponent::password($this->data['User']['password']);
}
}
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
public function bindNode($user) {
return array('model' => 'Role', 'foreign_key' => $user['User']['role_id']);
}
role model:
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'role_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
return null;
}
now, when I try to access /users/install just to check that all relations are successfully created or not, i'm getting this error
AclNode::node() - Couldn't find Aco node identified by "Array ( [Aco0.model] => model [Aco0.foreign_key] => U ) "
Warning (2): Illegal string offset 'id' [CORE\Cake\Model\AclNode.php, line 140]
In the parentNode function in your User model, you have group_id instead of role_id.
Replace all occurrences of group_id to role_id in your User model and you should be ok and also Replace Group with Role
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['role_id'])) {
$roleId = $this->data['User']['role_id'];
} else {
$roleId = $this->field('role_id');
}
if (!$roleId) {
return null;
} else {
return array('Role' => array('id' => $roleId));
}
}

How do I query data in CakePHP using HABTM relationships and translation?

I'm using Cakephp and try to make my model to ActAs 'Translation'. But i have a problem.
My Client model relations are:
public $belongsTo = array("User");
public $hasAndBelongsToMany = array(
'Category' =>
array(
'className' => 'Category',
'joinTable' => 'categories_clients',
'foreignKey' => 'client_id',
'associationForeignKey' => 'category_id',
'unique' => true,
),
);
In Category Model:
public $name = 'Category';
public $hasMany = array(
'Product'
);
public $displayField = 'title';
public $recursive = 1;
public $actsAs = array(
'Tree',
'Translate' => array(
'title',
'alias',
'description',
),
'Containable' => array('CategoryTranslations'),
);
public $translateModel = 'CategoryTranslations';
public $translateTable = 'category_translations';
When i query from Client Controller to get all Client and relative Category, i can't get field "title", "alias" and "description" of Category. Here is my code:
if(isset($this->passedArgs['language_id'])){
$language_id = $this->passedArgs['language_id'];
}
else{
$language_id = Configure::read('Config.language_site');
}
$this->Client->Category->locale = $language_id;
$this->Client->recursive = 1;
$this->paginate = array('limit' => 20,'order' => array('User.id' => 'desc'));
$Clients = $this->paginate();
Here is result:
array(
'Client' => array(
'id' => '1',
'user_id' => '1',
'client_type' => 'tenderer',
'image' => null,
'image_list' => null,
'code' => 'SG0593',
'name' => 'Oorts',
'address' => '1000 Huynh Tan Phat',
'telephone' => '0987654321',
'fax' => '0983213434',
'email' => 'nguyentuandat.vn#gmail.com',
'proxy' => 'Dat Nguyen',
'position' => 'C.E.O',
'mobile' => '0987654321',
'referred_by' => 'noone',
'order' => null,
'status' => true,
'created' => '2014-03-27 00:00:00',
'modified' => '2014-03-27 00:00:00'
),
'User' => array(
'password' => '*****',
'id' => '1',
'username' => 'admin',
'group_id' => '1',
'gender' => 'Male',
'first_name' => 'Nguyễn',
'middle_name' => 'Tuấn',
'last_name' => 'Đạt',
'email' => 'nguyentuandat.vn#gmail.com',
'phone_number' => '(+84) 947235313',
'image' => '/uploads/images/255443_102754699884371_1177788671_n.jpg',
'status' => true,
'created' => '2014-01-16 09:26:09',
'modified' => '2014-01-22 06:47:25',
'full_name' => 'Nguyễn Tuấn Đạt'
),
'Category' => array(
(int) 0 => array(
'id' => '1',
'parent_id' => '0',
'type' => 'product',
'image' => '',
'order' => '0',
'status' => true,
'lft' => '1',
'rght' => '16',
'created' => '2014-01-25 00:00:00',
'modified' => '2014-01-25 00:00:00',
'CategoriesClient' => array(
'id' => '1',
'category_id' => '1',
'client_id' => '1'
)
)
)
)
Can you help me? Thank you!
Restructure the code
Unfortunately because of how behaviors work - that's none-trivial to solve exactly as asked.
You can however do something like this:
$Clients = $this->paginate();
// extract category ids in the results
$categoryIds = array_unique(Hash::extract($Clients, '{n}.Category.{n}.id'));
$categories = $this->Client->Category->find('all', array(
'conditions' => array(
'Category.id' => $categoryIds
)
));
// generate a category-id indexed array of categories
$categories = Hash::combine($categories, '{n}.Category.id', '{n}.Category');
// replace the untranslated category entries with translated category data
foreach($Clients as &$client) {
foreach($client['Category'] as &$cat) {
$id = $cat['id'];
$cat = $categories[$id];
}
}
This example is shown as pseudo controller code - you could also consider putting it in your Client afterFind method or a custom finder.
Thank for help.
I found a solution:
I added bellow code into AppModel->afterFind and it work fine
public function afterFind($results, $primary = false) {
if(!empty($this->hasAndBelongsToMany)) {
foreach($this->hasAndBelongsToMany as $model => $settings) {
if(isset($this->{$model}->actsAs['Translate'])) {
if(!empty($results[0][$model])) {
foreach($results as $k => $v){
foreach($results[$k][$model] as $row => $result) {
$supplement = $this->{$model}->find('first', array(
'conditions' => array(
$model .'.id' => $result['id']),
'fields' => $this->{$model}->actsAs['Translate'],
'recursive' => -1));
if(!empty($supplement)) {
$results[$k][$model][$row] = array_merge($results[$k][$model][$row], array_diff($supplement[$model], $result));
}
}
}// end foreach k=>v
}
}
}
}
return $results;
}

CakePHP saveAssociated not saving HasMany Through Model Data

I'm trying to get my associated models in CakePHP 2.3 to save properly, but I'm having issues. I'm storing posts, and I want to know what links are in those posts. For each of those links, I'd like to store anchor text if it is available. My database is set up as in the following diagram.
(source: derekperkins.com)
Anchor Model
class Anchor extends AppModel {
public $hasMany = array(
'PostsUrl' => array(
'className' => 'PostsUrl',
'foreignKey' => 'anchor_id',
'dependent' => false
)
);
public function save($data = NULL, $validate = true, $fieldList = array()) {
$id = Anchor::find('first', array(
'fields' => array('id'),
'recursive' => -1,
'conditions' => array('anchor' => $data['anchor'])
));
if( $id )
$data['id'] = $id['Anchor']['id'];
return parent::save($data, $validate, $fieldList);
}
}
URL Model
class Url extends AppModel {
public $hasMany = array(
'PostsUrl' => array(
'className' => 'PostsUrl',
'foreignKey' => 'url_id',
'dependent' => false
)
);
public function save($data = NULL, $validate = true, $fieldList = array()) {
$id = Url::find('first', array(
'fields' => array('id'),
'recursive' => -1,
'conditions' => array('url' => $data['url'])
));
if( $id )
$data['id'] = $id['Url']['id'];
return parent::save($data, $validate, $fieldList);
}
}
PostsUrl Model
class PostsUrl extends AppModel {
public $belongsTo = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'post_id'
),
'Url' => array(
'className' => 'Url',
'foreignKey' => 'url_id'
'Anchor' => array(
'className' => 'Url',
'foreignKey' => 'anchor_id'
)*/
);
}
Post Model
class Post extends AppModel {
public $hasMany = array(
'PostsUrl' => array(
'className' => 'PostsUrl',
'foreignKey' => 'post_id',
'dependent' => false
)
);
public function save($data = NULL, $validate = true, $fieldList = array()) {
$id = Post::find('first', array(
'fields' => array('id'),
'recursive' => -1,
'conditions' => array('external_post_id' => $data['external_post_id'])
));
if( $id )
$data['id'] = $id['Post']['id'];
return parent::save($data, $validate, $fieldList);
}
}
Submitting Data
I've created a form to test my model. This is the code I'm using to save the array created by the form. I am getting a message saying that things saved successfully, but only the post saves. Nothing is entered into the other three tables. I'm also using DebugKit and no SQL calls reference any of that data.
$this->Post->saveAssociated($this->request->data, array('deep' => true))
Array
(
[Post] => Array
(
[external_post_id] => 12345
[sentiment_score] => 3.3
)
[URL] => Array
(
[url] => http://test.com
)
[Anchor] => Array
(
[anchor] => Test Anchor
)
)
I've also tried formatting my arrays to have the URL and Anchor underneath PostsUrl as a subarray, but that didn't work either.
My Model::save functions are there to keep me from duplicating data, and they work properly in other models I have used in the past (though I'm open to suggestions on a better way to do this, as this uses a database call for each check). I've also tried commenting them out, and it doesn't affect my code. How should I structure this to save properly?
First of all about your Model::save functions, for example:
public function save($data = NULL, $validate = true, $fieldList = array()) {
$id = Post::find('first', array(
'fields' => array('id'),
'recursive' => -1,
'conditions' => array('external_post_id' => $data['external_post_id'])
));
if( $id )
$data['id'] = $id['Post']['id'];
pr($data);
return parent::save($data, $validate, $fieldList);
}
it prints $data in this way :
Array
(
[id] => 3 //for example 3
[Post] => Array
(
[external_post_id] => 12345
[sentiment_score] => 3.3
)
[URL] => Array
(
[url] => http://test.com
)
[Anchor] => Array
(
[anchor] => Test Anchor
)
)
this $data is incorrect, the correct data is:
Array
(
[Post] => Array
(
[id] => 3 //for example 3
[external_post_id] => 12345
[sentiment_score] => 3.3
)
[URL] => Array
(
[url] => http://test.com
)
[Anchor] => Array
(
[anchor] => Test Anchor
)
)
You must change your Model::save function this way:
public function save($data = NULL, $validate = true, $fieldList = array()) {
$id = Post::find('first', array(
'fields' => array('id'),
'recursive' => -1,
'conditions' => array('external_post_id' => $data['external_post_id'])
));
if( $id )
$data[$this->name]['id'] = $id['Post']['id'];
return parent::save($data, $validate, $fieldList);
}
second , you can't save this data with single save, you should save your data this way:
$postData = array
(
'Post' => array
(
'external_post_id' => 12345
'sentiment_score' => 3.3
)
);
$this->Post->save($data);
$postUrlId = $this->PostsUrl->find('first', array(
'conditions' => array(
'post_id' => $this->Post->id
),
'fields' => array('id')
));
$urlAnchorData = array(
'URL' => array
(
'url' => 'http://test.com'
),
'Anchor' => array
(
'anchor' => 'Test Anchor'
),
'PostsUrl' => array(
'id' => $postUrlId['PostsUrl']['id']
)
);
$this->PostsUrl->saveAll('$urlAnchorData');

could not find driver (ZF2 + Doctrine 2 + PostgreSQL). Wrong driver

application.config.php :
'doctrine' => array(
'connection' => array(
// default connection name
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
'params' => array(
'driver' => 'pdo_pgsql',
'host' => 'localhost',
'port' => '5432',
'user' => 'postgres',
'password' => 'root',
'dbname' => 'thebasee',
)
)
),
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
)
)
)
),
module.config.php:
// Controllers in this module
'controller' => array(
'classes' => array(
'Account/Account' => 'Account\Controller\AccountController'
),
),
'doctrine' => array(
'driver' => array(
'orm_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => 'orm_driver'
)
),
'odm_driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
),
'odm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Document' => 'odm_driver'
)
)
)
),
// Routes for this module
'router' => array(
'routes' => array(
'Account' => array(
'type' => 'segment',
'options' => array(
'route' => '/account[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Account/Account',
'action' => 'index',
),
),
),
),
),
// View setup for this module
'view_manager' => array(
'template_path_stack' => array(
'Account' => __DIR__ . '/../view',
),
),
Now, the error is:
"could not find driver" in /var/www/applicationDir/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:36
Driver for postgresql is installed (another project's works fine) but for MySql inst, and in track i sow this:
/var/www/applicationDir/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(36):
PDO->__construct('mysql:host=loca...', 'postgres', 'root', Array)
It seems it uses Mysql driver, but why, if i choose Pg?!
Please help.

FilesSize validator in Model using factory

That's my model SiteDesign.php in Object\Model:
namespace Object\Model;
use Zend\Validator\StringLength;
use Zend\Validator\NotEmpty;
use Zend\Validator\File\Size;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class SiteDesign {
public $logo_filename;
public $background_image;
public $background_color;
public function exchangeArray($data) {
$this->logo_filename = (isset($data['logo_filename'])) ? $data['logo_filename'] : null;
$this->background_image = (isset($data['background_image'])) ? $data['background_image'] : null;
$this->background_color = (isset($data['background_color'])) ? $data['background_color'] : null;
}
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new \Exception('Not used');
}
public function getInputFilter() {
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'logo_filename',
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'FilesSize',
'options' => array(
'max' => 4 * 1024,
'messages' => array(
Size::TOO_BIG => 'Размерът на логото не може да надвишава 5MB.',
),
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'background_image',
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'FilesSize',
'options' => array(
'max' => 4 * 1024,
'messages' => array(
Size::TOO_BIG => 'Размерът на изображението за фон не може да надвишава 5MB.',
),
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'background_color',
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
but there's an error message:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for FilesSize
so is it possible to define a FilesSize validator directly in the model using factory or not? And is this the right way to do it? Thanks in advance.
As of Zend\Validator\ValidatorPluginManager (which is responsible of instantiating validators by name) the correct name is `"FileSize" (you got too many "s" in there).

Resources