How to connect CakePHP 3.4 with remote SQL database - sql

I need to connect the CakePHP 3.4 application with the SQL database. Already application has a connection with MySQL database, but need to connect to this SQL database for other purposes.

It's a simple no extra configuration.
But check that hosting/MySQL provider allows wildcard entry. mean host other than the server can connect to your MySQL, so confirm that wildcard/prefer domain is enabled.
in your config.php
'Datasources' => [
'default' => [
'className' => Connection::class,
'driver' => Mysql::class,
'persistent' => false,
'host' => 'yourhost.com',
'username' => 'username_of_host',
'password' => 'password_of_host',
'database' => '**************',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
.......................
]
],

Related

How to allow my user to use their own external SMTP in Yii2

I have an app where I want my user to be able to add their own SMTP server, whether by Mailgun, Amazon SES, etc.
If we're taking the mailgun as an example, right now my Mailgun is set up in config/web.php like so
'mailgun' => [
'class' => 'boundstate\mailgun\Mailer',
'key' => 'MYKEY',
'domain' => 'DOMAIN',
],
Then I use the following to compose an email
Yii::$app->mailgun->compose()->setFrom($FROM)
->setReplyTo($contest_creator_email)
->setTo($email)
->setSubject($subject_line)
->setTextBody($plaintext)
->setHtmlBody($htmlemail)
->send();
How would I make it so that my user could set-up his own key instead of using mine? Is there a way to do this?
You can do this by setting mailer configuration.
Before sending email you can set mailer configuration in your api/controller.
//Set config value dynamicaly
Yii::$app->set('mailer', [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'YourHostName',
'username' => 'UserName',
'password' => 'Password',
'port' => 'Port',
'encryption' => 'Encryption'
],
]);
And send mail as below.
Yii::$app->mailer->compose()->setFrom($FROM)
->setReplyTo($contest_creator_email)
->setTo($email)
->setSubject($subject_line)
->setTextBody($plaintext)
->setHtmlBody($htmlemail)
->send();

Yii2 Db Connection to another server

I am trying to connect a db from another server.
Is it possible to connect Yii2 db connection from one server to another server db?
In db config you have to specify IP address of your database in your dsn:
'dsn' => 'mysql:host=YOUR_IP_HERE;dbname=YOUR_DB_HERE',
And that's all.
It's possible that second server doesn't allow connections on 3306 port, so you have to allow it in iptables (linux servers).
It is very possible, even you can connect yii2 application with many different database server. You just need to add a bit in the configuration file as below:
'components' => [
'db_server1' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=DB_NAME;port=PORT_CONNECTION',
'username' => 'DB_USERNAME',
'password' => 'DB_PASSWORD',
'charset' => 'utf8',
],
'db_server2' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=OTHER_HOST;dbname=DB_NAME;port=PORT_CONNECTION',
'username' => 'DB_USERNAME',
'password' => 'DB_PASSWORD',
'charset' => 'utf8',
],];
Hope can help you
You have to configure another db connection in your web.php inside the config folder:
'db2'=>[
'class'=>'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbname',
'username' => 'root',
'password' => '',
'charset' => 'utf8'
],
and then you call db2 component.

How to set access rights for files uploaded to s3 server using fedemotta Yii2 extension?

I can properly upload files to my s3 bucket using fedemotta yii2-aws-sdk extension. I cannot access the files because I have to set access rights to each file I upload.
My config looks something like this:
'components' => [
'awssdk' => [
'class' => 'fedemotta\awssdk\AwsSdk',
'credentials' => [
'key' => 'ZXCV',
'secret' => 'zxcv',
],
'region' => 'us-east-1',
'version' => 'latest',
],
I found out that in the default yii2-file-upload extension, it can be done using setACL('public-read'). How do I do this in fedemotta extension?
Solved.
Just add 'ACL' => 'public-read', to your upload function in the model
$this->s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ACL' => 'public-read',
));`

How to print YII Logger without Yii::app()->end

I am using YII 1.0 logger but i am not able to print YII logs without Yii::app()->end.
Below is my test program :
$test = 123;
if($test){
Yii::log('Test', CLogger::LEVEL_INFO, "This is for testing");
}
Below is my configuration settings :
'log' => array(
'class' => 'CLogRouter',
'routes' => array(
array(
'class' => 'CFileLogRoute',
'levels' => 'trace, info, error, warning, vardump',
),
array(
'class' => 'CWebLogRoute',
'enabled' => YII_DEBUG,
'levels' => 'error, warning, trace, notice',
'categories' => 'application',
'showInFireBug' => true,
),
array(
'class'=>'CFileLogRoute',
'logFile'=>'custom.log',
'categories'=>'custom.*',
),
),
),
Is there something wrong in my code ?
Thanks in advance.
You may learn CLogger api for more useful information.
Yii::getLogger()->flush(true);
Also hope it helps:
http://www.yiiframework.com/forum/index.php/topic/8671-force-message-routing-before-the-end-of-the-application/
http://www.yiiframework.com/forum/index.php?/topic/8158-logger-and-flush-problem/
Messages can be logged using yii::log,
syntax:
Yii::log($message, $level, $category);
example:
Yii::log("This is for testing","error","custom");

Customize getting data from SugarCRM Custom Module by Query parameter in rest api

Hi I'm trying to get data from SugarCRM by REST api. It's fine with default module, but with custom module, I got a issue.
I used this example
And getting data from Contacts module which has last name = "abc" is ok with these code:
$get_entry_list_parameters = array(
'session' => $session_id,
'module_name' => 'Contacts',
'query' => "contacts.last_name='abc'",
'order_by' => "",
'offset' => '0',
'select_fields' => array(
'id'
),
'link_name_to_fields_array' => array(
),
'max_results' => '1',
'deleted' => '0',
'Favorites' => false,
);
When I tried to get data from our custom module (eg: Event Session inside Event package), so it's ok as well without Query param like these code
$get_entry_list_parameters = array(
'session' => $session_id,
'module_name' => 'event_event_session',
'order_by' => "",
'offset' => '0',
'select_fields' => array(
'id',
'name'
),
'link_name_to_fields_array' => array(
),
'max_results' => '3',
'deleted' => '0',
'Favorites' => false,
);
But after that I put Query param, I got no record
$get_entry_list_parameters = array(
'session' => $session_id,
'module_name' => 'event_event_session',
'query' => "event_event_session.name='def'",
'order_by' => "",
'offset' => '0',
'select_fields' => array(
'id',
'name'
),
'link_name_to_fields_array' => array(
),
'max_results' => '3',
'deleted' => '0',
'Favorites' => false,
);
I think the way I used to get data from custom module is not correct.
Anyone can help me resolve this issue. Thank you!
Johnny
Are you sure the table name for the custom module is "event_event_session"?
When we create any new custom module using module builder then it is asking for key name. So our module will become as key_modulename. So make sure you are passing the key name along with module name. So that way you can access the data of custom new module.