Yii Framework Forum: Getattributes Of A Relational Query - Yii Framework Forum

Jump to content

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

Getattributes Of A Relational Query I have a problem with null values in relational model. Rate Topic: -----

#1 User is offline   Fedek6 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 25-May 13

Posted 25 July 2013 - 10:15 AM

Hi there,
I'm a newcomer in the Yii society. But back to the problem. I'm trying to encode a relational model into json data.

Here is a relation declaration:
'weekdays' => array(self::HAS_MANY, 'StudentAvabilityWeekdays', array('avability_id'=>'id'), 'select'=>'week_day, begin_time1, end_time1'),


That's how I'm trying to query database:
			$criteria=new CDbCriteria();
			
			
			$criteria->addInCondition('student_id',array(intval($id))); 
			$criteria->addCondition('end_date=(SELECT MAX(end_date) FROM t_student_avability WHERE student_id=:ycp0)');
			$criteria->select = array('begin_date', 'end_date');
			
			$model = StudentAvability::model();
			
			$data = $model->with('weekdays')->findAll($criteria);
			$weekDays = array();
			
			if($data !== NULL) {
				foreach($data as $obj)
					$weekDays[] = $obj->weekdays;
			} 
			
			echo CJSON::encode($weekDays);

And that's what I get:
[[{"week_day":"0","begin_time1":"10:00:00","end_time1":"18:00:00","id":"2171","avability_id":null,"is_time1":null,"is_time2":null,"begin_time2":null,"end_time2":null,"create_datetime":null,"update_datetime":null,"is_deleted":null},{"week_day":"1","begin_time1":"10:00:00","end_time1":"18:00:00","id":"2172","avability_id":null,"is_time1":null,"is_time2":null,"begin_time2":null,"end_time2":null,"create_datetime":null,"update_datetime":null,"is_deleted":null},{"week_day":"2","begin_time1":"10:00:00","end_time1":"18:00:00","id":"2173","avability_id":null,"is_time1":null,"is_time2":null,"begin_time2":null,"end_time2":null,"create_datetime":null,"update_datetime":null,"is_deleted":null},{"week_day":"3","begin_time1":"10:00:00","end_time1":"18:00:00","id":"2174","avability_id":null,"is_time1":null,"is_time2":null,"begin_time2":null,"end_time2":null,"create_datetime":null,"update_datetime":null,"is_deleted":null},{"week_day":"4","begin_time1":"10:00:00","end_time1":"18:00:00","id":"2175","avability_id":null,"is_time1":null,"is_time2":null,"begin_time2":null,"end_time2":null,"create_datetime":null,"update_datetime":null,"is_deleted":null},{"week_day":"5","begin_time1":"10:00:00","end_time1":"20:00:00","id":"2176","avability_id":null,"is_time1":null,"is_time2":null,"begin_time2":null,"end_time2":null,"create_datetime":null,"update_datetime":null,"is_deleted":null},{"week_day":"6","begin_time1":"10:00:00","end_time1":"20:00:00","id":"2177","avability_id":null,"is_time1":null,"is_time2":null,"begin_time2":null,"end_time2":null,"create_datetime":null,"update_datetime":null,"is_deleted":null}]]


As you can see there are empty columns with null values which I want to filter from the result. What is propper technique to accomplish this task?
0

#2 Guest_zqrt_*

  • Group: Guests

Posted 25 July 2013 - 07:10 PM

try this :

 $criteria=new CDbCriteria();
                        
                        
                        $criteria->addInCondition('student_id',array(intval($id))); 
                        $criteria->addCondition('end_date=(SELECT MAX(end_date) FROM t_student_avability WHERE student_id=:ycp0)');
                        $criteria->select = array('begin_date', 'end_date');
                        
                        $model = StudentAvability::model();
                        
                        $data = $model->with('weekdays')->findAll($criteria);
                        $weekDays = array();
                        
                        if($data !== NULL) {
                                foreach($data as $obj)
                                        $weekDays[] = $obj->weekdays;
                        } 

                         $weekDays = array_filter($weekDays);
                        // or
                        //$weekDays = array_filter($weekDays, 'strlen');

                        echo CJSON::encode($weekDays);

0

#3 User is offline   Fedek6 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 25-May 13

Posted 26 July 2013 - 04:07 AM

Thanks. I've found other solution. Maybe it will help anyone with the same NULL problem. You just need to iterate through array of objects:

			$criteria=new CDbCriteria();
			
			
			$criteria->addInCondition('student_id',array(intval($id))); 
			$criteria->addCondition('end_date=(SELECT MAX(end_date) FROM t_student_avability WHERE student_id=:ycp0)');
			// $criteria->select = false;
			
			$model = StudentAvability::model();
			
			$data = $model->with('weekdays')->findAll($criteria);
			$toEncode = array();
			
			if($data !== NULL) {
				foreach($data as $row) {
					foreach($row->weekdays as $weekday) {
						$toEncode[] = $weekday->getAttributes(array('week_day', 'begin_time1', 'end_time1'));
					}
				}
					
			} 
			
			header('Content-type: application/json');
			echo CJSON::encode($toEncode);

And the result is:
[{"week_day":"0","begin_time1":"10:00:00","end_time1":"18:00:00"},{"week_day":"1","begin_time1":"10:00:00","end_time1":"18:00:00"},{"week_day":"2","begin_time1":"10:00:00","end_time1":"18:00:00"},{"week_day":"3","begin_time1":"10:00:00","end_time1":"18:00:00"},{"week_day":"4","begin_time1":"10:00:00","end_time1":"18:00:00"},{"week_day":"5","begin_time1":"10:00:00","end_time1":"20:00:00"},{"week_day":"6","begin_time1":"10:00:00","end_time1":"20:00:00"}]

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