Call to a member function on a non-object

I am new to Yii and haven’t been able to find a solution to the following error. Any help is much appreciated!

Fatal error: Call to a member function getClassName() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/comp/protected/modules/voting/widgets/AVoteButtons.php on line 153

My view




   <?php Yii::app()->getModule('AVotingModule');

    $this->widget('application.modules.voting.widgets.AVoteButtons', array(

      'model'=>$model,

    ));



My widget AVoteButtons.php




	/**

	 * Gets the upvote button for this votable model.

	 * @return string the HTML for the upvote button

	 */

	public function getUpvoteButton() {

		$htmlOptions = array("class" => $this->upvoteClass);

		$htmlOptions['id'] = $this->getId()."-upvote";

		$label = $this->upvoteLabel;

		if ($this->model->userHasVoted && $this->model->userVote->score == 1) {

			if (!isset($htmlOptions['class'])) {

				$htmlOptions['class'] = "";

			}

			$htmlOptions['class'] .= " active";

			$label = $this->upvotedLabel;

		}

		return CHtml::link($label, array("/voting/vote/up", "ownerModel" => $this->model->asa("AVotable")->getClassName(), "ownerId" => $this->model->asa("AVotable")->getId()),$htmlOptions);


	}



components/AVotable.php




	/**

	 * Gets the id of the object being moderated.

	 * @return integer the id of the object being moderated.

	 */

	public function getId() {

		return $this->owner->primaryKey;

	}


	/**

	 * Gets the name of the class that is being moderated.

	 * @return string the owner model class name

	 */

	public function getClassName() {

		return get_class($this->owner);

	}



I want to know whether AVtable.php is a behavior class.

Where have you attached the behavior (instance of Avotable) to ‘$this->model’.

Check whether the following is helpful, if AVotable.php extends CBehavior.




   /**

         * Gets the upvote button for this votable model.

         * @return string the HTML for the upvote button

         */

        public function getUpvoteButton() {

                $htmlOptions = array("class" => $this->upvoteClass);

                $htmlOptions['id'] = $this->getId()."-upvote";

                $label = $this->upvoteLabel;

                if ($this->model->userHasVoted && $this->model->userVote->score == 1) {

                        if (!isset($htmlOptions['class'])) {

                                $htmlOptions['class'] = "";

                        }

                        $htmlOptions['class'] .= " active";

                        $label = $this->upvotedLabel;

                }


//Create a behavior object.

$obj=new AVotable;


//Attach behavior

$this->model->attachBehavior('AVotable',$obj);

                return CHtml::link($label, array("/voting/vote/up", "ownerModel" => $this->model->asa("AVotable")->getClassName(), "ownerId" => $this->model->asa("AVotable")->getId()),$htmlOptions);



Thanks for the quick response.

modules/components/AVotable.php is a behavior class.




class AVotable extends CActiveRecordBehavior implements IAVotable {



With your suggestion now I get the following error:

My Stack Trace:




include(AVotable.php) [<a href='function.include'>function.include</a>]:failed to open stream: No such file or directory

(/Applications/XAMPP/yii/framework/YiiBase.php:418)

Stack trace:

#0

/Applications/XAMPP/xamppfiles/htdocs/comp/protected/modules/voting/widgets/AVoteButtons.php(155):

spl_autoload_call()

#1

/Applications/XAMPP/xamppfiles/htdocs/comp/protected/modules/voting/widgets/AVoteButtons.php(100):

AVoteButtons->getUpvoteButton()

#2 /Applications/XAMPP/yii/framework/web/CBaseController.php(174):

AVoteButtons->run()

#3

/Applications/XAMPP/xamppfiles/htdocs/comp/protected/views/property/view.php(42):

PropertyController->widget()



Please see source code of module: github dot com /phpnode/YiiBlocks/tree/master/voting

Thank you!

Seenivasan,

Your suggestion worked…I didn’t have my main configed :mellow:

Thanks again!