I have an array to store the configuration like this
$config = array (
'db' => array(
'db1' => array(
'dbname' => 'mydatabase',
'user' => 'myusername',
'pass' => 'mypassword',
'host' => 'myhost'
)
),
'url' => array(
'homeUrl' => 'http://www.example.com'
)
)
And I'm writing a function to retrieve the data from the array by passing in a string like db.db1.dbname and it supposes to give me 'mydatabase'
I tried to explode the string into an array in order to get the keys, db, db1, and dbname. but after that, I got kinda stuck on how exactly I'm supposed to use them like $config -> db -> db1 -> dbname or $config['db']['db1']['dbname'] in order to get 'mydatabase'.
Ideally, say I have the function named read($arg, $array), and I would like to retrieve results like this
read('db.db1.dbname', $config), returns 'mydatabase'
read('url.homeUrl', $config), returns 'http://www.example.com'
Since I don't know how many keys are contained in the string, I need this to be more dynamic.
Thanks in advance
I think you're thinking about JSON
try $config['db']['db1']['dbname'];
I'm really wondering why you would want to do this, but here it goes:
function read($item, $config)
{
$selectors = explode('.', $item);
$configItem = $config;
foreach($selectors as $selector) {
$configItem = $configItem[$selector];
}
return $configItem;
}
$config = array (
'db' => array(
'db1' => array(
'dbname' => 'mydatabase',
'user' => 'myusername',
'pass' => 'mypassword',
'host' => 'myhost',
),
),
'url' => array(
'homeUrl' => 'http://www.example.com',
),
);
read('db.db1.dbname', $config); // will return mydatabase
Note that you would have to check whether the keys exists and throw an error or exception if that's not the case.
Instead of using the function is there a reason why you cannot do:
function functionThatNeedsDatabaseInfo($databaseInfo)
{
// do database stuff
}
functionThatNeedsDatabaseInfo($config['db']['db1']);
They are just plain nested arrays.
$config['db'] will give you the first inner array
$config['db']['db1'] will give you the array with all the database configurations you're looking for.
$config['db']['db1']['dbname']: 'dbname' is the index in the 'db1' array that will give you the value you want.
function read($layers, $arr){
$toReturn = $arr;
foreach(split('.', $layers) as $layer)
$toReturn = $toReturn[$layer];
return $toReturn;
}
Why would you want to do this, I haven't the foggiest. But there you go.
Cast that thing to an Object! Or rather, cast every array as an object, then you can use it like one.
$config = (object) array (
(object) 'db' => array(
(object) 'db1' => array(
'dbname' => 'mydatabase',
'user' => 'myusername',
'pass' => 'mypassword',
'host' => 'myhost'
)
),
(object) 'url' => array(
'homeUrl' => 'http://www.example.com'
)
)
Related
I tried to take out my database connection from the LocalConfiguration. But it doesn't work on this way. Do you have any ideas how i can realize it. Here what i tried to make it work:
LocalConfiguration.php:
<?php
include_once 'databaseConn.php';
return [
'BE' => [
'debug' => false,
'explicitADmode' => 'explicitAllow',
'installToolPassword' => '$P$CcKE/MYkjKWDzNWsnVZhMBDAttVVrf.',
'loginSecurityLevel' => 'rsa',
],
and in the databaseConn.php:
<?php
$TYPO3_CONF_VARS['DB']['database'] = 'db_name';
$TYPO3_CONF_VARS['DB']['host'] = 'localhost';
$TYPO3_CONF_VARS['DB']['password'] = 'password';
$TYPO3_CONF_VARS['DB']['socket'] = '';
$TYPO3_CONF_VARS['DB']['username'] = 'usr_name';
Hope you can help me.
thanks
Chris
Create a file called AdditionalConfiguration.php in same directory. You can override every value there by addressing it directly
$GLOBALS['TYPO3_CONF_VARS']['DB']['database'] = 'custom';
You can also check the ApplicationContext by $context = GeneralUtility::getApplicationContext()->__toString(); which can be set in a .htaccess or vhost config
Use the following code in AdditionalConfiguration.php:
$configurationSettings = array();
#include_once(__DIR__.'/DatabaseCredentials.php');
#include_once(… some other files …);
if (is_array($configurationSettings)) {
foreach ($configurationSettings as $path => $value) {
$GLOBALS['TYPO3_CONF_VARS'] = \TYPO3\CMS\Core\Utility\ArrayUtility::setValueByPath($GLOBALS['TYPO3_CONF_VARS'], $path, $value);
}
}
unset($configurationSettings);
then set your database credentials in DatabaseCredentials.php:
$configurationSettings = array_merge($configurationSettings, array(
'DB/database' => 'local_database',
'DB/username' => 'local_username',
'DB/password' => 'secret'
));
and you're done.
It is better that you add your database connection code into "LocalConfiguration.php".
return array(
'BE' => array(
'debug' => false,
'explicitADmode' => 'explicitAllow',
'installToolPassword' => '$P$CcKE/MYkjKWDzNWsnVZhMBDAttVVrf.',
'loginSecurityLevel' => 'rsa',
),
'DB' => array(
'database' => 'db_name',
'extTablesDefinitionScript' => 'extTables.php',
'host' => 'localhost',
'password' => 'password',
'socket' => '',
'username' => 'username',
),
I'm trying to create a ZF2 application with multiple databases. Based on a user, the database should be dynamically set.
Right now I've the following:
database.local.php
return array(
'db' => array(
'adapters' => array (
'master_db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=master_db;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
'username' => 'USERNAME',
'password' => 'PASSWORD'
),
'tentant_db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=tenant_db;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
'username' => 'USERNAME',
'password' => 'PASSWORD'
),
)
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Db\Adapter\AdapterAbstractServiceFactory',
)
),
);
For test purposes I've created a form that has a method to fetch some data and put it in a select box. The code to get the database connection is shown in the code below.
MyController.php (in some module)
//... some code
public function someAction(){
$dbAdapter = $this->getServiceLocator()->get('tentant_db');
$form = new AddEolConnectorForm($dbAdapter);
$viewModel = new ViewModel(array(
'form' => $form
));
return $viewModel;
}
//... some more code
My question is, how can I dynamically set the dbname for the tentant_db adapter in my controller (or module)?
Thanks for your help.
The config merge event is one of zend newer event's I believe. It triggers when zend is mergin the config array's which is perfect for the problem you are facing since you can override some array key's dynamically.
public function onMergeConfig(ModuleEvent $e)
{
$configListener = $e->getConfigListener();
$config = $configListener->getMergedConfig(false);
// I'm actually not sure if you have the route match here otherwise you may have to
// use some other method to retrieve the url.
$match = $e->getRouteMatch();
switch ($match) {
case 'first-dependency':
$config['db']['adapter'] => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=master_db;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
'username' => 'USERNAME',
'password' => 'PASSWORD',
),
break;
case 'second-dependency':
$config['db']['adapter'] => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=tenant_db;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
'username' => 'USERNAME',
'password' => 'PASSWORD',
),
break;
// Pass the changed configuration back to the listener:
$configListener->setMergedConfig($config);
}
Based on the above answer I've created to following:
Module.php
class Module implements AutoloaderProviderInterface
{
public function init(ModuleManager $moduleManager)
{
$events = $moduleManager->getEventManager();
// Registering a listener at default priority, 1, which will trigger
// after the ConfigListener merges config.
$events->attach(ModuleEvent::EVENT_MERGE_CONFIG, array($this, 'onMergeConfig'));
}
public function onMergeConfig(ModuleEvent $e)
{
$db = $this->getTentantDb();
$configListener = $e->getConfigListener();
$config = $configListener->getMergedConfig(false);
$config['db']['adapters']['tenant_db']['dsn'] = 'mysql:dbname='. $db .';host=localhost';
$configListener->setMergedConfig($config);
}
// Some more code
public function getTenantDb(){
$tenant_db = 'tenant_12345'
return $tenant_db;
}
}
I don't know if it is the best solution, but the above code is working. I think the next steps should be to put the code in a generic module or something so I can access it from all my modules.
I have been playing with the developer preview version of CakePHP 3.0 and I'm stuck trying to get the new ORM working with pagination.
In my PostsController.php I have:
<?php
namespace App\Controller;
use App\Controller\AppController;
class PostsController extends AppController {
public $name = 'Posts';
public $uses = 'Posts';
public $components = ['Paginator'];
public $paginate = [
'fields' => ['Posts.id'],
'limit' => 1,
'order' => [
'Post.id' => 'asc'
]
];
public function index() {
$posts = $this->paginate('Posts');
$this->set('posts', $posts);
}
}
However the paging is working but the data doesn't come back. Apparently this because the data isn't directly returned in the new ORM but an object... Has anyone tried this yet? Knows how to fix the issue to get it working with the paginator?
I've been reading the Migration Guide: http://book.cakephp.org/3.0/en/appendices/orm-migration.html but don't see anything about combining it with the paginator.
Note: I can't debug $posts and show it here because it's about 2000 lines of code containing all sorts of stuff about the ORM. Here's a taster...
object(Cake\ORM\ResultSet) {
[protected] _query => object(Cake\ORM\Query) {
[protected] _table => object(Cake\ORM\Table) {
[protected] _table => 'posts'
[protected] _alias => 'Posts'
[protected] _connection => object(Cake\Database\Connection) {
[protected] _config => array(
'password' => '*****',
'login' => '*****',
'host' => '*****',
'database' => '*****',
'prefix' => '*****',
'persistent' => false,
'encoding' => 'utf8',
'name' => 'default',
'datasource' => object(Cake\Database\Driver\Mysql) {
[protected] _baseConfig => array(
'password' => '*****',
'login' => '*****',
'host' => '*****',
'database' => '*****',
'port' => '*****',
'persistent' => true,
'flags' => array(),
'encoding' => 'utf8',
'timezone' => null,
'init' => array(),
'dsn' => null
)
[protected] _config => array(
'password' => '*****',
'login' => '*****',
'host' => '*****',
'database' => '*****',
'port' => '*****',
'prefix' => '*****',
'persistent' => false,
'encoding' => 'utf8',
'name' => 'default',
'flags' => array(),
'timezone' => null,
'init' => array(),
'dsn' => null
)
[protected] _autoQuoting => false...
So as you can see it's a huge object and presumably the data is somewhere within it.
Apparently this because the data isn't directly returned in the new
ORM but an object...
It's not a bug it's a feature. ;) CakePHP3 returns a ResultSet object as you can see and entity objects for records. You'll have to work with these objects now instead of arrays.
I wounder if you really read the migration guide you've linked because it is all in there:
Cake\ORM\ResultSet - A collection of results that gives powerful tools for manipulating data in aggregate.
Cake\ORM\Entity - Represents a single row result. Makes accessing data and serializing to various formats a snap.
Further down on that page there is even more info about that. Take a look at the ResultSet API. You'll see that it implements Iterator, you can use it like an array:
Controller method:
public function index() {
$this->set('users', $this->Paginator->paginate($this->Users, [
'limit' => 5,
'conditions' => [
'Users.active' => 1
]
]));
}
There is a lot of documentation to read in the doc block of the paginate() method.
View index.ctp:
foreach ($users as $user) {
debug($user);
}
This will show you Entity objects. I'm not pasting the whole long debug output here, just a part of it.
object(Cake\ORM\Entity) {
[protected] _properties => array(
'password' => '*****',
'id' => '52892217-91ec-4e5d-a9f4-1b6cc0a8000a',
'username' => 'burzum',
'slug' => '',
// ...
To get something from the object back just do this:
echo $user->username;
The actual data is in the protected property Entity::$_properties and accessed by __get.
This will be in your controller.
public function index() {
$this->set('users', $this->paginate($this->Users));
$this->set('_serialize', ['users'])
}
This you can put in your action
Paginatore logic
In my controller I have two functions that pull all records from the db with a status = 4. In one function it works fine. I copied the find all statement from the working function:
$this->set('completed', $this->Topic->find('all', array('fields' => array(
'Topic.creator','Topic.link','Topic.id', 'Topic.topic_name', 'Topic.info', 'Topic.priority', 'Topic.user_id',
'Topic.completed','Topic.created', 'Topic.status','User.name','User.id','Topic.category','Topic.tags'),'conditions' => array(
'Topic.status' => 4))));
But in the new function the $completed array doesn't seem to exist. The debug statement is just a blank line. If I debug the sql log using debug($this->Topic->getDataSource()->getLog()); this is the returned array:
Array
(
[log] => Array
(
[0] => Array
(
[query] => SELECT `Topic`.`creator`, `Topic`.`link`, `Topic`.`id`, `Topic`.`topic_name`, `Topic`.`info`, `Topic`.`priority`, `Topic`.`user_id`, `Topic`.`completed`, `Topic`.`created`, `Topic`.`status`, `User`.`name`, `User`.`id`, `Topic`.`category`, `Topic`.`tags` FROM `topics` AS `Topic` LEFT JOIN `users` AS `User` ON (`Topic`.`user_id` = `User`.`id`) LEFT JOIN `events` AS `Event` ON (`Event`.`topic_id` = `Topic`.`id`) WHERE `Topic`.`status` = 4
[affected] => 9
[numRows] => 9
[took] => 0
)
)
[count] => 1
[time] => 0
)
The SQL statement in the log works. If I plug in into mySQL it produces results. And the affected and numRows field show the correct number of records. But the produced data isn't being set to variables. Any ideas would be greatly appreciated. My boss and I are stumped. Here are both complete functions:
public function dashboard(){
$this->set('completed', $this->Topic->find('all', array('fields' => array(
'Topic.creator','Topic.link','Topic.id', 'Topic.topic_name', 'Topic.info', 'Topic.priority', 'Topic.user_id',
'Topic.completed','Topic.created', 'Topic.status','User.name','User.id','Topic.category','Topic.tags'),'conditions' => array(
'Topic.status' => 4))));
$this->set('total_inprogress_release', $this->Topic->find('count', array('conditions' => array(
'OR' => array('status <>' => 4,'status <>' => 0),
'priority' => 4))));
$this->set('upcoming_events', $this->Topic->Event->find('all'));
$this->Topic->virtualFields['count'] = 'COUNT(*)';
$this->set('graph_data', $this->Topic->find('chart', array('fields' => array('status_txt', 'priority_txt', 'count'), 'group' => array('status', 'priority'),
'chart' => array(
'xaxisTitle' => 'Status',
'yaxisLabels' => array('Release', 'Company', 'News'),
'xaxisLabels' => array('Open','In Progress','Completed'),
'yaxis' => 'Topic.priority_txt',
'xaxis' => 'Topic.status_txt',
'data' => 'Topic.count'
))));
}
And here's the nonworking function:
public function completed(){
$foo = $this->Topic->find('all', array('fields' => array(
'Topic.creator','Topic.link','Topic.id', 'Topic.topic_name', 'Topic.info', 'Topic.priority', 'Topic.user_id',
'Topic.completed','Topic.created', 'Topic.status','User.name','User.id','Topic.category','Topic.tags'),'conditions' => array(
'Topic.status' => 4)));
debug($foo);
debug($this->Topic->getDataSource()->getLog());
}
After several hours of tracking the data until we figured out where it was being lost, we found that the problem was some of the data in the database. It was encoded and it was causing a while statement in the core cake files to reset when it encountered said data. The solution was to alter Config/database.php as such:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'login',
'password' => 'Password',
'database' => 'database',
'prefix' => '',
'encoding' => 'utf8',//this line allows the database to read utf8 data.
);
}
I think the relationships may be causing an issue here. Try this in the completed function and pr($foo) the results:
$foo = $this->Topic->find('all', array(
'fields' => array(
'Topic.creator',
'Topic.link',
'Topic.id',
'Topic.topic_name',
'Topic.info',
'Topic.priority',
'Topic.user_id',
'Topic.completed',
'Topic.created',
'Topic.status',
'User.name',
'User.id',
'Topic.category',
'Topic.tags'
),
'conditions' => array(
'Topic.status' => 4,
),
'joins' => array(
array(
'table' => 'users',
'alias' => 'User',
'type' => 'LEFT',
'conditions' => array(
'Topic.user_id = User.id'
),
),
),
'recursive' => -1,
));
I suspect you will get the results you are looking for.
currently i am using this
$big_test = array(
'username' => 'test',
'password' => 'test'
);
$test = json_encode($big_test);
echo $test;
the result is :
{"username":"test","password":"test"}
But I data to be wrap inside results,maybe liek this
{"Results":[{"username":"test","password":"test"}]}
and what about to create as result array :
$big_test = array( "Results" => array ( array(
'username' => 'test',
'password' => 'test'
)));
$test = json_encode($big_test);
echo $test;
you get same thing what you put there
Simple:
$array = array("Results" => array('username' => 'test','password' => 'test'));