/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $question = new Question(); if (isset($_POST['Question'])) { $this->forcePostRequest(); $_POST = Yii::app()->input->stripClean($_POST); $question->attributes = $_POST['Question']; $question->content->populateByForm(); $question->post_type = "question"; if ($question->validate()) { $question->save(); if (isset($_POST['Tags'])) { // Split tag string into array $tags = explode(", ", $_POST['Tags']); foreach ($tags as $tag) { $tag = Tag::model()->firstOrCreate($tag); $question_tag = new QuestionTag(); $question_tag->question_id = $question->id; $question_tag->tag_id = $tag->id; $question_tag->save(); } } $this->redirect($this->createUrl('//questionanswer/question/view', array('id' => $question->id))); } } $this->render('create', array('model' => $question)); }
public function addTagsForUser($phrase, $userId) { // split phrase into individual tags $tags = Tag::splitPhrase($phrase . ' ' . sfConfig::get('app_permanent_tag')); // add tags foreach ($tags as $tag) { $questionTag = new QuestionTag(); $questionTag->setQuestionId($this->getId()); $questionTag->setUserId($userId); $questionTag->setTag($tag); try { $questionTag->save(); } catch (PropelException $e) { // duplicate tag for this user and question } } }
/** * Method to show the views for * asking a new question and actually * creating the new question */ public function actionNew_question() { error_reporting(E_ALL); ini_set("display_errors", 1); $model = new Question(); $model->created_by = Yii::app()->user->id; $model->post_type = "question"; if (isset($_POST['Question'])) { $model->attributes = $_POST['Question']; if ($model->validate()) { $model->save(); // Question has been saved, add the tags if (isset($_POST['Tags'])) { // Split tag string into array $tags = explode(", ", $_POST['Tags']); foreach ($tags as $tag) { $tag = Tag::model()->firstOrCreate($tag); $question_tag = new QuestionTag(); $question_tag->question_id = $model->id; $question_tag->tag_id = $tag->id; $question_tag->save(); } } else { // throw error(?) no tag provided } $this->redirect($this->createUrl('//questionanswer/main/view', array('id' => $model->id))); } } $this->render('new_question', array('model' => $model)); }