$this->id im Model leer?

Hi,

weiß jemand warum $this->id beim Aufruf eines Models leer sein kann?




 	public function addComment($oComment)

	{

		$oComment->flashcards_id = $this->id;

		return $oComment->save();

	}



When posting in the general section, please use english so other can understand your question/post.

Hi sorry, wrong forum

I thought I was in the german forum ;)

My problem is that “$this->id” is empty and I don’t know why


 -- FlashcardsController.php

protected function createComment($issue)

{

	$oComment = new FlashcardsComments();

	if(isset($_POST['FlashcardsComments']))

	{

		$oComment->attributes = $_POST['FlashcardsComments'];

		$oComment->rating = 2;

		Flashcards::model()->addComment($oComment);

	}

}






 	public function addComment($oComment)

	{

		$oComment->flashcards_id = $this->id;

		return $oComment->save();

	}



do you know why?

Sure, it’s empty because you are not assigning a value to it. Do you? You are writing “$this”, so it looks in the object itself (Flashcards). But where do you create this object and assign a value to $id?

Because your Flashcards is a new record. Did you mean to use a findByPk?


Flashcards::model()->addComment($oComment);

thanks for your answers

mmh, well:

  1. There is a created Flashcards with an ID in the database.

  2. The user open the flashcard and want to write a comment


 --FlashcardsController.php

public function actionView($id)

{

	$oFlashcard = Flashcards::model()->findByAttributes(array('url' => $id));


	$oCreateComment	= $this->createComment($oFlashcard);

	$oEntity			= $this->createFlashcardsEntity($oFlashcard);


	$oCreateComment->flashcards_id = $oFlashcard->id;

		

	$this->renderPartial('_commentsForm', array

	(

		'oFlashcard'	=>	$oFlashcard,

		'oCreateComment'=>	$oCreateComment,

		'oComments'		=>	$oFlashcard->flashcardsComments,

	));

	return;

}



I set here the $oFlashcard->id!

Now if the user is writing a comment I call this




protected function createComment($issue)

{

        $oComment = new FlashcardsComments();

        if(isset($_POST['FlashcardsComments']))

        {

                $oComment->attributes = $_POST['FlashcardsComments'];

                $oComment->rating = 2;

                Flashcards::model()->addComment($oComment);

        }

}



and then this




        public function addComment($oComment)

        {

                $oComment->flashcards_id = $this->id;

                return $oComment->save();

        }



First of all you should definitely revise your structure. The controller only controls, so it should give data to models and they should do all the work.

Take a look at the yii guide.

Anyway, you should modify your addComment function to:




public function addComment($oComment, $flashcardid)

        {

                $oComment->flashcards_id = $flashcardid;

                return $oComment->save();

        }



and call it that way:




Flashcards::model()->addComment($oComment, $issue->id);



However read the yii guide and improve your structure. :slight_smile: