In one of my CgridViews I need to put a column which shows the latest week (number and date) on which the person was active.
Now , I tried to find Yii’s tools for sql queries , but there weren’t the MAX option there(or did I miss them?) , thus I used a pure SQL:
public function latestWeek() //this function is for Person
{
$datalogin=//connection to DB is working well
$sql ="SELECT w.number,MAX(w.start_date)
FROM tbl_person_week t, tbl_week w
WHERE t.person_id=$this->id AND t.week_id=w.id";
$query = mysqli_query($datalogin, $sql);
return $query;
}
Problem with this is that the query is fine (I checked it on the server) but I get the error that an object is being sent back , instead of a string , but I found no working solution for this conversion.
they may be no MAX function in Yii querybuilder, but you can still run any custom query with plain DAO interfaces without need to setup your own database link…
just use:
$command = Yii::app()->db->createCommand( 'SELECT w.number,MAX(w.start_date) FROM tbl_person_week t, tbl_week w WHERE t.person_id=:person_id AND t.week_id=w.id' );
$result = $command->query( array( ':person_id'=>$this->id ) );
they may be no MAX function in Yii querybuilder, but you can still run any custom query with plain DAO interfaces without need to setup your own database link...
public function latestWeek() //this function is for Person
{
$datalogin=mysqli_connect("192.168.1.137", "volapp", "t2ru3s1Lv","vol_app");
$sql ="SELECT w.start_date
FROM tbl_person_week t, tbl_week w
WHERE t.person_id=$this->id AND t.week_id=w.id
ORDER BY w.start_date DESC";
$query = mysqli_query($datalogin, $sql);
$numrows = mysqli_fetch_array($query);
$n=$numrows[0];
return $n;
}
Ordering by DESC and taking the first value fom the array.
HOWEVER, I got another question now:
In addition to the start_date , I want to show another Week’s attribute - number , in the same column.