title: "CakePHP 使用小技巧" date: 2014-11-19T22:00:00+08:00 tags: ["CakePHP"] draft: false
$this->Order->id = $id;
$this->Order->saveField('status', $status);
$this->Widget->updateAll(
array('Widget.numberfield' => 'Widget.numberfield + 1'),
array('Widget.id' => 1)
);
// 只获取 name 字段信息
$this->User->read('name', $id);
// 获取所有信息
$this->User->read(null, $id);
<!--more-->
$this->redirect($this->referer());
$this->requestAction(
array('controller'=>'Wx','action'=>'aa'),
array('data'=>
array('xing'=>'A1','ming'=>'A2')
)
);
这样可以在 A 控制器调用 B 控制器方法,而且在后面传参过去,用$this->request->data 获取参数值。
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
Model 要改一下名字才能用。
$this->User->find('all', array(
'conditions' => array(
'OR' =>array(
array('nickname like ' => "%$keyword%"),
array('User.id' => $keyword),
)
),
'fields' => 'User.id,User.nickname'
));
#findAllBy<fieldName>(string $value, array $fields, array $order, int $limit, int $page, int $recursive)
$this->Product->findAllByOrderStatus('3');
$this->User->findAllByUsernameAndPassword('jhon', '123');
$this->User->findAllByEmailOrUsername('jhon', 'jhon');
$this->User->findAllByLastName('psychic', array(),array('User.user_name' => 'asc'));
#findBy<fieldName>(string $value[, mixed $fields[, mixed $order]]);
$this->Recipe->findByType('Cookie');
$this->User->findByEmailOrUsername('jhon','jhon');
$this->User->findByUsernameAndPassword('jhon','123');
for ($i=0; $i < count($data['product_id']); $i++) {
$item[$i]['DiscountProduct']['discount_id'] = $this->Discount->id;
$item[$i]['DiscountProduct']['discount'] = $data['discount'][$i];
}
$this->DiscountProduct->saveAll($item);
必须在 Controller 的 action 里面使用这个:$this->request->data = $data;
修改的时候才能读取数据,并且 view 里面的 form 要使用 CakePHP 的
<?php echo $this->Form->create('PrintAd', array('type'=>'post')); ?>
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
echo $this->Html->link(
$this->Html->image($value['PrintClient']['weixin_code_img'], array('width'=>'60px')),
$value['PrintClient']['weixin_code_img'],
array('escape' => false)
);
$options['joins'] = array(
array(
'table' => 'channels',
'alias' => 'Channel',
'type' => 'LEFT',
'conditions' => array(
'Channel.id = Item.channel_id',
)
));
$Item->find('all', $options);
Router::fullbaseUrl()
public function__construct($request = null, $response = null)
{
parent::__construct($request, $response);
# code...
}
#array():
$this->params->pass
#第一个值:
$this->params->pass[0]
$this->loadModel('WifiList');
$this->SearchPagination->setup('WifiList');
$this->request->data['WifiList']['seller_id'] = SELLER_ID;
$this->paginate = array(
'fields' => array('WifiList.*', 'WxPersistentQrcodes.ticket'),
'conditions' => $this->WifiList->parseCriteria($this->request->data['WifiList']),
'order' => 'WifiList.id desc',
'joins' => array(
array(
'table'=>'wx_persistent_qrcodes',
'alias'=>'WxPersistentQrcodes',
'type'=>'LEFT',
'conditions'=>array(
'WifiList.wx_p_qrcode_id=WxPersistentQrcodes.scene_id and WxPersistentQrcodes.seller_id='.SELLER_ID
)
)
),
'limit' => 10
);
$data = $this->paginate('WifiList');
$this->set(compact('data'));
if(!$id){
throw new NotFoundException();
}
$this->redirect(array(
'controller'=>'dist',
'action'=>'result',
$status,
'?'=>array('sid'=>SELLER_ID,)
));
// the other model to load & use
App::uses('AnotherModel', 'Model');
class MyModel extends AppModel {
public $name = 'MyModel';
public function test() {
// load the Model
$anotherModel = new AnotherModel();
// use the Model
$anotherModel->save($data);
}
}