Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8335769
  • 博文数量: 1413
  • 博客积分: 11128
  • 博客等级: 上将
  • 技术积分: 14685
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-13 10:03
个人简介

follow my heart...

文章分类

全部博文(1413)

文章存档

2013年(1)

2012年(5)

2011年(45)

2010年(176)

2009年(148)

2008年(190)

2007年(293)

2006年(555)

分类:

2010-01-02 00:19:13

Yii初看了一下,还是比较适合自己的。MVC,命令行接口,这些都是我所喜欢的。
昨天在初步的YII接触上,建立了最基本的YII程序框架。今天讲述更为重要的部
分,与数据库的交互部分。
1).设置与数据库的连接:
在WebRoot/testdrive/protected/config/main.php文件的components下面,输入
db部分。如:
return array(
......
'components'=>array(
......
'db'=>array(
'connectionString'=>'sqlite:protected/data/source.db',
),
),
......
);
-备注:如果在运行时,出现下面的内容:
CException
Description
Property "CWebApplication.db" is read only.
说明你将db部分没有放在components下面,我曾经不小心在这个地方犯过错。
-对于不同的数据库连接,可以参考如下部分:
* SQLite: sqlite:/path/to/dbfile
* MySQL: mysql:host=localhost;dbname=testdb
* PostgreSQL: pgsql:host=localhost;port=5432;dbname=testdb
* SQL Server: mssql:host=localhost;dbname=testdb
* Oracle: oci:dbname=//localhost:1521/testdb
例如:
array(
......
'components'=>array(
......
'db'=>array(
'class'=>'CDbConnection',
'connectionString'=>'mysql:host=localhost;dbname=testdb',
'username'=>'root',
'password'=>'password',
'emulatePrepare'=>true, // needed by some MySQL
installations
),
),
)
2).创建一个数据库,以sqlite3为
例:WebRoot/testdrive/protected/data/source.db.
建立示例表User,以如下sql语句:
CREATE TABLE User (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
username VARCHAR(12 8) NOT NULL,
password VARCHAR(12 8) NOT NULL,
email VARCHAR(12 8) NOT NULL
);
试验起见,将data目录及子目录设为777权限,如sudo chmod -R 777 data
3).CRUD操作
% cd WebRoot/testdrive
% sudo YiiRoot/framework/yiic shell,注意,因为我这边/var/www/html目录为
root权限,所以必须用sudo,否则在执行其中的生成程序的时候会失败,而且最坏
的一点是失败的时候没有提示。
Yii Interactive Tool v1.0
Please type 'help' for help. Type 'exit' to quit.
>> model User
generate User.php

The 'User' class has been successfully created in the following file:
D:\wwwroot\testdrive\protected\models\User.php

If you have a 'db' database connection, you can test it now with:
$model=User::model()->find();
print_r($model);

>> crud User
generate UserController.php
generate create.php
mkdir D:/wwwroot/testdrive/protected/views/user
generate update.php
generate list.php
generate show.php

Crud 'user' has been successfully created. You may access it via:

4).访问页面测试:

默认的登录用户名密码为admin/admin.
好了,齐活了,不同于较之前熟悉的django,他是根据数据库来生成数据逻辑。无
论如何,虽然这还是最初级的,已经算有点意思了。
可以参
考:,这
里有更详细的信息。

阅读(1721) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~