首页 > 后端开发 > php教程 > [我的PHP之旅] YII框架学习 03连接数据库(使用ActiveRecord)

[我的PHP之旅] YII框架学习 03连接数据库(使用ActiveRecord)

WBOY
发布: 2016-07-29 09:02:07
原创
820 人浏览过

随意准备一个数据表

<code><span><span>CREATE</span><span>TABLE</span><span>`entry`</span> (
    <span>`id`</span><span>INT</span>(<span>11</span>) <span>NOT</span><span>NULL</span> AUTO_INCREMENT,
    <span>`value`</span><span>VARCHAR</span>(<span>12</span>) <span>NULL</span><span>DEFAULT</span><span>NULL</span>,
    <span>PRIMARY</span><span>KEY</span> (<span>`id`</span>)
)
<span>COLLATE</span>=<span>'utf8_general_ci'</span>
ENGINE=InnoDB
AUTO_INCREMENT=<span>12</span>;</span></code>
登录后复制

配置Yii数据库链接

首先要知道ActiveRecord是利用PDO_MySQ来实现的,所以要确认自己的这个插件好用。 然后修改框架目录/config/db.php 如下:

<code><span>return</span> [
    <span>'class'</span> => <span>'yii\db\Connection'</span>,
    <span>'dsn'</span> => <span>'mysql:host=localhost;dbname=test'</span>,
    <span>'username'</span> => <span>'root'</span>,
    <span>'password'</span> => <span>'root'</span>,
    <span>'charset'</span> => <span>'utf8'</span>,
];</code>
登录后复制

创建活动记录对象(ActiveRecord)

<code><span>namespace</span><span>app</span>\<span>models</span>;
<span>use</span><span>yii</span>\<span>db</span>\<span>ActiveRecord</span>;
<span><span>class</span><span>Entry</span><span>extends</span><span>ActiveRecord</span> {</span>}</code>
登录后复制

这个类里面什么都不写,我们利用这个类告诉了Yii我们要从哪一张表中提取出数据。

简单的控制器

<code><span>namespace</span><span>app</span>\<span>controllers</span>;

<span>use</span><span>yii</span>\<span>web</span>\<span>Controller</span>;
<span>use</span><span>app</span>\<span>models</span>\<span>Entry</span>;
<span>use</span><span>yii</span>\<span>data</span>\<span>Pagination</span>;
<span><span>class</span><span>EntryController</span><span>extends</span><span>Controller</span> {</span><span>public</span><span><span>function</span><span>actionGetAll</span><span>()</span> {</span><span>$query</span> = Entry::find();
        <span>$pagination</span> = <span>new</span> Pagination([
                <span>'defaultPageSize'</span> => <span>5</span>,
                <span>'totalCount'</span> => <span>$query</span>->count(),
        ]);

        <span>$entries</span> = <span>$query</span>  ->  offset(<span>$pagination</span>->offset)
                                        ->  limit(<span>$pagination</span>->limit)
                                        ->  all();

        <span>return</span><span>$this</span>->render(<span>'showEntry'</span>, [
            <span>'entries'</span>           => <span>$entries</span>,
            <span>'pagination'</span>    => <span>$pagination</span>,
        ]);
    }

    <span>public</span><span><span>function</span><span>actionDelete</span><span>()</span> {</span>
        Entry::deleteAll(<span>'value=\'***\''</span>);

        <span>return</span><span>self</span>::actionGetAll();
    }

}</code>
登录后复制

代码中pagination是用来分页的,如果没有需求当然那可以不写。其中的删除函数写的好像不怎么正规。。。

创建视图

<code><span><?php </span><span>use</span><span>yii</span>\<span>helpers</span>\<span>Html</span>;
<span>use</span><span>yii</span>\<span>widgets</span>\<span>LinkPager</span>;
<span>?></span><h1>Entries : </h1>
<ul>
<span><?php </span><span>foreach</span> (<span>$entries</span><span>as</span><span>$entry</span>)  { <span>?></span>
    <li>
<span></span>= Html::encode(<span>"$entry->id : $entry->value"</span>) <span>?></span>
</li>
<span><?php </span> } <span>?></span>
</span></span>
</ul>

<span></span>= LinkPager::widget([
        <span>'pagination'</span> => <span>$pagination</span>,
]) <span>?></span></span></code>
登录后复制

结合LinkPager与Pagination,使用Yii完成了查询与分页。

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了[我的PHP之旅] YII框架学习 03连接数据库(使用ActiveRecord),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板