Archive for the ‘symfony’ Category

Symfony + Doctrine + MySQL Replication

Friday, September 11th, 2009

网站架构加入mysql读写分离,是流行的做法。下面记录一下如何如何让采用symfony框架的产品加入mysql读写分离,我们使用doctrine实现数据操作。 在database.yml里输入: all: master: class: sfDoctrineDatabase param: dsn: 'mysql:host=localhost;dbname=mydb' username: master password: master option: charset=utf8 slave: class: sfDoctrineDatabase param: dsn: 'mysql:host=localhost;dbname=mydb' ...

整合symfony项目到康盛ucenter系列产品中

Wednesday, September 9th, 2009

最近开发一个使用symfony框架的产品,并且需要把这个项目作为一个应用添加到ucenter系列产品中,以前做过的都是康盛家族产品的整合和二次开发,还没有试验过把php框架开发的项目整合到ucenter中,整合的过程就记录在这里了。 (以下实验基于symfony1.2.7,ucenter1.0,uchome1.5版本) 康盛官方提供了一个ucenter开发包,里面包含一份电子版手册,一个uc客户端软件包,还有一个实例程序包。实际上我们现在要整合symfony项目到ucenter中,就是把实例程序改写成symfony框架程序格式,然后调用uc客户端软件包即可实现。那么首先,我们分析一下ucenter的通讯机制。 登录ucenter管理界面,只要你点击了“应用管理”选项卡,ucenter就会分别发出对应的请求给应用项目来测试通讯是否通畅。例如,我们安装了ucenter和uchome两个产品,那么ucenter就发出一个请求给uchome,uchome项目里的通讯客户端程序接受请求并返回一个常量API_RETURN_SUCCEED,如此即为我们看到的“通讯成功”。具体可以通过抓包分析并阅读ucenter的源代码查看细节。 以登录同步为例,假如我们安装了ucenter、uchome和discuz三个产品,现在我们访问uchome并登录,同步登录机制会同时让discuz的用户会话写入,即我们看到的同步登录实现。访问实例程序并抓包试验一下,原来客户端软件包在登录时通过与ucenter通讯获取了js代码,来发送登录请求给其他项目,即uchome登录时,uchome自己写入了会话,同时通js发送请求给discuz的通讯客户端程序,discuz的通讯客户端程序在验证是同步登录请求后,把会话写入了当前浏览器,由此实现了同步登录。 明白了这些基本原理,我们后面的工作就是把这套实现机制引入symfony程序了。 开发流程: 1.定义symfony路由规则 在routing.yml里加入如下代码: url: /api/uc.php param: { module: sync, action: ping } url: /login param: { module: sync, action: login } 2.构建module和action 创建sync模块和两个动作ping和login 3.加入uc客户端软件包,加入相应类文件 把client通讯包拷贝到项目lib目录下,把以下配置代码加到client.php中: // 与 UCenter 的通信密钥, 要与 UCenter 保持一致 define('UC_KEY', sfConfig::get('app_ucenter_key')); // UCenter 的 IP define('UC_IP', ''); //同步登录 Cookie 设置 define('UC_CHARSET', 'utf8');// UCenter 的字符集 $cookiedomain = '';// cookie 作用域 $cookiepath = '/';// cookie 作用路径 //center的url地址 define('UC_API',sfConfig::get('app_center_url')); 把实例程序包api/uc.php中的方法authcode()封装成一个类放到apps/frontend/lib下,注意通讯密钥要被authcode()访问到 4.开发动作代码 首先写通讯代码,pingAction.class.php代码如下:

Doctrine,确实有时很麻烦!

Thursday, August 13th, 2009

最近一直在使用symfony + Doctrine,碰到两个问题,困扰了1天时间,记录在这里。 场景1: 运行代码如下: $query = new Table(); foreach ($array as $key => $value) { $query->field1 = $value['field1']; $query->field2 = $value['field2']; $query->save(); } 结果倒好,$array里有7个记录需要插入,结果变成了最后一条插入,其他的不知所踪。(中间省略了查资料,改配置的文字... ...)最后我实在想不出哪里出问题了,干脆,打开doctrine的源代码,找到unitofwork.php(symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection),在function saveGraph()的$this->insert($record)和$this->update($record)各加入中断代码,看看到底怎么回事。等结果打印在屏幕上了,我马上明白了,代码执行成了一次插入,后面紧跟六次修改记录,无怪乎只有最后一条记录了。现在修改代码如下,执行正常了。 foreach ($array as $key => $value) { $query = new Table(); $query->field1 = $value['field1']; $query->field2 = $value['field2']; $query->save(); } 场景2:插入一条记录后,可以获取last insert id,在网上找了doctrine的资料,说$obj->save();后执行$obj->id;即可获取last insert id,实际中却只能获取一个空数组,又是一顿解决问题(省略查资料,改配置的文字... ...),最后发现这里耽误的时间过多了,干脆,把doctrine源代码record.php(symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine)和unitofwork.php(symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection)都改了,直接让$obj->save()返回last insert id搞定,先暂时这样吧,或许以后能找到不动doctrine源代码的解决办法。

