I'm using a small CI codebase to run a simple feature in a larger codebase (a mix of Zend and Syfmony).
That master system uses a bootstrap file to set various global variables that I've accessed by creating a library file. it all works great and I can access the parent system, functions and global vars within my CI app.
BUT... That parent system stores DB connection info in sever related override files (So if code is deployed on server test.server, then config file test.server is loaded, and all relevant vars are set to that server.
What I need to do is tell my DB config file to access those vars.
OR, in my model, set hostname, user and pass vars.
I've tried:
$this->db->hostname = GLOBAL_VAR_HOSTNAME;
but that is not working.
So can I pass any data from my library file into a config file, so I can set:
$db['default'] = array(
'hostname' => GLOBAL_VAR_HOSTNAME,
'username' => GLOBAL_VAR_USERNAME,
etc etc
Setting up all the DB info in the standard database config file as new servers are added all the time and they tech leads want to use the existing system for sharing vars.
I'm going for a coffee...
You can setup manual connections like this:
$this->load->database(array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'ci',
'dbdriver' => 'mysqli',
));
var_dump($this->db->get('posts')->result());
Or you can create another connection completly:
$database = $this->load->database(array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydb',
'dbdriver' => 'mysqli',
), TRUE);
var_dump($database->get('posts')->result());
Related
I have a CakePHP 3 app with shells that I run from crontab.
When I run the shells through crontab, it creates cache files owned by the user running the crontab, which is not the user that runs apache...
Sometimes when I run the crontab the cached models are owned by apache and the shell fails, sometimes when I visit a page the models are owned by ec2-user and the page fails...
I posted a question on github, https://github.com/cakephp/cakephp/issues/11265#issuecomment-333951638
I was told to modify the chmod option for the cache config, I tried the following but it didn't work...
/**
* Configure the cache adapters.
*/
'Cache' => [
'default' => [
'className' => 'File',
'path' => CACHE,
'url' => env('CACHE_DEFAULT_URL', null),
'chmod' => 777
],
Any ideas on how I can make the default file permissions 777 on the cake cache files?
I would suggest having the chron run as the correct user OR having the chron task change owner and keep permissions as set but if you really want to have it all as it is and just change the permissions then you can use the mask option which I assume is what they meant.
Cache Config Options
Set using the following:
'Cache' => [
'default' => [
'mask' => 0777,
// other config options
],
]
#KaffineAddict is correct but make sure you do not wrap the value of mask in quotes as this can cause the values to not give correct permissions.
'Cache' => [
'default' => [
'mask' => 0777,
// other config options
],
]
I am using, for the same problem, the code which is given in this question Yii2 Create Database Connection.
I realise the $config variable is no longer the one in the web.php file, from the "config" folder, and that he is changing $configin his Configuration::setConfig() function.
My question for those more experienced than me in Yii is what should I write
in the web.php file in the db field (or in the db.php file) to "create a database connection programmatically without using the config file" ?
in the function Configuration::setConfig() to properly configure the application?
I'm sorry if my question is not clear enough. Please ask for details in the comments if needed. Thank you!
You can define a new connection this way
$db = new yii\db\Connection([
'dsn' => 'mysql:host=localhost;dbname=example',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
]); and
$db->open();
After the DB connection is established, one can execute SQL statements
like the following eg: :
$command = $db->createCommand('SELECT * FROM post');
$posts = $command->queryAll(); or
$command = $connection->createCommand('UPDATE post SET status=1');
$command->execute();
you can look at this for doc and guide
http://www.yiiframework.com/doc-2.0/guide-db-dao.html
http://www.yiiframework.com/doc-2.0/yii-db-connection.html
In cakephp 3.0 what is the right way to configure Cache based on cake built-in caching engines?
I'm using PHP 5.6 with Zend OPcache And APCu.
Zend Opcache is a built in opcode cache, it isn't something you need to configure, APCu however will need to be compiled against your webserver and enabled with APC emulation
in config/app.php you need to enter the following:
// Using a fully namespaced name.
'long' => [
'className' => 'Cake\Cache\Engine\ApcEngine',
'duration' => '+1 week',
'probability' => 100,
'path' => CACHE . 'long' . DS,
]
You can create different configs for different cache times.
Then to cache an object you simply:
$data = Cache::remember('my_cache_key', function () {
return Service::expensiveCall();
});
Or use the functions Cache::read and Cache::write
What, if it exists, is the canonical way to use ZF2 with MS SQL Server on a non-Windows OS?
From what I can tell from the documentation, only the Sqlsrv driver is officially supported, which only works on the Windows platform.
In ZF1, we used the Pdo_Mssql adapter and specified the pdoType as dblib. I can't find any references to doing anything similar in ZF2.
Apparently there was a Pdo\Mssql driver some time ago which was removed during a major refactoring, but I don't see a currently documented way of using Pdo_Dblib.
According to the adapter documentation above, you can set the driver in the adapter config to Pdo=OtherPdoDriver, but there's no documented examples of this usage. Does that mean you can use Pdo=Pdo_Dblib or Pdo=dblib and it will work automagically?
I've found passing references to a PDO ODBC driver, which would be a usable alternative, but can't find any documentation or code references.
Install the php5-sybase. Make sure that freetds is installed. You should have a conf file under /etc/freetds called freetds.conf.
In your freetds.conf file you should setup your mssql connection like this:
[MyMsSqlServer]
host = symachine.domain.com
port = 5000
tds version = 5.0
Then, (using the ZF2 Albums Tutorial as an example here) you set up the adapter like this:
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'dblib:host=MyMsSqlServer;dbname=zf2tutorial',
'charset' => 'UTF-8',
'username' => 'username',
'password' => 'password',
'pdotype' => 'dblib',
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
See if this gets you anywhere and let me know. I had a bitch of a time getting this to work, and I am more then happy to help anyone else that is having issues with this!!!
You can use a pdo connection with pdotype set to dblib (with loaded pdo_dblib extension).
sqlsrv is only available for Windows.
Here you can see how freetds and unixodbc is configured to connect to a MS SQL Server.
http://featurebug.blogspot.de/2011/01/mac-os-x-php-zend-server-ce-freetds-and.html
UPDATE:
Here is an example how to use a connection string to connect:
$db = new Zend\Db\Adapter\Adapter(
array(
'driver' => 'Pdo',
'dsn' => 'dblib:host=mssql_freetds;',
'username' => 'mssql_username',
'password' => 'mssql_password',
)
);
Yes, you can use odbc to connect. It is not currently officially supported, but you can get it working fairly easily, with minimal modifications.
The first thing you need to do, is make sure you can connect using the basic PHP PDO functions. So, do something like this:
$resource = new PDO('odbc:driver=FreeTDS;dbname=MYDB;Server=127.0.0.1;Port=8090;UID=Testuser;PWD=testpass;', 'Testuser', 'testpass', array());
$s = $resource->prepare('SELECT * FROM TEST_TABLE');
$r=$s->execute();
var_dump($r);
You'll need to change the host, port, user, pass, table name etc. for your settings, but you can use this to make sure you have those details right. Once you know what you are doing to connect, you can access it with Zend, like this:
$configArray = array(
'driver' => 'pdo_odbc',
'driver_options' => array('driver' => 'FreeTDS'),
'platform' => 'Mssql',
'dbname' => 'MYDB',
'host' => '127.0.0.1',
'port' => 8090,
'user' => 'Testuser',
'pass' => 'testpass'
);
$adapter = new Zend\Db\Adapter\Adapter($configArray);
$q = new Zend\Db\Sql\Select();
$q->from('TEST_TABLE');
$sql = $q->getSqlString($adapter->platform);
$r = $adapter->query($sql);
echo $r->getSql()."\n";
$result = $r->execute();
while(($res = $result->next()) !== false){
print_r($res);
}
However this will not actually work, for two reasons. First, the platform - Zend currently doesn't have an mssql platform; it defaults to sql92, which will not work, because it quotes identifiers in "", when mssql needs them to be quoted with []. So you'll need to copy the Zend/Db/Adapter/Platform/Sql92.php as Mssql.php, and change it to use [ and ] instead of ".
Second, because the dsn for connection to odbc requires the driver field, which zend currently doesn't support. You'll need to open up the Driver/Pdo/Connection class and modify the connect function, to add $dsn[] = "driver={$options['driver']}"; and replace $dsn[] = "host={$hostname}"; with $dsn[] = "server={$hostname}"; when $pdoDriver = odbc.
Note that the other answers are posted by people who do not actually understand how the Zend 2 library works - if you specify a value for 'dsn', then the other fields are ignored. I have NO idea where they got the idea of 'pdotype', as that is not a field that appears anywhere in the code.
I want to log payment gateway errors in payment.log. So add this to bootstrap:
CakeLog::config('payment', array(
'engine' => 'FileLog',
'file' => 'payment',
));
and when a problem occurred:
CakeLog::write('payment', 'The is a problem!');
but, the above command will log This is a problem! in both payment.log and error.log, while log into first file is enough.
Also, if any other problems occurred in other controllers (like users), it will log into both files. while it should just log in error.log
(I mean payment errors should log into payment.log and any other problems should write into error.log)
Where's the mistake?
Thanks.
It looks like you are missing the scopes option in the config maybe?
CakeLog::config('payments', array(
'engine' => 'File',
'scopes' => array('payment', 'order')
));
Then you need to specify the scope where you want to write to:
CakeLog::write('warning', 'Stuff is broken here', 'payment');
It may take some fiddling to get it just right, but this should help.