cakephp使用筆記

一、cakephp,一個controller裏面能夠使用多個model,用$uses來聲明php

public $uses = array("Question", "Answer");html

model的命名遵循駝峯式,不像view是用下劃線隔開數組

二、model默認返回數組,使用下面的代碼來轉換成對象eclipse

public $actsAs = array('Bean');post

須要有對應的bean對象才能進行轉換。ui

複製類的時候必定要記得改類名,zend studio不會像eclipse那樣提示類名錯誤this

三、php拼接htmlspa

<?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>';
	}
?>

四、經過在文件中打印內容,遠程調試代碼調試

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);

五、controller 引用model的問題code

WechatsController 默承認以直接使用Model Wechat,可是若是controller裏面使用了下面的引入語句
public $uses = array('Game', 'User', 'Contest');
其中沒有包含Wechat, 那wechat就不能直接使用了

Wechat沒有在根目錄下的Model裏,若是引用的話要加上外面的目錄
$this->loadModel('External.Wechat');

六、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));

七、後臺賦值和前臺取值

post取值:$this->data['openId'];
get取值:$openId = $this->request->query['openId'];
後臺複製:$this->set("openId",$openId);
前臺取值:<input type="hidden" name="openId" value="<?php echo $openId; ?>"/>

八、指定某些方法無需權限驗證

public function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('sheet', 'allagainst');
	}
相關文章
相關標籤/搜索