YiiFramework 随笔(四)

YII 的 JQUERY 的json 运用方法
1. php中建立数组
$chage[‘fieldid’] = $fieldid;
$chage[‘butflag’] = 0;
echo CJSON::encode($chage);

2. JS文件中输出
var Jsonstr = $.json.decode(msg);
这样就可以取值Jsonstr[‘fieldid’]

———————————————————————————————–
YII 不用自带的php validate验证功能,自己可以模仿输出
1. 在php中
先用数据库查询用户是否存在
if($this->checkEmail($_POST[’email’]))
{
$error[0] = ’email already exists’;
$er = true;
}
if($er) // 如果存在返回到首页
{
$questions = $this->loadQuest();
$this->render(‘index’, array(
‘post’ => $questions,
‘error_msg_email’ => $error[0],
‘error_msg_zip’ => $error[1],
‘firstname’ => $_POST[‘firstname’],
’email’ => $_POST[’email’],
‘zipcode’ => $_POST[‘zipcode’],
‘perstatus’ => $_POST[‘perstatus’],
));
}

2. tpl中 index.tpl
if($textname == “email”)
{
echo CHtml::textField($textname, $value = $$textname, array(“class” => “required email”, “minlength” => “3”));
//用css的控制,决定错误提示是否输出及输出的位置
echo ‘<label class=”errorjs” for=”email” generated=”true” style=”‘,($error_msg_email == ”)?’display:none’:”,'”>’,$error_msg_email,'</label>’;
echo CHtml::hiddenField($textname . “hiden”, $model->id);
}

———————————————————————————————————————————-
类的不同调用
1. 当定义类中函数有静态属性时候。加static 的是静态成员,不能用实例化。
在你运行的时候他自己在内存中开辟了块空间,不用再一次new, 有点像全局变量
class Uti
{
public static function teOptions($text){}
}
2. 外面函数调用时候,用 Uti::teOptions() 这样就可以。但引用变量时候需self::$a,这样的方式
3. 若类中定义为 先实例化(new)一下才能用
class Uti
{
public function texOptions($text){}
}
4. 外部调用如下: 类中变量可以$this->a方式使用
$component = new Uti;
$tmpflag = $component -> texOptions();
——————————————————————————————————————————————————–
解释models文件夹中读取数据库中表的文件。relationship的应用
http://www.yiiframework.com/doc/guide/zh_cn/database.arr
1. 表结构如下
category (pk) id , name
postcategory (pk,fk1) postID,(pk,fk2)categoryID
post (pk) id , title,content,createTime,(fk1)authorID
user (pk) id , username,password email
profile (pk,fk1) ownerID,photo,website

2. relation结构如下
‘VarName’=>array(‘RelationType’, ‘ClassName’, ‘ForeignKey’, …additional options)
// author 为自己命名的标志,在查询时候应用
// user和post的关系是一对多,post belongs to user
// 外键是 authorID
// categories 为自己命名的标志,在查询时候应用
// category和post的关系是多对多,只不过用了一个中间表让之形成一对多,多对一这样的关系。post many_many category
// 外键是 postcategory 这个表,它里面有两个字段
class Post extends CActiveRecord { public function relations() { return array( ‘author’=>array(self::BELONGS_TO, ‘User’, ‘authorID’), ‘categories’=>array(self::MANY_MANY, ‘Category’, ‘PostCategory(postID, categoryID)’), ); } }
// posts 为自己命名的标志,在查询时候应用
// post和user的关系是多对一,user has many post
// 外键是 authorID
// profile 为自己命名的标志,在查询时候应用
// user和Profile的关系是多对一,user has one Profile 是 has many的变种
// 外键是 ownerID
class User extends CActiveRecord { public function relations() { return array( ‘posts’=>array(self::HAS_MANY, ‘Post’, ‘authorID’), ‘profile’=>array(self::HAS_ONE, ‘Profile’, ‘ownerID’), ); } }

3. sql程序调用时候
//因为没有调用之前定义的author,categories,查询结果不启用关联查询,不会消耗性能
$post=Post::model()->findByPk(10);
//当用到with()这个函数时候,它关联了author 所以查询结果中增加user表中的结果
$posts=Post::model()->with(‘author’)->findAll();
//用到了三个命名标志,所以它关联三个表 user ,Category, PostCategory
$posts=Post::model()->with(‘author’,’categories’)->findAll();
//因为有关联到user中的profile 所以目前关联的是 post ,user, category, postcategory,profile
$posts=Post::model()->with(array( ‘author’=>array( ‘profile’, ‘posts’), ‘categories’))->findAll();

