1、cakephp,一个controller里面可以使用多个model,用$uses来声明
public $uses = array("Question", "Answer");
model的命名遵循驼峰式,不像view是用下划线隔开
2、model默认返回数组,使用下面的代码来转换成对象
public $actsAs = array('Bean');
需要有对应的bean对象才能进行转换。
复制类的时候一定要记得改类名,zend studio不会像eclipse那样提示类名错误
3、php拼接html
<?php
foreach($message as $key => $value){
echo '<li>
<p class="p1">'.$value['name'].'<a href="#">'.(isset($value['city'])?$value['city']:"").'</a></p>
<p>'.$value['amount'].(isset($value['active'])?'('.$value['active'].')':"").'</p>
<p>'.$value['msg'].'</p>
</li>';
}
?>
4、通过在文件中打印内容,远程调试代码
ob_start();
echo '<pre>';
print_r($content);
$txt = ob_get_contents();
ob_end_clean();
$fp = fopen('/home/she/weixin.txt', 'a+');
fwrite($fp, $txt);
fclose($fp);
5、controller 引用model的问题
WechatsController 默认可以直接使用Model Wechat,但是如果controller里面使用了下面的引入语句
public $uses = array('Game', 'User', 'Contest');
其中没有包含Wechat, 那wechat就不能直接使用了
Wechat没有在根目录下的Model里,如果引用的话要加上外面的目录
$this->loadModel('External.Wechat');
6、cakephp 查询
$conditions = array();
$conditions['or'] = array(
array('BuddyRelationship.user_id'=>$userId, 'BuddyRelationship.buddy_user_id'=>$buddyUserId),
array('BuddyRelationship.user_id'=>$buddyUserId, 'BuddyRelationship.buddy_user_id'=>$userId)
);
$conditions['and'] = array('BuddyRelationship.status'=>self::Accepted, 'BuddyRelationship.deleted'=>self::Undeleted);
$buddyCount = $this->find('count', compact('conditions'));
$creditModel = ClassRegistry::init("Credit");
$conditions['and'] = array('Credit.amount <>'=>0);
$conditions['and'] = array("User.id" => $uid);
$credit = $creditModel->find('first', array('conditions'=>$conditions));
7、后台赋值和前台取值
post取值:$this->data['openId'];
get取值:$openId = $this->request->query['openId'];
后台复制:$this->set("openId",$openId);
前台取值:<input type="hidden" name="openId" value="<?php echo $openId; ?>"/>
8、指定某些方法无需权限验证
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('sheet', 'allagainst');
}