At this point, if we had shares in our database, we'd be able to see them. So let's work on sharing new content! From within our controller, the action to handle sharing is simply going to be loading a new Share model and populating it. Have a look at the following code:
public function actionCreate() { $share = new Share; if (isset($_POST['Share'])) { $share->attributes = array( 'text' => $_POST['Share']['text'], 'reply_id' => isset($_POST['Share']['reply_id']) ? $_POST['Share']['reply_id'] : NULL, 'author_id' => Yii::app()->user->id ); // Share the content if ($share->save()) { $this->renderPartial('share', array('data' => $share)); Yii::app()->end(); } } throw new CHttpException(500, 'There was an error sharing your content'); }
Though, the real power behind sharing content happens in the beforeSave()
method of our Share model. From here, we...