手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:framework

YUI PHP Loader Utility

YUI是一个低调又强大的框架。你不能说它有什么,但它确实就存在着。。。
jQuery是Yii框架自带的一个工具,比如ZII里集成的全是JQuery,但YUI也是有着自己的工具,比如这个YUI PHP Load Ulility

The YUI PHP Loader Utility is a server-side utility that allows you to load specific YUI components and their dependencies into your page via PHP. YUI PHP Loader can operate as a holistic solution by loading all of your necessary YUI components, or it can be used to add one or more components to a page on which some YUI content already exists.

YUI also contains a client-side loader, which provides similar functionality from JavaScript.

YUI PHP Loader adds value in the following ways:

  1. Reliable, sorted loading of dependencies: YUI comprises more than two-dozen components, many of which work together to provide the best possible compromise between compartmentalization and code reuse. Because of this, YUI components often need to load with specific dependencies in a specific order. YUI PHP Loader understands which components depend on one another, and based on this knowledge it ensures that the right resources are loaded in the right order.
  2. Automatic use of rolled-up files. YUI PHP Loader knows about all of the built-in rollup files that ship with YUI — like the yahoo-dom-event.js file that contains the Yahoo Global Object, the Dom Collection, and the Event Utility, three components that are commonly used together. By automatically using rolled-up files when it makes sense to do so, the YUI PHP Loader helps you reduce HTTP requests and thereby keep your page as efficient as possible.

As you think about how you want to load YUI on the page, you may find it useful to refer to this overview of some of the most common loading strategies and their relative merits:

YUI的CSS其实也不错,现在细想想如果不是当年YAHOO那么强势的推PHP,如今的PHP也不会被这么多公司所能够接受吧。google是在强推python,虽然现在有着go语言,但python也正在被越来越多的公司所接受了,比如它的兼容性,它的运行速度。。。我还是比较喜欢的。。。。。。

Tags: yui, javascript, framework, jquery, yii

Yii activerecord 中 index的用法

很多时间,我们需要查询出来的结果按照指定的列进行索引,所以就会有了下面的代码

$lists = xxx::model('xx')->findAll(array('id'=>array(1,2,3,4,5)));//语句应该是不对的,表达我想in查询
然后
取出结果后
foreach($lists as $list){
   $ls[$list->id]=$list;
}
这样有没有简化写法呢?因为我这样之后,发现。。。in查询变成了多条查询。
但是是lazy loading Xxxxx.xxx(relation中的关联)

有的,Yii中有类似的写法,那就是ar中的index(上文我刚说了on)

'index': the name of the column whose values should be used as keys  of the array that stores related objects. 

于是直接

$lists = xxx::model('xx')->findAll(array('index'=>'id'));

就OK了

Tags: yii, framework, ar, index

Yii Relation记录

Yii的relation中遇到的问题,记录一下。。。

目前我有两个表,表不是我设计的。所以我才特别郁闷。

商品表:字段:p_id,member_id

商铺表:字段:shop_id,member_id

其中p_id,shop_id都是主键,member_id是索引

考虑过这个原因,是最初设计的时候,每一个普通用户都可以发布商品,所以商品表没有记录shop_id。

可能也会考虑过,每一个用户会有多个商铺吧,所以shop表的member_id也不是唯一索引。

然后,我用Yii在做表关联,事实上,在我们改动程序的时候,我们已经把普通用户能够发布商品这个功能去掉了,因此,事实上对我们来说member_id,其实肯定是于shop表中的member_id有对应关系。

于是我在ShopProduct的relations这样写

PHP代码
  1. /** 
  2.  * @return array relational rules. 
  3.  */  
  4. public function relations()  
  5. {  
  6.     // NOTE: you may need to adjust the relation name and the related  
  7.     // class name for the relations automatically generated below.  
  8.     return array(  
  9.            'member' => array(self::BELONGS_TO,'ShopMember','member_id','with'=>'extends','condition'=>"member.member_state = '1'"),  
  10.            'shop' => array(self::BELONGS_TO , 'ShopInfo' , 'member_id','on'=>'t.member_id = shop.member_id' ),  
  11.     );  
  12. }  