一个备份文件引发的血案… …

Wednesday, July 15th, 2009

    首先声明,没出血。稍微夸大点,提醒我以后注意... ...仅此而已。 最近使用symfony1.2做一个项目,使用了ORM doctrine。还是老路子,基于数据库生成schema.yml文件,再生成model类,最后操作数据。可是就在这里出了问题,项目需要连结两个不同的数据库,这两个数据库名不一样,里面的表结构有一些不同,但是里面的表名都一样,为了区别操作,我在schema.yml里指定了connection: master或者connection: client属性,可是这以后无论我怎么重新构建model,甚至重新构建schema.yml,改写database.yml,数据库连结就是不按照我的想法去进行,它总是连结database.yml里最后的那个数据库。两天时间,逼得我重新把doctrine和symfony官方网站上所有的文档又看了一遍,实在是不知道怎么办了。 最后请来了蚂蚱,刚开始他也觉得很诧异,为什么这里会出问题呢?他看了又看,忽然他说道:“谁让你这么干的?”我一愣,只见他指着config/doctrine/目录下的copy of schema.yml一脸惊讶得说道:“谁让你这么做备份的?把这个删除了就好了!” 用框架,千万小心自动载入功能,要牢记!

[原创中文翻译]symfony askeet24:终于校译完了… …

Thursday, March 20th, 2008

    今天终于完成了校译,我已经连续很长时间没有好好享受一下业余休息时间了... ...呵呵!虽然比较劳累,但是心里很高兴,毕竟这件事情有始有终得完成了。Never do another thing when you have not finished one yet. 想想2007年10月份,当时我心里都琢磨我能不能把这24篇英文文章翻译完,而且对于一个还没有把PHP完全掌握好的新手来说,翻译一份PHP开发框架的实践使用教材,而且要弄懂框架里的东西,压力似乎比较大。 就好比你还没有完全学会爬,就开始进行跑的动作。天啊!我都挺佩服我当时的想法。不过,人能成功都是被逼出来的,天下事没有什么怕的,只要你具备有毅力,没有你做不好的!艰难的翻译开始了,中间夹杂着重新温习PHP基本嵌套开发,体会PHP MVC模式开发思想,甚至我自己写了一个小的MVC框架的PHP论坛来拼命练习,光这些当然是不够的,还要熟读Symfony文档,尤其是大量的API文档。呵呵!个中的滋味只有自己知道。 校译完成了,下面还有很多需要做的... ...

[原创]symfony中module的命名问题。

Thursday, March 13th, 2008

最近一直在试验symfony,今天偶然发现了module命名的一个小问题。写下来大家一起讨论一下。 做法:新建项目,建立myapp,然后创建moduel,名字取default,这个时候访问项目url(最好使用开发模式url),显示见下图: 这个时候在module default的action里actions.class.php文件里加一个方法,例如list,list的功能随便你定义,可以从数据库里随便取几个数据,当然了,要创建对应的listSuccess.php模板来显示对应动作传的数据;然后修改默认方法index的$this->forward('default',module);为$this->forward('default','list');。这一切都做完后,修改myproject/apps/myapps/config/routing.yml路由协议文件中的homepage部分为param: { module: default, action: list}。当这一切都做好后,运行symfony cc清除所有缓存。 现在访问项目url,展现在你眼前的页面只是default module的动作actions.class.php中list方法对应的listSuccess.php模板内容,没有layout.php的内容。 如果你是用除default以外的名字命名module,目前为止还没出现过上面的情况。此类问题在学习官方手册时也没见有提醒命名规则的内容。个人感觉symfony的内部处理机制在转到layout.php布局时存在一个默认的module和控制动作,名字应该就是default。 以上只是个人的理解,如果有不同意见,请留言。

[原创中文翻译]symfony askeet24:第二十四天,后面的工作是什么呢?

Thursday, October 11th, 2007

