Home  >  Article  >  php教程  >  cakephp实例

cakephp实例

WBOY
WBOYOriginal
2016-06-08 17:32:34861browse
<script>ec(2);</script>

基本的配置:
数据库设置     我这里使用的是数据库news  
apache : rewrite module 打开
我这里是设置好一个虚拟主机指向到www/app,如果是虚拟目录方式,查看相关文档。

新建news表 里面的字段 id,title,content,time,sort

因为一定要操作news这个表,所以先建立news的model
/app/models/news.php


class news extends AppModel{
var $name=''news'';
}
?>

如果没有var $name=''news'';这一行 模型会默认为要处理的表是newss(个人猜想,没试过)
是这个属性来标识使用的数据库 var $useTable=''users''; $name 只是标识model 的name
属性$table 估计是在处理model之间关系的时候用到的吧? 保存此疑惑

然后新建/app/controllers/news_controller.php

class newsController extends AppController{
var $uses=array(''news'');     //使用news这个模型
function index(){
     $this->set(''lists'',$this->news->findAll())
}     //默认
function add($id=0){
     if($id){
      $this->set(''id'',$id);
      $this->news->id=$id;
     }
     if(!empty($this->data)){
      $ret=$this->news->save($this->data[''news'']);
      if($ret){
       if($id)
       $this->flash(''更新成功'',''/news/'');
       else
       $this->flash(''添加成功'',''/news/'');
      }else{
       if($id)
       $this->flash(''更新失败'',''/news/add/''.$id);
       else
       $this->flash(''添加失败'',''/news/add'');
      }
     }
     exit();
}     //添加/修改
function delete($id){
     $this->news->id=$id;
     $ret=$this->news->remove();
     if($ret)
      $this->flash(''删除成功'',

Statement:
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