注意:当不设置relation的时候但仍然要操作left join 之类的操作。方法如下
$sql = “select * from date left join post on post.id = date.id”;
$temp=Date::model()->dbConnection->createCommand($sql)->queryAll();
用 Yii的AR时候不设置relation时候不能操作
这样是错误的。他调用了Yii的 CActiveRecord类中的populateRecords方法,它会根据relation中的表字段重新排列一下,所以左连接的内容不会被显示
Date::model()->findBySql(“select * from date left join post on post.id = date.id”);
———————————————————————————————————————————————————-
View 的应用
http://www.yiiframework.com/doc/guide/basics.view
1. 可以用render用来传值到view层
$this->render(‘edit’, array(
‘var1’=>$value1,
‘var2’=>$value2,
));
当用这种形式时候
通过调用 renderPartial() 可以不依赖布局而渲染视图.

2. 主文件的tpl在这里protected/views/layouts/main.php
可以用这样的形式来定义头和尾文件
……header here……
<?php echo $content; ?>
……footer here……

3. 应用组件CWidget
<?php $this->beginWidget(‘path.to.WidgetClass’); ?>
…body content that may be captured by the widget…
<?php $this->endWidget(); ?>

或者不需要body的组件
<?php $this->widget(‘path.to.WidgetClass’); ?>

也可以通过传递值到组件中
<?php
$this->widget(‘CMaskedTextField’,array(
‘mask’=>’99/99/9999’
));
?>

4. 系统视图
当出现404这样的错误时候 protected/views/system 在这里的模版可以列出显示错误页面

——————————————————————————–
Path Alias and Namespace 路径假名
1. 应用YiiBase::getPathOfAlias() 会把system.web.CController 变成真正的物理路径 yii/framework/web/CController

导入文件的方法,这样比include和require高效
Yii::import(‘system.web.CController’);
这样可以导入一个目录的文件
Yii::import(‘system.web.*’);
2. import基于spl_autoload_extensions创建新对象,且比较快http://cn.php.net/spl_autoload
ClassA.php
<?php class ClassA { var $val = ‘Hello from class “ClassA”‘; } ?>
ClassB.php
<?php class ClassB { var $val = ‘Hello from class “ClassB”‘; } ?>
ClassC.php
<?php class ClassC { var $val = ‘Hello from class “ClassC”‘; } ?>
ClassD.php
<?php class ClassD { var $val = ‘Hello from class “ClassD”‘; } ?>
ClassE.php
<?php class ClassE { var $val = ‘Hello from class “ClassE”‘; } ?>

1》spl_autoload_extensions(‘.php,.inc’);
// new priority: .php .inc
for($n=65; $n<70; $n++) {
$className = ‘Class’.chr($n);
spl_autoload($className);
$ins = new $className;
echo $ins->val.'<br>’;
//1.4 miliseconds
}

2》 Simple:
<?php
// default priority: .inc .php
for($n=65; $n<70; $n++) {
$className = ‘Class’.chr($n);
spl_autoload($className);
$ins = new $className;
echo $ins->val.'<br>’;
}
// 4.2 miliseconds
?>

3. http://www.yiiframework.com/doc/guide/zh_cn/extension.integration
用Yii::import 方法还可以嵌入第三方程序
1》我们嵌入zend的Zend_Search_Lucene模块方法如下
首先,假设protected是网站的主目录,我们提取Zend Framework的发布文件到protected/vendors目录。确认protected/vendors/Zend/Search/Lucene.php文件存在

2》在一个controller类文件的开始写入如下语句
Yii::import(‘application.vendors.*’);
require_once(‘Zend/Search/Lucene.php’);
在任何action中可以这样引用了
$lucene=new Zend_Search_Lucene($pathOfIndex);
$hits=$lucene->find(strtolower($keyword));

————————————————————————
Conventions 相关协定
1. URL
传统GET传输参数http://hostname/index.php?r=ControllerID/ActionID
当用到CUrlManager这个组件时候(相关参考protected/config/main.php),url变成http://hostname/ControllerID/ActionID.html

——————————————————————–
在controller文件中 设定默认显示的action
1. public $defaultAction=’login’; 这样就可以显示默认的第一个action了

2. action 也可以自由定义 跟controller一样
http://www.yiiframework.com/doc/guide/zh_cn/basics.controller
定义如下:
class UpdateAction extends CAction
{
public function run()
{
// place the action logic here
}
}
当在controller中调用此action时候。action类文件为protected/controllers/post/UpdateAction.php
class PostController extends CController
{
public function actions()
{
return array(
‘edit’=>’application.controllers.post.UpdateAction’,
);
}
}

3. public function actions() 单独的这个函数表名执行controller时候需要前期执行某些函数
Filters preprocess/postprocess 都有可能被用到
———————————————

可以用YII的errorhandler

if($this->_category === null)
throw new CHttpException(500, ‘The requested category does not exist.’);



发表评论

您的电子邮箱地址不会被公开。

− 2 = 1