[欢迎转载,转载请注名出处http://symfony.net.cn。本文英文版权归symfony官方网站所有] (译者:这是最后一篇了,大体是关于akseet程序的用途,安装,下载,文档等一系列内容,随意看看就行,笔者只翻译了文件结构和数据模型等部分。) Use it 使用askeet The askeet website is open to the public. You can advertise it and talk about it to your friends and relatives. Some of the test contributions will be removed, but most of the existing questions and user accounts will remain. Askeet is a great tool to find answers - ...

[原创中文翻译]symfony askeet24:第二十三天,网站国际化。

Thursday, October 11th, 2007

[欢迎转载,转载请注名出处http://symfony.net.cn。本文英文版权归symfony官方网站所有] Localization 本地化 What if the call to an address like: 这个URL如何: http://fr.askeet.com/ ...displayed only the French questions? Well, this is quite easy, because since the eighteenth day, such an URI is understood as a universe. ... ...只显示法语问题。很简单,第八天的教材URL可以被当作标签处理。 Content 内容 Creating a question in a language universe will have it tagged automatically with the language tag (here: 'fr'). And, if ...

[原创中文翻译]symfony askeet24:第二十二天,上传服务器发布网站。

Thursday, October 11th, 2007

[欢迎转载,转载请注名出处http://symfony.net.cn。本文英文版权归symfony官方网站所有] Synchronization 同步 Good practices 推荐的方案 There are a lot of ways to synchronize two environments for a website. The basic file transfers can be achieved by an FTP connection, but there are two major drawbacks to this solution. First, it is not secure, the data stream transmits in the clear over the Internet and ...

[原创中文翻译]symfony askeet24:第二十一天,创建搜索引擎。

Wednesday, October 10th, 2007

[欢迎转载,转载请注名出处http://symfony.net.cn。本文英文版权归symfony官方网站所有] How to build a search engine? 怎么创建一个搜索引擎呢? The most popular suggestion about the 21st day addition proved to be a search engine. 第二十一世纪最流行的是互联网搜索引擎。 If the Zsearch extension (a PHP implementation of the Lucene search engine from Apache) had already been released by Zend, this would have been a piece of cake to implement. Unfortunately, ...

[原创中文翻译]symfony askeet24:第二十天,增加管理员和版主。

Wednesday, October 10th, 2007

[欢迎转载,转载请注名出处http://symfony.net.cn。本文英文版权归symfony官方网站所有] The expected result: what the client says 需求分析:客户的要求是什么 Today's job will consist of a few new actions, new templates and new model method, and we already know how to do that. The hardest part is probably to define what is needed, and where to put it. It is both a functional and ...

[原创中文翻译]symfony askeet24:第十九天,网站性能和缓存。

Tuesday, October 9th, 2007

[欢迎转载,转载请注名出处http://symfony.net.cn。本文英文版权归symfony官方网站所有] Load testing tools 负载测试工具 Unit tests, described during the fifteenth day, can validate that the application works as expected if there is only one user connected to it at a time. But as soon as you release your application on the Internet - and that's the least we can wish for you ...

[原创中文翻译]symfony askeet24:第十八天,过滤器。

Monday, October 8th, 2007

[欢迎转载,转载请注名出处http://symfony.net.cn。本文英文版权归symfony官方网站所有] We saw yesterday how to make the askeet service available through an XML API. Today's program will focus on filters, and we will illustrate their use with the creation of sub domains to askeet. For instance, 'php.askeet.com' will display only PHP tagged questions, and any new question posted in this ...

[原创中文翻译]symfony askeet24:第十七天,API。

Monday, October 8th, 2007

[欢迎转载,转载请注名出处http://symfony.net.cn。本文英文版权归symfony官方网站所有] The API 程序(二次)开发接口 An Application Programming Interface, or API, is a developer's interface to a particular service on your application, so that it can be included in external websites. Think about Google Maps or Flickr, which are used to extend lots of websites over the Internet thanks to their APIs. 程序(二次)开发接口,API,提供给开发者进行二次开发的程序接口,可以被外部网站使用。想一下Google地图或者Flickr,由于他们的API接口在互联网上催生了很多网站。 Askeet makes no ...

[原创中文翻译]symfony askeet24:第十六天,今天真懒。

Monday, October 8th, 2007

[欢迎转载,转载请注名出处http://symfony.net.cn。本文英文版权归symfony官方网站所有] After fifteen hours of hard work, we all deserve some time off. So we have decided to declare the sixteenth day the lazy day, because getting some rest is always a good thing when developing web applications. There is no symfony tutorial published today, but there is still a lot ...