Redirect to the new post on creation

I’m creating a simple forum. Topics can range over many pages.

I want the user to be taken to their post when they submit a new one and not the start of the topic.

Is there a nice way of doing this in Yii?

Editing a post is done with the following code:

view:




echo CHtml::link('Edit post',array('post/update&id='.$data->post_id,'page'=>$_GET['Post_page']));

controller:




$this->redirect(array('topic/view','id'=>$model->post_topic_id,'Post_page'=>intval($_GET['page']),'#'=>'post'.$model->post_id)); 

but how about when they create a new post? The redirect needs to know the topicid, postid and page number… first two are simple but page number could be the page of the previous post or a new page.

Well after creating a post, you can calculate the page number by querying the database.

'k, done it!




$newPostID=$post->primaryKey; 

$topicID=$post->post_topic_id;

$postCount=$topic->postCount;

$postPage=ceil($postCount/10);

$this->redirect(array('topic/view','id'=>$topicID,'Post_page'=>$postPage,'#'=>'post'.$newPostID));



(postCount is a relation in the topic model - ‘postCount’ => array(self::STAT, ‘Post’, ‘post_topic_id’))

Could not get getLastInsertID() to work for some reason. Wondered if there was a way to use one of the pager classes but the above is concise enough for me.

I also only want the new post form to show at the end of the thread on the last page. This is how I achieved this:




$pager['pages']=$dataProvider->getPagination();

$pageCount = $pager['pages']->getPageCount();

$pageCurrent = $pager['pages']->getCurrentPage();

if ($pageCount == $pageCurrent+1){

	echo "<h4>Reply to this Topic</h4>";

	$this->renderPartial('/post/_form',array(

		'model'=>$post,

	         )); 

	}