sqlsrv date time converstion

May 30th, 2011 § 0 comments § permalink

The PHP Sqlsrv driver for the Sql server 2008 will return dateTime as an Object by default.If you need to use it in PHP then you need to convert into string. The new sqlsrv driver is blessed with the option to convert dateTime to string. You just need to set “ReturnDatesAsStrings” option as TRUE.

$host = "192.168.1.1";

$connectionInfo = array(
"UID" => "db_user",
 "PWD" => "db_password",
 "Database" => "db_sample",
"ReturnDatesAsStrings" =>1
);

sqlsrv_connect($host, $connectionInfo);

Thatz it you have done it. Now the sqlsrv driver for PHP will return all the datetime fields as string.

install pear and php unit

March 18th, 2011 § 2 comments § permalink

Here is the step by step procedure to install pear and PHPUnit framework.
You can install pear either through browser or through command line. I highly recommend you to use command line since i failed twice in browser.

Update
Make sure you added a path to php.exe into the Environment variable(php) in windows.

 cd /path/to/php/
 php.exe path/to/go-pear.php
  • A wizard will take you to provide settings

IF the installation is success now its time to install the PHPUnit framework

  • run these commands
 pear channel-discover pear.phpunit.de
 pear channel-discover components.ez.no
 pear channel-discover pear.symfony-project.com
 pear install phpunit/PHPUnit 

cakePHP acl group level permission

March 2nd, 2011 § 2 comments § permalink

In this post i just want you to give an idea how you can set a permission on a group regardless of user. What is mean is consider the condition. In your system there are 4 types of users.Each users have different functionalities and  some actions can be used only by a certain user. Here there we are not setting any permission on the users and we are not considering a condition that we should give different permission to different users.All users in a group are equal they can access any action which have permission for that group.According to the ACL we need to create an ARO’s for each users for it. This post will tell you how can you achieve this without creating the ARO’s of users(Just creating the ARO’s of groups)

Considering you have created all the Aco’s and Aro’s of groups and already set permission of it.

Now when you try to check the permission it will deny access even though the group have permission.Itz because they will search for the user in the Aro’s table.

now here is the hack to get rid of this

create a component named customAuth and save it in controllers/components/custom_auth.php.Now paste the following code in it.

<?php
App::import('Component','Auth');
class CustomAuthComponent extends AuthComponent {

    public function isAuthorized($type = null, $object = null, $user = null) {

        $actions  = $this->__authType($type);
        if( $actions['type'] != 'actions' ){
            return parent::isAuthorized($type, $object, $user);
        }
        if (empty($user) && !$this->user()) {
            return false;
        } elseif (empty($user)) {
            $user = $this->user();
        }

        $group = array('model' => 'Group','foreign_key' =>$user['Login']['group_id']);
        $valid = $this->Acl->check($group, $this->action());
        return $valid;
    }
}
?>

This will override the `isAuthorised` method in the Auth component.
Don’t forget to use the below code in `AppController.php`

function beforeFilter()
{
$this->CustomAuth->actionPath = 'controllers/';
$this->CustomAuth->authorize = 'actions';
}

Have something to say? please leave it here.

A better for loop

February 18th, 2011 § 3 comments § permalink

A simple for loop in a better way

Our regular for loop

$arr = array(10,20,30,40);
$sum = 0;
for($i=0;$i<count($arr);$i++){
      $sum += $arr[$i];
}
echo $sum;

A better option

$arr = array(10,20,30,40);
$sum = 0;
$count = count($arr);
for($i=0;$i<$count;$i++){
       $sum += $arr[$i];
}
echo $sum;

U need even more better option??

$arr = array(10,20,30,40);
$sum = 0;
$count = count($arr);
for($i=$count;$i--;){
     $sum += $arr[$i];
}
echo $sum;

cakephp sqlsrv dbo

February 7th, 2011 § 6 comments § permalink

Are you using cakePHP with Sql server 2008?  Then you might be using new PHP driver for connecting it.Here is the dbo which supports the sqlsrv driver in cakePHP. you are free to download and use it. If you find any bug please let me know. If you are interested in fixing bugs please fork the project in github.

Download

Please leave your response

Thank you

Happy baking.

Get last date of this month in php

February 6th, 2011 § 3 comments § permalink

This tutorial helps you to find last date of the month. It’s really easy to find it with “date” and “strtotime” functions.

It is a simple as

Last date of this month

echo date("Y-m-d",strtotime("+1 month -1 second",strtotime(date("Y-m-1"))));

Last date of next month

echo date("Y-m-d",strtotime("+2 month -1 second",strtotime(date("Y-m-1"))));

Last date of previous month

echo date("Y-m-d",strtotime("-1 second",strtotime(date("Y-m-1"))));

The logic behind this is quite simple. Any way the first date of every month will be “1” .We only have a confusion on the last date ie., whether it will be 28,29,30 or 31.

So first we will get the first date of this month by `date(“Y-m-1”)`. For eg. which result us `2011-02-01`.With the help of `strtotime` function i can convert the date into Unix time stamp and with the help of the same function i can add 1 month to it. `strtotime(“+1 month”,strtotime(date(“Y-m-1”))));`. So now it return the Unix time stamp corresponding to  `2011-03-01`. Subtracting a second from this will result you the last date of this month.

Happy coding.