Zend framework中的增删改查常用方法

<?php
//数据库的增删改查
require_once 'BaseController.php';
require_once APPLICATION_PATH.'/models/Student.php';
require_once APPLICATION_PATH.'/models/Course.php';

class ZendtableController extends BaseController 
{
	public function showAction(){
		//显示前三个学生的信息,按照年龄排序
		$where='1=1';
		$order='sage';
		$count=3;
		$offset=0;
		//创建表模型
		$studentModel=new Student();
		$res=$studentModel->fetchAll($where,$order,$count,$offset)->toArray();
		
		echo "<pre>";
		print_r($res);
		echo "</pre>";
		//显示计算机系的前三个学生
		//显示前三个学生的信息,按照年龄排序
		$where="sdept='计算机'";
		$order='sage';
		$count=3;
		$offset=0;
		//创建表模型
		echo "<h1>计算机</h1>";
		//$studentModel=new Student();
		$res=$studentModel->fetchAll($where,$order,$count,$offset)->toArray();
		echo "<pre>";
		print_r($res);
		echo "</pre>";
		//考虑sql注入问题
		//创建适配器
		$db=$studentModel->getAdapter();
		$where=$db->quoteInto("sdept=?",'计算机');
		//注入考虑,一步一步注入
		$db=$studentModel->getAdapter();
		$where=$db->quoteInto("sdept=?",'计算机').$db->quoteInto(" AND ssex=?",F);
		//取出所有学生的名字和性别
		$db=$studentModel->getAdapter();
		//$sql=$db->quoteInto("select sname,ssex from student");
		//不能这样用
		//$sql=$db->quoteInto("select sname,ssex from student where ssex=:sex and sdept=:dept",array('sex'=>'F','dept'=>'计算机'));
		$res=$db->query("select sname,ssex from student where ssex=:sex and sdept=:dept",array('sex'=>'F','dept'=>'计算机'))->fetchAll();
		
		//$res=$db->query($sql)->fetchAll();//使用适配器完成查询数据,不用toArray()
	    echo "<h1>~~~~~·~~~~~~~~</h1>";
		echo "<pre>";
		print_r($res);
		echo "</pre>";
		
		//增加一条数据
		$course=new Course();
		$data=array(
		   'cid'=>'56',
		   'cname'=>'java编程',
		   'ccredit'=>87
		);
		$course->insert($data);
		
		//修改数据
		$set=array(
		   'ccredit'=>99
		);
		$where="cid='55'";
		$course->update($set,$where);
		
		//删除一条数据
		$course->delete($where);
		
		
	} 
	public function testAction(){
		$studentModel=new Student();
		$db=$studentModel->getAdapter();
		//根据主键取出shuju
		//$stu=$studentModel->find("20041010")->toArray();
		$stu=$studentModel->find(array("20041010","20041011"))->toArray();
		echo "<pre>";
		print_r($stu);
		echo "</pre>";
		
		//取出一条记录,这样比较快捷
		$res=$studentModel->fetchRow("saddress='天津'","ssex")->toArray();
		echo "<h1>只查询一条记录</h1>";
		echo "<pre>";
		print_r($res);
		echo "</pre>";
		
		//取出不重复的数据
		//什么时候使用表模型=增加,删除,查询全部字段,什么时候使用适配器=查询部分字段,多表查询,防止注入漏洞
		$res=$db->query("select distinct sdept from student")->fetchAll();
		echo "<h1>只查询!!!!!</h1>";
		echo "<pre>";
		print_r($res);
		echo "</pre>";
		
		//查询广州和香港的学生
		$res=$db->query("select * from student where saddress in('广州','香港')")->fetchAll();
		echo "<h1>只查询广州和香港的学生</h1>";
		echo "<pre>";
		print_r($res);
		echo "</pre>";
		
		//查询计算机和会计的学生
		$res=$db->query("select sdept,avg(sage) from student group by sdept")->fetchAll();
		echo "<h1>只查询广州和香港的学生</h1>";
		echo "<pre>";
		print_r($res);
		echo "</pre>";
		
		
		$this->render('show');
	}   
}

初始化数据库适配器

<?php
/*
*@author FTS
*@copyright(c) 2014
* 做一个父类,专门供其他controller类来继承
*/
class BaseController extends Zend_Controller_Action {
	public function init(){
		//初始化我们的数据库适配器
		 $url=constant("APPLICATION_PATH").DIRECTORY_SEPARATOR.'configs'.DIRECTORY_SEPARATOR.'application.ini';
		 $dbconfig=new  Zend_Config_Ini($url,"mysql");
		 $db=Zend_Db::factory($dbconfig->db);
		 $db->query('SET  NAMES  UTF8');
		 Zend_Db_Table::setDefaultAdapter($db);
	}
}
?>
对应数据库中的course表,其中主键为cid
<?php
/**
*@author FTS
*@copyright(c) 2014
*/
class Course extends Zend_Db_Table {
     protected $_name='course';
     protected $_primary='cid';
}
?>
对应数据库中student表,其中主键为sid
<?php
/**
*@author FTS
*@copyright(c) 2014
*/
class Student extends Zend_Db_Table {
	protected $_name='student';
	protected $_primary='sid';
}
?>
数据库配置:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0

[mysql]
db.adapter=PDO_MYSQL
db.params.host=localhost
db.params.username=root
db.params.password=root
db.params.dbname=stusys
student表的创建
create table student(
sid char(8) primary key,
sname varchar(64) not null default '',
ssex char(2) not null defalut '女',
sdept varchar(32) not null default '',
sage tinyint unsigned default 0,
saddress varchar(64) not null default '');
course表的创建
create table course(
cid char(2) primary key,
cname varchar(64) not null default '',
ccredit tinyint unsigned not null default 0);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值