Yii Framework Forum: Modify Resultset - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Modify Resultset Rate Topic: -----

#1 User is offline   yii_king 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 05-August 12

Posted 10 February 2013 - 06:46 AM

If I search for all cars added between 2012-09-24 and 2012-09-26 in my database, and there are only cars added 2012-09-24 and 2012-09-26, that's all which will be returned.

In cases when there are no cars added, I'd like to have the application add information. Since there are no cars added 2012-09-25, I'd like to modify the resultset to show data anyway.

Instead of this output result:
2012-09-24 - Audi A6
2012-09-26 - Audi A7

I want to show this:
2012-09-24 - Audi A6
2012-09-25 - No cars found this date
2012-09-26 - Audi A7

I'm trying to achieve it this way:

Quote

$carData = Yii::app()->db->createCommand($sql)->queryAll();

// Modify the resultset
if (!in_array("2012-09-25", $carData["date"]) {
array_push($carData, "date=>2012-09-25");
}

0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 10 February 2013 - 10:39 AM

Dear Friend

I created a model Car.(id(INT),name(VARCHAR),date_added(DATE)).

In the controller I declared a static method to increment a date by oneday.

public static function addOneDay($date)
    {
	$dateObject=new DateTime($date);
	$dateObject->add(new DateInterval('P1D'));
	$date=$dateObject->format('Y-m-d');
	return $date;
    }


In view
//getting the first date.
sql="SELECT name , date_added FROM car ORDER BY date_added ASC";
$db=Yii::app()->db;
$command=$db->createCommand($sql);
$firstDate=$row['date_added'];

//getting the all data.
$cars=$command->queryAll();

//getting the last date.
$sql="SELECT name , date_added FROM car ORDER BY date_added DESC";
$db=Yii::app()->db;
$command=$db->createCommand($sql);
$row=$command->queryRow();
$lastDate=$row['date_added'];

//making the loop
for($i=$firstDate;$i<=$lastDate;$i=TestController::addOneDay($i))
{   $noCars=true;
	foreach($cars as $car)
	{
		
		if($i==$car['date_added'])
		{
                 	echo $car['date_added'].": ".$car['name']."</br>";
		        $noCars=false;
		}
	}

    if($noCars)
		echo $i.": "."No cars added"."</br>";
}


At the outset, it looked very simple.
But I could not acheive it in a simple way.
But may be this can be done easily at database level playing with some sql.
I may miss some simple things.

Regards.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users