In my previous post, i have connection Yii with Oracle, then, when i move to my new office, they use sql server for database. It is almost the same as connection with oracle. Here we go,

  1. you must install sql client server, i use windows 7 and it need sql client server 2012. 
  2. download php_pdo_sqlsrv_54_nts.dll, php_pdo_sqlsrv_54_ts.dll, php_pdo_sqlsrv_53_nts.dll, php_pdo_sqlsrv_53_ts.dll. if u use php 5.4 then paste 54 or php 5.3 then paste 53 file. Then paste all the files into xampp\php\ext\, 
  3. then edit php.ini, like this : 
      extension=php_sqlsrv_54_ts.dll
      extension=php_pdo_sqlsrv_54_ts.dll


    4. restart apache..
    5. configure the yii files : protected\config\main.php like this :

             db'=>array(
            'connectionString' => 'sqlsrv:Server=LOCALHOST;Database=CPPermata',
            'username' => 'sa',
            'password' => 'sa1234',
            'charset' => 'GB2312',
        ),


then you can generate model by gii.. :D


I have a task to create company profile of company where i work. Then i decide using Yii framework to develop that. One of the fitur is news and it needs upload image to complete the news. I write in here just because i can open it again in the future. i realize that this way to upload is simpler than php non framework. here we go :

in view -> form, we have :

    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'news-form',
        'enableAjaxValidation' => false,

        'htmlOptions' => array('enctype' => 'multipart/form-data')
            ));
    ?>



        labelEx($model, 'newsImage'); ?>
       
        error($model, 'newsImage'); ?>
   

endWidget(); ?>

in models, we have :

class News extends CActiveRecord
{
    public $newsImage;


    //other function

    public function rules()
    {       
        return array(          
            array('newsId', 'numerical', 'integerOnly'=>true),
            array('newsTitle,  newsAuthor', 'length', 'max'=>200),
            array('newsImage', 'file', 'types'=>'jpg, gif, png'),
            array('user_in, user_up', 'length', 'max'=>8),
            array('newsDate, newsContent, date_in, date_up', 'safe'),
            array('newsId, newsTitle, newsDate, newsContent, newsImage, newsAuthor, user_in, date_in, user_up, date_up', 'safe', 'on'=>'search'),
        );
    }

    
    //other function
}

in controller we have save create example :

public function actionCreate() {
        $model = new News;
        if (isset($_POST['News'])) {
            $model->attributes = $_POST['News'];
            $model->newsImage = CUploadedFile::getInstance($model, 'newsImage');
            if ($model->save()) {
                $model->newsImage->saveAs(Yii::app()->basePath.'/../images/news/'.$model->newsImage);
                $this->redirect(array('view', 'id' => $model->newsId));
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }


I hope this help. I get reference from here :
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/


Sometimes when u have many filters in your searching data, then u need query in string format. It's avoid many 'if else' of field that u use to filter. I have a senior programmer who tell me how to write the sp. Here we go the example :

BEGINSET NOCOUNT ON;
declare @str NVarchar(4000) , @where varchar(2000), @UserId char(8)
select @UserId = 'U0000000', @where = '(m.DocumentKeywords like ''%og%'' or m.DocumentKeywords like ''%og%'') AND convert(datetime,m.DocumentCreatedDate,106) between ''01 Mar 2013'' and DATEADD(DAY, +1,''31 Mar 2013'')'
set @str=N'SELECT distinct m.DocumentId, m.DocumentName
FROM dbo.DocumentsAccess a inner join dbo.Documents m on a.DocumentId=m.DocumentId and m.stsrc <> ''D''
inner join Users u on m.DocumentCreatedById = u.UserId
left join Users um on m.DocumentModifiedById = um.UserId and u.stsrc <> ''D''
WHERE (a.Stsrc <> ''D'' and a.canAccess=''1'' and a.UserId = '''
+ @UserId + ''') and '

set @str=@str+'('+@where +')'
+ 'ORDER BY m.DocumentName '

exec sp_executesql @str
print @str

END Then the result will be :

SELECT distinct m.DocumentId, m.DocumentName
FROM dbo.DocumentsAccess a inner join dbo.Documents m on a.DocumentId=m.DocumentId and m.stsrc <> 'D'
inner join Users u on m.DocumentCreatedById = u.UserId
left join Users um on m.DocumentModifiedById = um.UserId and u.stsrc <> 'D'
WHERE (a.Stsrc <> 'D' and a.canAccess='1' and a.UserId = 'U0000000') and ((m.DocumentKeywords like '%og%' or m.DocumentKeywords like '%og%') AND convert(datetime,m.DocumentCreatedDate,106) between '01 Mar 2013' and DATEADD(DAY, +1,'31 Mar 2013'))ORDER BY m.DocumentName