从一组不同的表创建活动流。

时间:2014-03-20 12:55:41

标签: php mysql yii

我有一个用户可以执行某些操作的网站:发布照片,发表评论,观看视频等。这些操作中的每一个都会导致在带有UNIX时间戳的MySQL表中创建一行 - PhotoPosts,Comments, VideoView等

我希望建立一个个人资料页面,用户可以在该页面上查看他们在网站上所做的一切。

我想知道,有没有办法查询我的数据库,从各个表中向我发送一系列不同的行?

2 个答案:

答案 0 :(得分:0)

你可以用Yii做到这一点,例如像这样

$user = Yii::app()->db->createCommand()
    ->select('id, username, profile')
    ->from('tbl_user u')
    ->join('tbl_profile p', 'u.id=p.user_id')
    ->where('id=:id', array(':id'=>$id))
    ->queryRow();

您可以在此处找到更多信息: http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder

答案 1 :(得分:0)

我已经完成了这个

    public function actionSearch() {
    $this->keyword = $_GET['keyword'];
    $pages = Yii::app()->db->createCommand()
            ->select('url as id, name, CONCAT_WS(" ", content) as text, "page" as type')
            //->select('id, name, content as text, "page1" as type')
            ->from('Page')
            ->where('content LIKE :keyword AND status="active"')
            ->getText();

    $events = Yii::app()->db->createCommand()
            ->select('date as id, name, CONCAT_WS(" ", teaser_text, text) as text, "event" as type')
            ->from('Event')
            ->where('CONCAT_WS(" ", teaser_text, text) LIKE :keyword AND status="active"')
            ->getText();

    $news = Yii::app()->db->createCommand()
            ->select('id, name, CONCAT_WS(" ", teaser_text, text) as text, "news" as type')
            ->from('News')
            ->where('CONCAT_WS(" ", teaser_text, text) LIKE :keyword AND status="active"')
            ->getText();

    $programmes = Yii::app()->db->createCommand()
            ->select('id, name, CONCAT_WS(" ", teaser_text, text) as text, "programme" as type')
            ->from('Programme')
            ->where('CONCAT_WS(" ", teaser_text, text) LIKE :keyword AND status="active"')
            ->getText();

    $union = $programmes . ' UNION (' . $events. ') UNION (' . $news. ') UNION (' . $pages . ')';
    $records = Yii::app()->db->createCommand($union)->bindValue(':keyword', '% ' . $this->keyword . ' %')->queryAll();


    $dataProvider=new CArrayDataProvider($records, array(
        'id'=>'id',
        'pagination'=>array(
            'pageSize'=>6,
        ),
    ));

    $this->render('search',array(
        'keyword'=>$this->keyword,
        'dataProvider'=>$dataProvider,
    ));
}

我的搜索似乎是

            <section class="search-results">
                <h1>Search Results - <?php echo $keyword?></h1>
<?php
$this->widget('zii.widgets.CListView', array(
    'id'=>'search',
    'dataProvider'=>$dataProvider,
    'itemView'=>'_search_view',
    'template'=>'{items} {pager}',
    'itemsTagName'=>'ul',
    'itemsCssClass'=>'results-list',
    'pager' => array(
        'header' => '',
        'maxButtonCount' => 10
    ), 
)); ?>
            </section><!-- end search-results -->

你明白了。我还为网站的另一部分实现了此http://www.yiiframework.com/extension/audittrail2/。它很酷,它收集了1个表中的所有动作。