Note that there are some explanatory texts on larger screens.

plurals
  1. POYii - Retrieve Model Relation Current ID from Controller / Model
    text
    copied!<p>In Yii I have a view that displays a question, that questions associated answers, and each answers associated comments.</p> <p>My question is: how do you retrieve a model's relation's current id from a controller/model without navigating to a view specific to that relation?</p> <p>More details:</p> <p>The relation's foreach loops are each setup and the data is being displayed correctly without a problem.</p> <p>There is one view with many nested 'renderPartial' views but the one question and all associated answers/comments are all displayed on the same page.</p> <p>Each answer has a database field question_id that is the FK to the question's PK. The code I am using to set the correct model object to be saved and the correct current question_id is: Function in QuestionController.php:</p> <pre><code>/** * creates answer model and if answer is being submitted via post, saves the answer data * @protected * @return object answer object being saved */ protected function createAnswer($question) { $answer = new Answer(); if(isset($_POST['Answer'])) { $answer-&gt;attributes = $_POST['Answer']; if($question-&gt;addAnswer($answer)) { Yii::app()-&gt;user-&gt;setFlash('answerSubmitted', "Thank you for your answer!"); $this-&gt;refresh(); } } return $answer; } </code></pre> <p>This function is called in QuestionController.ActionView:</p> <pre><code> /** * Displays a particular model. */ public function actionView() { //load question model $question = $this-&gt;loadModel(); //load associated answers for question $answer = $this-&gt;createAnswer($question); //load associated comments for answers $comments = $this-&gt;createComment($question-&gt;answer); $this-&gt;render('view',array( 'model'=&gt;$question, 'answer'=&gt;$answer, 'comments'=&gt;$comments, )); } </code></pre> <p>This works great, and the createAnswer function calls the following function in the Question.php model:</p> <pre><code> /** * Adds an answer to this question * @param model answer the answer model * @return model saved answer with the current question's id */ public function addAnswer($answer) { $answer-&gt;question_id = $this-&gt;id; $answer-&gt;status = param('answerStatus'); return $answer-&gt;save(); } </code></pre> <p>The code above sets the current question's id and it works great.</p> <p>The problem I have is setting the correct answer_id for the comment's answer. I am trying to use the following code in the QuestionController:</p> <pre><code> /** * @protected * @return object comment object being saved * Saves comment for this particular answer */ protected function createComment($answer) { $comment = new AnswerComment; if(isset($_POST['AnswerComment'])) { $comment-&gt;attributes = $_POST['AnswerComment']; if($answer-&gt;addComment($comment)) { //Yii::app()-&gt;user-&gt;setFlash('commentSubmitted', $answer-&gt;answer-&gt;id . 't'); Yii::app()-&gt;user-&gt;setFlash('commentSubmitted', "Your comment has been added."); $this-&gt;refresh(); } } return $comment; } </code></pre> <p>and calling the function in the Answer model:</p> <pre><code> /** * Adds a comment to this answer * @param model answer the answer model * @return model saved answer with the current question's id */ public function addComment($comment) { $comment-&gt;answer_id =$this-&gt;id; $comment-&gt;status = param('answerCommentStatus'); return $comment-&gt;save(); } </code></pre> <p>This however does not work. The <code>$this-&gt;id</code> is evidently not set. I believe this is possibly because it is a relation of the main model and not the main model itself. How can I retrieve the current answer's id without going to a view specific to that answer i.e. /answer/view/1 ,etc.</p> <p>(as an example think of how StackOverflow allows you to comment on different answers without leaving the question's page)</p> <p>Thank you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload