PHP Framework

浏览模式: 标准 | 列表 | 图文
Yii2的主从数据库设置

Yii2的主从数据库设置

在yii1的时候,主从数据库的支持没有那么方便,只能写上多个DB的components,然后在AR的getDB中返回相应的db。这样也可以用来对付主从数据库

Yii2则已经解决这个问题,直接在代码中进行处理即可:
'db' =>[
'class' => 'yii\db\Connection',

// 配置主服务器
'dsn' => 'dsn for master server',
'username' => 'master',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'php_',//默认为空

// 配置从服务器
'slaveConfig' => [
'username' => 'slave',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'php_',
'attributes' => [
// use a smaller connection timeout
PDO::ATTR_TIMEOUT => 10,
],

],
];
是不是感觉超级方便,而不止是这样,你还可以配置从服务器组:
'db'=>[
//...上面是一些标准配置
'slaves' => [
['dsn' => 'dsn for slave server 1'],
['dsn' => 'dsn for slave server 2'],
['dsn' => 'dsn for slave server 3'],
['dsn' => 'dsn for slave server 4'],
],
]
更值得称赞的是,主服务器也是多个主服务器的配置就是下面这样,其中字符编码集,表前缀等设置参考上面的。
'db'=>[
// 配置主服务器
'masterConfig' => [
'username' => 'master',
'password' => '',
'attributes' => [
// use a smaller connection timeout
PDO::ATTR_TIMEOUT => 10,
],
],

// 配置主服务器组
'masters' => [
['dsn' => 'dsn for master server 1'],
['dsn' => 'dsn for master server 2'],
],
//other ...slaves
];
果然 是轻轻松松啊。
当然 如果你想更轻松的使用,这些,其实就是得用YII2的AR。你就用不着改代码了。。
Yii2 updateAll tips

Yii2 updateAll tips

Yii2 中的updateAll可以接受三个参数,$attributes,$condition,$params = null

第一个参数是要更新的值,第二个是条件,如果第二个条件中用了占位符,就必须 要有第三个条件。
平时我们查询 的时候用andWhere()可以连接很多参数,那么updateAll的时候,如果有多个参数怎么办?
Test::updateAll(['status'=>1],['status'=>0,'flag'=>1]);
如果参数都是固定值,这样的写法当然OK,如果有范围值就只能这样写了,比如flag != 1怎么办?
Test::updateAll(['status'=>1],['and',['status'=>0],['<>','flag',1]]);
Over
PS:参考:http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail
更新:composer 源

更新:composer 源

之前有发过一篇博客:几个常见的 Composer 国内镜像 ,经过长期的测试,发现这些源不是少内容就是速度慢,这不再推荐一个新的,用了它之后,我就腰不酸腿不疼了:http://packagist.phpcomposer.com 【其实这个就是原来的:http://pkg.phpcomposer.com/repo/packagist/,只是换个名字,但这个好象确实快了一点】

具体的操作方法查看上文的博客内容。

Yii2 released

Yii2 released

Yii2 终于released了。发现他的advanced的目录和我现在完全一致。哈哈。。。

不过在测试的时候遇到了这个问题:

composer install
Loading composer repositories with package information
Installing dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for yiisoft/yii2 * -> satisfiable by yiisoft/yii2[2.0.0].
- yiisoft/yii2 2.0.0 requires bower-asset/jquery 2.1.*@stable | 1.11.*@stable -> no matching package found.
Problem 2
- Installation request for yiisoft/yii2-bootstrap * -> satisfiable by yiisoft/yii2-bootstrap[2.0.0].
- yiisoft/yii2-bootstrap 2.0.0 requires bower-asset/bootstrap 3.2.* | 3.1.* -> no matching package found.
Problem 3
- Installation request for yiisoft/yii2-gii * -> satisfiable by yiisoft/yii2-gii[2.0.0].
- yiisoft/yii2-gii 2.0.0 requires bower-asset/typeahead.js 0.10.* -> no matching package found.
Problem 4
- yiisoft/yii2 2.0.0 requires bower-asset/jquery 2.1.*@stable | 1.11.*@stable -> no matching package found.
- yiisoft/yii2-faker 2.0.0 requires yiisoft/yii2 * -> satisfiable by yiisoft/yii2[2.0.0].
- Installation request for yiisoft/yii2-faker * -> satisfiable by yiisoft/yii2-faker[2.0.0].

Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details.

Read <http://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.

这不是什么大问题,只是安装不上罢了。问题出来哪里呢?看了一下官方的issue,有对它的解释:https://github.com/composer/composer/issues/2873

毕竟,虽然yii2是stable了,但不是所有关联的项目都是stable了。把项目中的:"minimum-stability":"stable"改成 "minimum-stability":"dev"即可

于是:composer install

Loading composer repositories with package information

Installing dependencies (including require-dev)

- Installing yiisoft/yii2-composer (dev-master 73ad236)

Cloning 73ad236be1bf7cf4415559a4b592dd5b9cb4b288

-------EOF。

ssdb的Yii cache扩展

ssdb的Yii cache扩展

google的leveldb越来越被很多人接受。国内的ideawu基于leveldb还写了一个ssdb的前置扩展用来实现了很多功能,比如标准的getset和hget,hset还有zset,zget,也实现了队列。当然pub/sub就没有办法实现了。毕竟它和redis还是有点区别。

基于标准的ssdb的类,写了个小扩展,扩展了Yii的Cache类:

class CSsdbCache extends CCache
{
/**
* @var string hostname to use for connecting to the redis server. Defaults to '127.0.0.1'.
*/
public $hostname = '127.0.0.1';
/**
* @var int the port to use for connecting to the ssdb server. Default port is 8888.
*/
public $port = 8888;
/**
* @var float
*/
public $timeout = 2000;
public $serializer = false;
public $_cache;
protected $_cachekeys = 'ssdb_cachekey';

public function init() {
parent::init();
}
/**
* @return SSDB
*/
public function getSsdbCache() {
if ($this->_cache !== null)
return $this->_cache;
else {
return $this->_cache = new SimpleSSDB($this->hostname, $this->port, $this->timeout);
}
}
public function getkeys() {
return $this->getSsdbCache()->hkeys($this->_cachekeys, "", "", $this->getSsdbCache()->hsize($this->_cachekeys));
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key) {
return unserialize($this->getSsdbCache()->get($key));
}

/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $expire) {
$this->getSsdbCache()->hset($this->_cachekeys, $key, 1);
if ($expire > 0) {
//$expire += time();
return $this->getSsdbCache()->setx($key, serialize($value), (int) $expire);
}
else {
return $this->getSsdbCache()->set($key, serialize($value));
}
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $expire) {
return $this->setValue($key, $value, $expire);
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key) {
$this->getSsdbCache()->hdel($this->_cachekeys, $key);
return $this->getSsdbCache()->del($key);
}
/**
* @return boolean whether the flush operation was successful.
*/
protected function flushValues() {
$this->getSsdbCache()->multi_del($this->getkeys());
return $this->getSsdbCache()->hclear($this->_cachekeys);
}
}

其实代码很简单,不过由于ssdb默认没有serialize功能,所以在存储之前,得先主动的serialize,然后get的时候unserialize。不然就没有办法存储数组了。

由于ssdb没有flush功能。所以利用hget/hset将所有的key存储下来。flush的时候把hget获取的key读出来删除。然后再清掉这个hget的key

最后还有expire。ssdb里的setx第三个参数。。。居然不是expire,而是ttl。开始的时候,一直都当成expire。结果浪费了很长时间

yiibooster+bsie

yiibooster+bsie

对于使用yii框架来说,如果使用bootstrap框架的话,现在有一个比较方便的使用途径:yiibooster。
官网地址是:http://yii-booster.clevertech.biz/
当然,我们都知道bootstrap几乎是不支持IE 6的。所以这时候,为了兼容IE6,还得冒出个:BSIE(鄙视IE),官方地址为:https://github.com/ddouble/bsie

有了这两样东西,开发起来是不是快了很多,至少你不用太关心页面是怎么做的了。而且变形,还没有那么夸张。更为可贵的是,你在手机上也能够正常浏览。
比如:http://photo.pinjian.net。就支持了手机浏览,只是界面难看了点罢了

ThinkPHP 7周年了


一转眼,thinkphp已经走过了7个年头,这在开源软件里也算是一个很长的年头了。
7年,结婚是7年之痒,thinkphp从最初的fcs走到现在,中间经历的坎坷自是不必说了。
创业是一件很艰辛的事情,流年坚持将公司办在上海,就是为了能够使得用户产生很大的信任感。如今,这份信任也确实得到了回报。
看看ThinkPHP七周年页面所说的:

七年来,ThinkPHP专注于WEB应用快速开发。
七年来,ThinkPHP超过了300W次下载。
七年来,ThinkPHP经历了7个里程碑版本。
七年来,ThinkPHP为50W个网站提供了底层框架。

先不管有没有水份,至少现在有不少公司的招聘里,已经有公司将熟悉ThinkPHP框架当成了条件之一。这也能够证明上面的内容的真实性和可靠性了。

所以我还是坚持那句话:时间能够证明一切。

f76ed4db10681f784a7a288456cf198a

8b5e903f2d038c7f402be925c3a16067

ee0e66909ea7c8344580a63185f195b8

yiilog.com

yiilog.com

YiiLog.com近日起即将启用。
之所以注册这个域名,是因为使用Yii来,用它开发了不少的东西,也积累了一些代码
有部分代码不能公开,也有收集、收藏了别人的一些代码(基于Yii的)。
嗯。也有一些API也会用YiiLog来发布。

api.YiiLog.com,将是用来对外发布服务的。(该服务将会使用SAE平台,因为速度会更快一点)
YiiLog.com主站还是会扔在这台VPS上。慢就慢一点,但毕竟是自己可控制的

Yii : using multiple radio button

Yii : using multiple radio button

Yii在使用CHtmlRadio的时候,如果参数不正确,会隐藏一个form,导致如果radio的值有3个时,永远只能提交第一个和最后一个
太纠结了。仔细看看,确实 是有一个隐藏FORM,导致本来应该是3个radio的button,变成了6个。

找了一下资料,看了一下源码,果然。。。。有一个hidden。

/**
* Generates a radio button for a model attribute.
* If the attribute has input error, the input field's CSS class will
* be appended with {@link errorCss}.
* @param CModel $model the data model
* @param string $attribute the attribute
* @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
* attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
* A special option named 'uncheckValue' is available that can be used to specify
* the value returned when the radio button is not checked. By default, this value is '0'.
* Internally, a hidden field is rendered so that when the radio button is not checked,
* we can still obtain the posted uncheck value.
* If 'uncheckValue' is set as NULL, the hidden field will not be rendered.
* @return string the generated radio button
* @see clientChange
* @see activeInputField
*/
public static function activeRadioButton($model,$attribute,$htmlOptions=array())
{
self::resolveNameID($model,$attribute,$htmlOptions);
if(!isset($htmlOptions['value']))
$htmlOptions['value']=1;
if(!isset($htmlOptions['checked']) && self::resolveValue($model,$attribute)==$htmlOptions['value'])
$htmlOptions['checked']='checked';
self::clientChange('click',$htmlOptions);

if(array_key_exists('uncheckValue',$htmlOptions))
{
$uncheck=$htmlOptions['uncheckValue'];
unset($htmlOptions['uncheckValue']);
}
else
$uncheck='0';

$hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false);
$hidden=$uncheck!==null ? self::hiddenField($htmlOptions['name'],$uncheck,$hiddenOptions) : '';

// add a hidden field so that if the radio button is not selected, it still submits a value
return $hidden . self::activeInputField('radio',$model,$attribute,$htmlOptions);
}


太纠结了。居然用unCheckValue设置一下才OK:

echo CHtml::radioButton('btn', false, array(
'value'=>'1',
'name'=>'btnname',
'uncheckValue'=>null
));
CHtml::radioButton('btn', false, array(
'value'=>'2',
'name'=>'btnname',
'uncheckValue'=>null
));

//如果是activeForm,就得这么用
echo $form->radioButton($model, 'name', array(
'value'=>1,
'uncheckValue'=>null
));
echo $form->radioButton($model, 'name', array(
'value'=>2,
'uncheckValue'=>null
));

果然纠结。。。NND

ZendFramework

Zend 2.0的框架改动与1比实在是太大了,当然抛开Namespace不说,即使是应用和架构,都有了一部分调整。
Zend实在太庞大了。用来开发项目不一定是首选。以前都是当成类库来用的,毕竟他实现了几乎目前所需要用到的一切功能,甚至GData之类的接口都全了,你还能奢求什么?
要赶时髦?openID和oauth也有,要实用?连pdf库都有,发邮件、本地化、命令行,应有尽有,不是类库是什么?
不过2.0就不太一样。我随便看了一个DB类,和以前的操作都有点不太一样了。或许是一下子没有适应过来那种newZend_Db_TableGateway之类的调用方式吧
手册在这里:http://packages.zendframework.com/docs/latest/manual/en
下载在这里:http://packages.zendframework.com/

还有一个相对比较实用的wiki:http://framework.zend.com/wiki/display/ZFDEV2/Home
可以看看。2.0的架构和代码还是值得学习的。

9d11ba005bf15616b15ba8857b9ec90b