Tag Archives: Yii

Yii2 MySQL remote SSL connection

'db' => [
	'class' => 'yii\db\Connection',
	'dsn' => 'mysql:host=REMOTE.HOST.MYSQL;dbname=remote_db_name',
	'username' => 'remote_db_user',
	'password' => 'remote_db_password',
	'charset' => 'utf8',
	'attributes' => [
		PDO::MYSQL_ATTR_SSL_KEY => dirname(dirname(__DIR__)) . '/common/config/ssl/client-key.pem',
		PDO::MYSQL_ATTR_SSL_CERT => dirname(dirname(__DIR__)) . '/common/config/ssl/client-cert.pem',                
		PDO::MYSQL_ATTR_SSL_CA => dirname(dirname(__DIR__)) . '/common/config/ssl/server-ca.pem',                
		],
],

This is assuming that the certs are within the common folder of an Yii2 advanced template project.

It’s probably also a good idea to chmod the pem files to 600 and the ssl directory to 700 so that only the user

Where are the pem files?

/var/lib/mysql/*.pem

Yii CGridView Default Order / Yii ActiveRecord Default Sort

Disclaimer, I know next to nothing about Yii….

Today I had an issue with a Yii App and it’s default admin CRUD tables, and sorting fields, well everything that listed a record from the database was in the order it was inserted into the database and not in a human friendly alphabetical order.

It turns out it’s really easy to set a default so that everything that uses the Model, comes out in a sorted way. (Note for larger things, you probably don’t want every single query that hits the DB getting sorted, so you’d probably only apply the sorting as required, but for small things, this shouldn’t be an issue).

For this example I have a Model called ‘Contacts’.

Open up your model (mine is: /models/Contacts.php)

Above the function ‘ public static function model($className=__CLASS__)’

Add the following function (if it’s not present)


/*
* Let's Setup some defaults!
*/
public function defaultScope()
{
return array(
'order' => 'FirstName'
);
}

In the array of ‘order’ change the value to the name of the column that you want to sort by, for my example it was ‘FirstName’.

Save, upload, test, enjoy.

Like I said at the start, I know nothing about Yii, had I, I probably wouldn’t have spent 20 minutes googling it.

Resources: http://www.yiiframework.com/doc/guide/1.1/en/database.ar#default-scope