Yii Framework Forum: Call to a member function on a non-object - Yii Framework Forum

Jump to content

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

Call to a member function on a non-object Rate Topic: -----

#1 User is offline   adoming3 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 31-July 12

Posted 07 August 2012 - 12:40 PM

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);
	}

0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 601
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 07 August 2012 - 02:48 PM

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);

0

#3 User is offline   adoming3 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 31-July 12

Posted 07 August 2012 - 04:57 PM

View Postseenivasan, on 07 August 2012 - 02:48 PM, said:

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!
0

#4 User is offline   adoming3 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 31-July 12

Posted 07 August 2012 - 06:47 PM

View Postadoming3, on 07 August 2012 - 04:57 PM, said:

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!
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