Problem Calling A Function In My Controller




public function actionMyAction()

{

  echo "Sample";

  $id = $_POST['idNo'];

  $x=$this->returnTest($id);

  if($x==1){

     do something...

  }

  

}


public functon returnTest($id)

{

  $x=0;

  $criteria = new CDbCriteria;

  $criteria->condition = 'idNo=$id';

  $data = MyModel::model()->findAll($criteria);

  foreach($data as $val)

  {

    if(...){ $x=1;}

    else $x=0;

  }

  return $x;

  

}



When I call the returnTest function my action doesnt peform anything as if

it stuck in an infinite loop…it doesnt even perform the echo "Sample"…

What might be the cause…is the CDbCriteria?

If you don’t use xdebug php extension. You can add log commands then watch the logs to see what’s happening.

http://www.yiiframework.com/doc/guide/1.1/en/topics.logging

I have no idea about the infinite loop, but the critera are not correct:


$criteria = new CDbCriteria;

$criteria->condition = 'idNo=:id';

$criteria->params=array(':id'=>$id)

$data = MyModel::model()->findAll($criteria);

Never embed a user input ($id = $_POST[‘idNo’]) in a query, it can lead to sql injections, always pass them as an array of params, they will be slashed.

You should probably post your actual code for the two methods. You could be omitting something important.

Also, I assume "functon" is just a typo in your forum post, not your actual code.

Here’s the actual code:




public function actionFetchregularload()

	{

		$criteria = new CDbCriteria;

		$criteria->limit = 15;

		$criteria->condition='semester = 2';

		$criteria->addSearchCondition('yrLvl',$_POST['yrlevel']);

		$idNo = $_POST['idNo'];

		$criteria->select = array('subjCode','subjDesc','lec','lab','units');

		$data = CompeProspectusA::model()->findAll($criteria);

		$arr = array();

		if(strlen($_POST['yrlevel'])>0)

		{

			foreach ($data as $item)

			{   

				$subjcode = $item->subjCode;

				$x = $this->returnTest($subjcode,$idNo);

				if($x==1)

				{	

					$arr[] = $item->subjCode.",".

							 $item->subjDesc.",".

						     $item->lec.",".

						     $item->lab.",".

						     $item->units;

				}

			}

		}

		$this->renderPartial('_table',array(

			'subjects'=>$arr,

			)

		);

	}


public function returnTest($subjcode,$idNo)

	{

		$allow = 0;

		$criteria = new CDbCriteria;

		$criteria->select = array($subjcode);

		$criteria->condition = 'idNo = :idNo';

		$criteria->params=array(':idNo'=>$idNo);

		$data = CpeAccomplishment::model()->findAll($criteria);

		foreach($data as $item)

		{

			if($item->$subjcode=='1')

			{

				$allow=1;

			}

		}

		return $allow;

	}		






                foreach($data as $item)

                {

                        if($item->$subjcode=='1')

                        {

                                $allow=1;

                        }

                }



Should that be:




                foreach($data as $item)

                {

                        if($item->subjcode=='1')

                        {

                                $allow=1;

                        }

                }



Note the $ before subjcode.

It might be worth littering your code with echo statements to determine what path the request is taking.

There’s no such column in my table named ‘subjcode’. The $subjcode contains the name of column to be selected so I tried ‘find’ instead of ‘findAll’ and removed foreach to reduce redundancy (there’s only one datum to be searched with a value of either ‘1’ or ‘0’) :




public function returnTest($subjcode,$idNo)

	{

		$allow = 0;

		$selection=strtolower($subjcode);

		$criteria = new CDbCriteria;

		$criteria->select = array($selection);

		$criteria->params=array(':idNo'=>$idNo);

		$criteria->condition = 'idNo = :idNo';

		$data = CpeAccomplishment::model()->find($criteria);

		if($data=='1')

		{

			$allow = 1;

		}

		return $allow;

	}		



But still it returns always 0;

I don’t quite understand what you’re attempting. Can you explain the purpose of this code? It’s pretty unusual to use a value from a column in one table to determine a column name in another table.

The table I want to search contains columns of different subjectcodes with the first column the ID numbers of the students (Serves as accomplishment table for students to all the subjects in a particular department with the value of ‘1’ if he/she already has completed the subject and ‘0’ otherwise. The $subjcode being passed from my action is the subjcode I want to select with the $idNo that contains the ID number of the student as condition.

I would imagine a better structure would be:

A subjects table with an ID and other relevant data.

An accomplishments table with user ID and subject ID.

This is a standard many-many table scenario. If a record exists for a specific student and a specific subject in the accomplishments table, the user has completed the subject. If no such record exists, the user has not.

My problem has been solved using this:




public function returnTest($subjcode,$idNo)

	{

		$allow = 0;

		$selection=strtolower($subjcode);

		$criteria = new CDbCriteria;

		$criteria->select = array($selection);

		$criteria->params=array(':idNo'=>$idNo);

		$criteria->condition = 'idNo = :idNo';

		$data = CpeAccomplishment::model()->findAll($criteria);

		foreach($data as $val)

		{

			if($val->$selection == '0')

			{

				$allow = 1;

			}

		}

		return $allow;

	}