首先建立数据库cake_ext,并执行如下sql文:
-
CREATE TABLE `companies` (
-
`id` int(11) NOT NULL auto_increment,
-
`company` varchar(50) NOT NULL,
-
`price` decimal(8,2) NOT NULL,
-
`change` decimal(8,2) NOT NULL,
-
`lastudp` date NOT NULL,
-
PRIMARY KEY (`id`)
-
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-
-
-- ----------------------------
-
-- Records
-
-- ----------------------------
-
INSERT INTO `companies` VALUES ('1', '3m Co', '71.72', '0.02', '2008-10-21');
-
INSERT INTO `companies` VALUES ('2', 'Alcoa Inc', '29.01', '0.42', '2008-10-20');
-
INSERT INTO `companies` VALUES ('3', 'AT&T Inc.', '31.61', '-0.48', '2008-10-21');
-
INSERT INTO `companies` VALUES ('4', 'Boeing Co.', '75.43', '0.53', '2008-10-13');
-
INSERT INTO `companies` VALUES ('5', 'United Technologies Corporation', '63.26', '0.55', '2008-10-09');
-
INSERT INTO `companies` VALUES ('6', 'Intel Corporation', '19.88', '0.31', '2008-10-15');
- INSERT INTO `companies` VALUES ('7', 'Exxon Mobil Corp', '68.10', '-0.43', '2008-10-17');
如下图所示建立工程:
数据库配置文件如下:
-
class DATABASE_CONFIG
-
{
-
var $default = array('driver' => 'mysql',
-
'connect' => 'mysql_connect',
-
'host' => 'localhost',
-
'login' => 'root',
-
'password' => 'root',
-
'database' => 'cake_ext',
-
'prefix' => '');
-
-
var $test = array('driver' => 'mysql',
-
'connect' => 'mysql_connect',
-
'host' => 'localhost',
-
'login' => 'root',
-
'password' => 'root',
-
'database' => 'cake_ext',
-
'prefix' => '');
- }
companies_controller.php:
-
- class CompaniesController extends AppController
- {
- var $name = 'Companies';
-
- function index()
- {
- $this->set('companies', $this->Company->findAll());
-
}
-
-
function view($id = null)
-
{
-
$this->Company->id = $id;
-
$this->set('company', $this->Company->read());
-
}
-
}
- ?>
company.php:
-
- class Company extends AppModel
- {
- var $name = 'Company';
- }
- ?>
index.thtml:
-
Test companies
-
-
-
Id |
-
company |
-
price |
-
change |
-
last update |
-
-
foreach ($companies as $company): ?>
-
-
echo $company['Company']['id']; ?> |
-
-
echo $html->link($company['Company']['company'], "/companies/view/".$company['Company']['id']); ?>
-
|
-
echo $company['Company']['price']; ?> |
-
echo $company['Company']['change']; ?> |
-
echo $company['Company']['lastudp']; ?> |
-
-
endforeach; ?>
view.thtml:
-
Company: echo $company['Company']['company']?>
-
Id: echo $company['Company']['id']?>
-
Price: echo $company['Company']['price']?>
-
Change: echo $company['Company']['change']?>
LastUpdate: echo $company['Company']['lastudp']?>
访问http://localhost/cakephp/companies即可运行测试程序。
本代码参考自官方自带例子:http://book.cakephp.org/view/326/The-Cake-Blog-Tutorial
http://www.bkjia.com/PHPjc/319623.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/319623.htmlTechArticle首先建立数据库cake_ext,并执行如下sql文: CREATETABLE`companies`( `id`int(11)NOTNULLauto_increment, `company`varchar(50)NOTNULL, `price`decimal(8,2)NOTNULL, `change`...