您的位置:首页 > 编程语言 > PHP开发

YiiFrameworkBlog开发向导:添加和更新文章

2011-12-01 09:39 281 查看


我们已经修改好了post model。还需要调整一下PostController控制器的视图(view)和动作(action)。在本节中我们首先完善controller中进行CRUD操作的权限。然后完善添加和更新操作。最后实现文章的预览。


操作权限

第一件事情便是完善访问权限,因为用Yiic工具生成的并不是我们需要的。我们如下修改/wwwroot/blog/protected/controllers/PostController.php文件中的accessRules()方法。

 
public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'list' and 'show' actions
            'actions'=>array('list', 'show'),
            'users'=>array('*'),
        ),
        array('allow', // allow authenticated users to perform any action
            'users'=>array('@'),
        ),
        array('deny',  // deny all users
            'users'=>array('*'),
        ),
    );
}

 
以上规则说明用户(包括访客的所有用户)可以访问list和show方法,验证用户(已登录的用户)可以访问所有方法。包括admin方法。其他情况一律禁止。注意是的是验证时会按照在这里的顺序进行。第一条规则不符合时会进行第二条的验证,如果还不符合则再往下验证,以此类推。


添加和更新操作的完善

添加和修改非常接近,他们都需要form表单,输入都需要验证,都需要保存。主要的不同在于更新操作需要预填充form表单,也就是根据原有的数据显示默认值。Yiic工具生成了/wwwroot/blog/protected/views/post/_form.php文件来提供添加或者更新时所需的HTML form。
我们首先修改_form.php。只保留我们需要的部分:标题、正文、状态和标签。其他的内容删除。其中状态提供下拉菜单让用户选择.修改原来的

 
<?php echo CHtml::activeTextField($post,'status'); ?>

 


 
<?php echo CHtml::activeDropDownList($post,'status',Post::model()->statusOptions); ?>

 

在上面的修改中,我们也可以使用 Post::model()->getStatusOptions()代替Post::model()->statusOptions来获取可用的状态选项。我们之所以可以使用后者是因为post组件允许我们以属性的方式读取getter方法。
然后修改post类,已让系统在数据保存到数据库前,自动的填充某些属性(如:创建时间、用户id)。我们重写了 beforeValidate()方法:

 
protected function beforeValidate($on)
{
    $parser=new CMarkdownParser;
    $this->contentDisplay=$parser->safeTransform($this->content);
    if($this->isNewRecord)
    {
        $this->createTime=$this->updateTime=time();
        $this->authorId=Yii::app()->user->id;
    }
    else
        $this->updateTime=time();
    return true;
}

 
在这个方法中我们用CMarkdownParser把content从Markdown格式转为HTML格式。并把结果保存到contentDisplay中。避免了我们显示一篇文章时重复的格式转换。如果文章是新增的则添加作者和创建时间,否则修改更新时间。当执行validate()或者save() 时,该方法会被自动调用。
因为我们希望把文章的标签保存到Tag表。我们修改Post类,使其在保存文章后自动调用。

 
protected function afterSave()
{
    if(!$this->isNewRecord)
        $this->dbConnection->createCommand(
            'DELETE FROM PostTag WHERE postId='.$this->id)->execute();
 
    foreach($this->getTagArray() as $name)
    {
        if(($tag=Tag::model()->findByAttributes(array('name'=>$name)))===null)
        {
            $tag=new Tag(array('name'=>$name));
            $tag->save();
        }
        $this->dbConnection->createCommand(
            "INSERT INTO PostTag (postId, tagId) VALUES ({$this->id},{$tag->id})")->execute();
    }
}
 
public function getTagArray()
{
    // break tag string into a set of tags
    return array_unique(
        preg_split('/\s*,\s*/',trim($this->tags),-1,PREG_SPLIT_NO_EMPTY)
    );
}

 
在上面的代码中我们先清除了PostTag表中相关的记录。然后将新的tag记录插入,如果不存在则建立之。


实现预览

除了以上修改之外我们还希望实现预览功能,让用户在保存前可以进行预览。首先我们修改_form.php 文件添加预览按钮和预览的展示。预览只有当用户点击了预览按钮并且所提交的内容没有错误的时候显示。

 
<?php if(isset($_POST['previewPost']) && !$post->hasErrors()): ?>
<h3>Preview</h3>
<div class="post">
  <div class="title"><?php echo CHtml::encode($post->title); ?></div>
  <div class="author">posted by <?php echo Yii::app()->user->name . ' on ' . date('F j, Y',$post->createTime); ?></div>
  <div class="content"><?php echo $post->contentDisplay; ?></div>
</div><!-- post preview -->
<?php endif; ?>

 

以上代码可以补加到_form.php,用来实现预览按钮,和预览的显示。注意补加的时候预览按钮一定要放在form中,否则不能提交表单的。也就是<?php if(isset($_POST['previewPost']) && !$post->hasErrors()): ?>一句,要放在_form.php中的<!-- yiiForm -->之前。
然后我们修改controller中的actionCreate() 和 actionUpdate() 方法来响应预览。下面显示的是actionCreate().actionUpdate()中与此相同。

 
public function actionCreate()
{
    $post=new Post;
    if(isset($_POST['Post']))
    {
        $post->attributes=$_POST['Post'];
        if(isset($_POST['previewPost']))
            $post->validate();
        else if(isset($_POST['submitPost']) && $post->save())
            $this->redirect(array('show','id'=>$post->id));
    }
    $this->render('create',array('post'=>$post));
}

 
如果点击了预览按钮我们调用 $post->validate()来验证输入是否合法。如果点击的保存按钮,我们调用$post->save()进行保存时也会自动的进行验证。如果保存成功我们跳转地址到一个新的建立文章的页面
原文地址:http://www.cnblogs.com/analyzer/tag/YII/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