PHP Save array to database in JSON format

If you’re using class, take advantage on using the setter and getter method.

class Example
{
 public function setDisaster($value)
 {
     //convert the array to json format
     $this->potential_disaster = json_encode($value);
 }
 
 public function getDisaster()
 {
     //convert json to array format
     return json_decode($this->potential_disaster);
 }

 public function save()
 {
    $model = new Example();
    $model->save();
 }
}

$obj = new Example();
//Save the array data
$obj->disaster = array(1,2,3);
$obj->save();

//Print the array data
print_r($obj->disaster);


Yii unit test for the console command

So you want to test your console command with PHPUnit..lets rock!

Let say i have this file MyCompareCommand.php in my commands folder:

class MyCompareCommand extends CConsoleCommand
{
public function compareMonth($currentDate, $compareWithDate, $selectedPeriod=3)
{
 $arrMonth = array(3, 6, 9, 12, 15);
 foreach($arrMonth as $val){
    if($val >= $selectedPeriod){
        $nextDate = JDateTime::getNextMonth($currentDate, $val);
        if($nextDate == $compareWithDate){
              return $val . ' months';
        }
     }
 }
 return false;
}

Then in my tests/unit folder, i’ll have MyCompareCommandTest.php with below code:

<?php
Yii::import('application.commands.*');
class HrmCommandTest extends CTestCase
{
 public function setUp()
 { 
      $commandName='MyTest';
      $CCRunner=new CConsoleCommandRunner();
      $this->api = new HrmCommand($commandName,$CCRunner);
 }
 
 public function tearDown()
 { 
      unset($this->api); 
 }
 
 public function testCompareMonth()
 {
 $this->assertEquals('3 months', $this->api->compareMonth('2015-03-08', '2015-06-08', 3));
 }
 
}

So run from your command line something like this

user@ubuntu:tests# ../../bin/phpunit unit/MyCompareCommandTest.php

 

Source Reference and thanks to Antonis:

http://www.yiiframework.com/forum/index.php/topic/29569-unit-testing-shell-cconsolecommands/

Yii 1.1.16 and PHPUnit 3.5.15 on Ubuntu 12.04 using Composer

Yii 1.1.16 works well with PHPUnit version  < 3.8.*, and this phpunit are install in your project root.

cd /var/www/yourYiiProject/

Create composer.json file

{
    "require-dev": {
        "phpunit/phpunit": "3.7.*",
        "phpunit/phpunit-selenium": ">=1.2",
        "phpunit/dbunit": ">=1.2",
        "phpunit/phpunit-story": "*",
        "phpunit/php-invoker": "*"
    },
    "autoload": {
        "psr-0": {"": "src"}
    },
    "config": {
        "bin-dir": "bin/"
    }
}

Install Composer
curl -sS https://getcomposer.org/installer | php
Run the Composer
./composer.phar install

Check your phpunit version
./bin/phpunit --version
Run the phpunit with your test code
user@jubuntu:#cd /var/www/yourYiiProject/protected/tests
user@jubuntu:tests# ../../bin/phpunit unit/MyUnitTest.php
Source References & thanks to Richard Walker:

http://stackoverflow.com/questions/22368623/php-warning-include-phpunit-extensions-story-testcase-php-failed-to-open-stream

http://blog.mediasuite.co.nz/tip-how-to-set-up-phpunit-for-yii-using-composer/

Yii/PHP Run Background Process

To run the php process in background like sending email, call this function.

public function runBackgroundProcess($message,$mail_type, $id )
{
//Classname Action
$cmd = "Esm sendMail";
//--parameter=value --parameter=value
 $cmd = $cmd ." --message=". $message . " --type=". $mail_type . " --id=". $id;
//get the yiic path
$cmd = "php -d memory_limit=128M " . Yii::getPathOfAlias("application.yiic") . " " . $cmd;
//the php path
$cmd = "/usr/bin/".$cmd;
//write to log file
 exec($cmd . " >> /home/hafiz/log/myfile-`date '+%Y-%m-%d'`.log 2>&1 &");
}

Yii:: CGridView – show relations HAS_MANY in gridview

 

Make sure you have the relations object:

public function relations()
 {
    return array(
         'example' => array(self::HAS_MANY, 'exTask', 'ex_id'),
   );
 }

 

Add function in your model file:

public static function extractObjData($objData) {
 $tmp = '
'; foreach ($objData as $obj) { $tmp = $tmp . $obj->name . '
'; }
 $tmp = $tmp.'
'; return $tmp; }

In your GridView columns:

array(
 'header' => 'PEOPLE',
 'value' => 'HumanModel::extractObjData($data->example)',
 'type' => 'raw',
 ),