Yii Framework Forum: Yii Query Not Working+Array Result - Yii Framework Forum

Jump to content

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

Yii Query Not Working+Array Result Rate Topic: -----

#1 User is offline   MarkBrass 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 130
  • Joined: 13-November 13

Posted 10 April 2014 - 11:11 AM

I have 3 models: Person , Event , Person_Event (which is a connection table , thus has fields like person_id and event_id)

I'm writing a mail action which gets the ids of selected events via ajax , and for each event , goes to the connection table , finds all the people who are participating , and sends them the info about their event.

I'v tested the code below in parts , and all is working except 2 things:
1. The sql command is not working , though the query itself is right (checked in phpmyadmin)
2. Are the rows being put into an array? because I'm doing a second "foreach" for people

public function actionSender()
	{
		 if(Yii::app()->request->getIsAjaxRequest())
        { 
                $checkedIds=$_GET['checked'];            //the selected events ids
                foreach($checkedIds as $id)
				{					
					$people = Yii::app()->db->createCommand()
						->select('person_id')
						->from('tbl_person_event')					
						->where('event_id = :id', array(':id'=>$id))
						->queryRow();
						
						foreach($people as $pid)
						{
							$event=Event::model()->findByPk($id);           //for putting event's attributes in the thml table
							$address=Person::model()->findByPk($pid)->email;							
								$subject='not set yet';
							$message = '
								<html>
						                                      //the html table I'm sending to the people
								</html>
								';
								$headers  = 'MIME-Version: 1.0' . "\r\n";
								$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			
							mail($address, $subject, $message,$headers);
						}
				}

        }
	}


Now , all the "people" in the app have my email address , but I get none.
What is wrong with the query? And how can I save people ids into an array?

Thanks , Mark.
0

#2 User is offline   Pecos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 21-March 14

Posted 11 April 2014 - 07:26 AM

I try to understand what you are doing with the second foreach. You will only fetch one row with queryRow. I guess it will return an associative array of table fields (not rows).
0

#3 User is offline   MarkBrass 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 130
  • Joined: 13-November 13

Posted 11 April 2014 - 12:34 PM

In the first foreach I take an Event's id so hat I'll be able to send its info to the people.

In second foreach I find all the people who are participating that specific event , and for each id I get the email address.
0

#4 User is offline   Pecos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 21-March 14

Posted 11 April 2014 - 02:37 PM

View PostMarkBrass, on 11 April 2014 - 12:34 PM, said:

In the first foreach I take an Event's id so hat I'll be able to send its info to the people.

In second foreach I find all the people who are participating that specific event , and for each id I get the email address.


Yes but queryRow will return the last row from tbl_person_event. Maybe queryAll is what you're looking for.

$people = Yii::app()->db->createCommand()
    ->select('person_id')
    ->from('tbl_person_event')                                      
     ->where('event_id = :id', array(':id'=>$id))
     ->queryAll();

  foreach($people as $row)
  {
       $event=Event::model()->findByPk($id);  //for putting event's attributes in the thml table
       $address=Person::model()->findByPk($row['person_id'])->email;                                                        
       $subject='not set yet';
     ...
  }


Not tested. Hope it leads you in the right direction :)
0

#5 User is offline   MarkBrass 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 130
  • Joined: 13-November 13

Posted 12 April 2014 - 02:47 AM

Maybe queryAll is what you're looking for.


Tried that as well but with no success.

I re-wrote this function with the use of criteria instead of query builder. Not as elegant as this , but working.
0

#6 User is offline   JamesBarnsley 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 420
  • Joined: 18-November 10
  • Location:UK

Posted 13 April 2014 - 12:12 PM

View PostMarkBrass, on 12 April 2014 - 02:47 AM, said:

Maybe queryAll is what you're looking for.


Tried that as well but with no success.

I re-wrote this function with the use of criteria instead of query builder. Not as elegant as this , but working.


Looking at your code in the first example you showed. It looked to me like your ' or " might not have been in the right place because the part of the code where the "mail()" function is, is in green and that means its commented out right or contained in a string or something. Either way it is not the correct color the function call should be.
0

#7 User is offline   JamesBarnsley 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 420
  • Joined: 18-November 10
  • Location:UK

Posted 13 April 2014 - 12:18 PM

View PostMarkBrass, on 12 April 2014 - 02:47 AM, said:

Maybe queryAll is what you're looking for.


Tried that as well but with no success.

I re-wrote this function with the use of criteria instead of query builder. Not as elegant as this , but working.


Also I would to mention, it was not just a case of the queryAll method, I think you missed the array part in the example Pecos showed you ...

$people = Yii::app()->db->createCommand()
    ->select('person_id')
    ->from('tbl_person_event')                                      
    ->where('event_id = :id', array(':id'=>$id))
    ->queryAll();


I am sure $people in this instance will be a multi dimensional array and not a single dimensional array and you using it in your foreach loop like this ...

foreach($people as $pid)


But it needs to be ...

foreach($people as $row)


And then to get the ID you need to specify $row["person_id"] etc.

James.
0

#8 User is offline   MarkBrass 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 130
  • Joined: 13-November 13

Posted 14 April 2014 - 01:44 AM

I see. I'll have to use it again in the future so I take this in mind.

Thanks, Mark.
0

#9 User is offline   JamesBarnsley 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 420
  • Joined: 18-November 10
  • Location:UK

Posted 18 April 2014 - 03:55 PM

View PostMarkBrass, on 14 April 2014 - 01:44 AM, said:

I see. I'll have to use it again in the future so I take this in mind.

Thanks, Mark.


Ok thanks.
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