Widget Creation Error

Hi all,

I have created a widget with the following code:




<?php

class DUserOverview extends CWidget

{

	public $companyID;


	public $_data;


	/**

	 * Renders the detail view.

	 * This is the main entry of the whole detail view rendering.

	 */

	public function run()

	{

		$_data = Users::model()->find('company='.$companyID);

		if(isset($_data))

			echo $_data->ID;

		else

			echo "nope";

	}




}

?>



When I try to use the widget with the following code:




<?php $this->widget('DUserOverview', array(

	'companyID'=>9,

)); ?>



I get the error: Undefined variable: companyID

even though I have set the variable above…

Any ideas would be great!

Thanks,

Owen

To access properties inside a class you need to use $this

so in your example inside run() it would be


$this->companyID

Thanks for the reply, unfortunately, I now have the line:


$_data = Users::model()->find('company='.$this->$companyID);

which gives the error: Undefined variable: companyID

The same applies if I try to access $_data by going $this->$_data

Thanks,

Owen


$this->companyID

Bah! Idiot reading abilities :) Thanks for the help/slap!!

Also, Isn’t it better to pass as a param?

$_data = Users::model()->find(‘company=:companyID’, array(’:companyID’ => $this->$companyID));

Only if it’s coming from “outside” (e.g. GET/POST parameter). If the value is generated in your code, parameter binding only adds unnecessary overhead.