是的,看上去好象不错,指定了foreignKey为member_id,同时指定了关联条件,即shop.member_id=product.member_id,然而世事总是那样的难以预料。。。。

在Yii的代码中,关于relation的on是这样写的:

XML/HTML代码
  1. 'on': the ON clause. The condition specified here will be appended to the joining condition using the AND operator. This option has been available since version 1.0.2.  

所以,上述的代码在SQL中显示出来就是 select * from product left outer join shop on (product.member_id = shop.shop_id and product.member_id = shop.member_id)//这个代码是伪代码,只是为了说明问题。

问了Yii群和hightman,他们告诉我解决方法都是将foreignKey字段设为空或者False,然后再指定ON。

于是,问题解决。多谢 Fising 和 HightMan

 

 

Tags: yii, framework, relation

yii-1.1.0-validator-cheatsheet

官方的GUIDE里,对于validator的介绍太少了,只有这么一段 :

7. Validator(验证)

 

Validator需继承CValidator和实现CValidator::validateAttribute方法。

class MyValidator extends CValidator
{
protected function validateAttribute($model,$attribute)
{
$value=$model->$attribute;
if($value has error)
$model->addError($attribute,$errorMessage);
}
}

即使在这里,http://www.yiiframework.com/doc/api/1.1/CValidator,介绍的也不是特别的多,后来,找到了这个cheatsheet,感觉还不错,对应一下api的内容,感觉有点爽

感谢龙井的回复,使得我注意到了wiki,地址:http://www.yiiframework.com/wiki/56/reference-model-rules-validation/,可以关注一下下哦

 

附件: yii-1.1.0-validator-cheatsheet.pdf (60.78 K, 下载次数:3225)

Tags: yii, cheatsheet, validator, framework

Yiiframework(Yii框架)开发笔记:续四


今天我讲两件事,都是关于插件的,1、minify(@hightman的EClientScript) ,2、swfupload
1、EclientScript,这是hightman开发的插件,可以将要发布的css和JS进行合并,方便WEB开发。关于 clientScript,可以查看官方的guide,在性能优化这一节有介绍。但官方的手册上也说了,官方的scriptmap还是需要自己合并。而 eclientScript则实现了这样的功能了。一些使用方法我就不多了,我只说一些存在的问题,直接用代码说,我不说啥了:

XML/HTML代码
  1. <?php  
  2. $css = Yii::app()->clientScript;  
  3. $css->scriptMap = array();  
  4. $css->registerCoreScript('jquery'); //这个是核心的,yii框架里默认带了,你的系统中可以不必存在  
  5. $baseUrl = Yii::app()->request->baseURL ;   
  6. $css->registerScriptFile( $baseUrl . "/xxx/filepath");  
  7. $css->registerCssFile("filepath");  

$baseUrl 这段我遇到的问题最多,开始的时候,我用的是Yii::app()->module->_assetUrl;,事实上我发现这样可以成功,但 生成到assets目录里后,文件就再也不变了(无论你怎么更改原始目录中的文件,assets里的文件都不会更改)。
最后,我是直接把目录扔到了assets的目录里。然后手动设置路径 ,由EclientScript来生成压缩后的css和script文件。(如果需要发布那就必须要用到assetManage)
之所以把目录扔到assets里,是因为我需要单独把assets目录最后用独立域名发布,【关于用独立域名发布,已经建议了hightman更新了,他也答应会更新】
我对eclientscript第二个更新是在输出的URL上加上了filemtime的时间戳。以保证用户可以下载到最新的css和script。

2、swfupload。我是准备把neatpic用Yii重写,而且很多人都想要文件上传功能,但自带的上传功能也只有2M(大多数情况下只有2M)。 在实际使用下来,觉得还成(因为还没有真正测试 phpsession的那个问题,即FF和IE的sessionId的问题)。
swfupload插件,居然把文件分成两个压缩包,没想通。。。如果只下载一个可是不行的哦。所幸,官方把测试用例还是写的较详细(对于flash上的 文字,可以通过widget的参数修改,handlers.js里面是一些completed的一些方式,建议根据自己的需要修改。)

后记:我是邀请了@walkerlee,想让他分享一下心得。他是答应了,所以可以考虑催他了

Tags: framework, yii, 开发笔记

Records:26123456