Yii Framework Forum: Error Array To String Convertion - Yii Framework Forum

Jump to content

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

Error Array To String Convertion Rate Topic: -----

#1 User is offline   FresherInYII 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 27-February 13
  • Location:Sri-Lanka

Posted 25 March 2013 - 01:14 PM

Hello everyone,

I'm still a newbie in yii....

model
public function getSubjects($teacher_id){
$list= Yii::app()->db->createCommand('select s.subjectName from teacherteachsub t, subjects s where s.subjectId=t.subjectId and subjectTeacherId="'.$teacher_id.'"')->queryAll();
$rs=array();
foreach($list as $item){
$rs[]=$item['subjectName'];
}
return $rs;
}
This method will provide data to a drop down list in my view, giving all the subject names a teacher teaches

But i need to convert the selected drop down list value teachers selected subject to its respective subject code.

for that i had another function......

public function getSubjectId($subject_name){
$subject_id= Yii::app()->db->createCommand('select subjectId from subjects where subjectName="'.$subject_name.'"')->queryAll();
return $subject_id;
}

I tried to invoke this in the beforeSave() function with the current value of subject as the parameter....
like:-

$this->subject= $this->getSubjectId($this->subject);

I'm getting a error saying can not convert array to string..... but im returning only $subject_id which isnt an array... what am i doing wrong??

Thanks a lot for your time!

Cheerz! :)
0

#2 User is offline   Denit S. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 13-March 13
  • Location:Netherlands

Posted 26 March 2013 - 08:06 AM

Actually performing a queryAll() on your command, will return a resultset containings rows of data (in your case, 1 row, with only 1 column. You could use queryScalar, to get only that particalar value.

Otherwise you would have to do something like this:


public function getSubjectId($subject_name){
	$result = Yii::app()->db
		->createCommand('select subjectId from subjects where subjectName="'.$subject_name.'"')
		->queryAll();
	$subject_id = $result[0]['subjectId']; //gets the column subjectId from the first row of your dataset (assuming there is always first row)
	return $subject_id;
}



OR (basically the same, but better readability, and does some of the error handling for you in case of an empty resultset)


public function getSubjectId($subject_name){
	$subject_id = Yii::app()->db
		->createCommand('select subjectId from subjects where subjectName="'.$subject_name.'"')
		->queryScalar();
	
	return $subject_id;
}


Here are some tunes for you to enjoy! listen, listen, listen.
0

#3 User is offline   FresherInYII 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 27-February 13
  • Location:Sri-Lanka

Posted 27 March 2013 - 10:49 AM

View PostDenit S., on 26 March 2013 - 08:06 AM, said:

Actually performing a queryAll() on your command, will return a resultset containings rows of data (in your case, 1 row, with only 1 column. You could use queryScalar, to get only that particalar value.

Otherwise you would have to do something like this:


public function getSubjectId($subject_name){
	$result = Yii::app()->db
		->createCommand('select subjectId from subjects where subjectName="'.$subject_name.'"')
		->queryAll();
	$subject_id = $result[0]['subjectId']; //gets the column subjectId from the first row of your dataset (assuming there is always first row)
	return $subject_id;
}



OR (basically the same, but better readability, and does some of the error handling for you in case of an empty resultset)


public function getSubjectId($subject_name){
	$subject_id = Yii::app()->db
		->createCommand('select subjectId from subjects where subjectName="'.$subject_name.'"')
		->queryScalar();
	
	return $subject_id;
}




@Denit S. - Thanks a lot.. query scalar did work..!
0

#4 User is offline   Denit S. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 13-March 13
  • Location:Netherlands

Posted 28 March 2013 - 03:56 AM

View PostFresherInYII, on 27 March 2013 - 10:49 AM, said:

@Denit S. - Thanks a lot.. query scalar did work..!


You're welcome ;)

Also, try to avoid placing variables directly into queries! When these variables are going to contain user input you're exposing yourself to SQL Injection vulnerabilty. Using binded parameters is A LOT safer, and also really easy in Yii.



Example:


public function getSubjectId($subject_name){
        $subject_id = Yii::app()->db
                ->createCommand('select subjectId from subjects where subjectName = :subject_name')
                ->queryScalar(array(':subject_name' => $subject_name));
        
        return $subject_id;
}


Here are some tunes for you to enjoy! listen, listen, listen.
0

#5 User is offline   FresherInYII 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 27-February 13
  • Location:Sri-Lanka

Posted 28 March 2013 - 04:27 AM

View PostDenit S., on 28 March 2013 - 03:56 AM, said:

You're welcome ;)

Also, try to avoid placing variables directly into queries! When these variables are going to contain user input you're exposing yourself to SQL Injection vulnerabilty. Using binded parameters is A LOT safer, and also really easy in Yii.



Example:


public function getSubjectId($subject_name){
        $subject_id = Yii::app()->db
                ->createCommand('select subjectId from subjects where subjectName = :subject_name')
                ->queryScalar(array(':subject_name' => $subject_name));
        
        return $subject_id;
}




@Denit S.- i really didnt know that.. thanks a lot! cheerz!
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