Home > php教程 > php手册 > Daject初探

Daject初探

WBOY
Release: 2016-06-13 09:41:31
Original
789 people have browsed it

Daject简介

Daject是用

github地址:https://github.com/kasonyang/Daject

如何Daject

假定数据库里有一张名为user的表,字段为id,name,age,主键为id

首先,我们先将Daject引入

<span include</span> 'Daject/Daject.php';
Copy after login

然后,我们创建一个Table模型

<span class</span> UserTable <span extends</span><span  DajectTableBase{
    </span><span protected</span> <span $keys</span> = <span array</span>('id'<span );
}</span>
Copy after login

注意类的名称,命名规则为:表名+Table

为了能够正常使用Daject查询数据,我们需要先对Daject进行初始化。

<span //</span><span 新增一个数据库</span>
DajectConfig::addDatabase('master', 'mysql', 'localhost', 'root', 'your_password', 'your_database_name', 'utf8'<span );
</span><span //</span><span 设置当前数据库</span>
DajectConfig::setDatabase('master', 'master'<span );
</span><span //</span><span 设置当前数据库前缀</span>
DajectConfig::setTablePrefix('');
Copy after login

接下来,我们就可以对数据库进行CURD操作了

<span //</span><span 实例化UserTable</span>
<span $table</span> = <span new</span><span  UserTable();
</span><span //</span><span 读取user表的前10条记录</span>
<span $data</span> = <span $table</span>->select(10<span );
</span><span foreach</span>(<span $data</span> <span as</span> <span $d</span><span ){
    </span><span echo</span> 'id:',<span $d</span>['id'],';name:',<span $d</span>['name'<span ];
}

</span><span //</span><span 插入一条记录</span>
<span $table</span>->insert(<span array</span>('name' => '张三','age' => 20<span ));

</span><span //</span><span 增加where条件</span>
<span $table</span>->where(<span array</span>('name'=>'张三'<span ));

</span><span //</span><span 更新数据</span>
<span $table</span>->update(<span array</span>('age'=>25<span ));

</span><span //</span><span 删除数据</span>
<span $table</span>->delete();
Copy after login

注意看上面的代码,整个代码中并未出现任何SQL语句,但是我们已经完成了常见的select,insert,update,delete操作。事实上,当我们访问select(),insert(array),update(array)和delete方法时,Daject会自动为我们生成SQL并执行,然后返回数据给我们。由于不用写SQL,大大的减少了我们在写SQL上浪费的时间,同时降低了SQL语法错误的概率。

当然,上面只是Daject的一些基本操作,还有更多的关于Daject的知识,我会在以后的文章给大家慢慢分享!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template