0
我使用beforeSave()
爲用戶分配臨時客戶編號。我需要將此值返回給訪問該API的設備。是否可以從我的控制器訪問?在cakePHP中使用beforeSave()修改的訪問值
// app/Model/User.php
<?php
class User extends AppModel {
function beforeSave($options) {
$this->data['User']['customerNumber'] = uniqid(); // This is the value I want
$this->data['User']['password'] = md5($this->data['User']['password']);
}
function isUnique() {
$users = $this->find('all', array('conditions' => array('email' => $this->data['User']['email'])));
if (empty($users)) {
return true;
} else {
return false;
}
}
}
?>
// app/Controller/UserController.php
<?php
class UserController extends AppController {
public $components = array('RequestHandler');
public function register() {
if ($this->request->is('post')) {
$this->User->set($this->data);
if ($this->User->isUnique()) {
if ($this->User->save($this->data)) {
// This is where I need to return the customer number
echo json_encode(array('status' => 'User registered', 'customerNumber' => $this->data['customerNumber']));
} else {
echo json_encode(array('status' => 'User could not be registered'));
}
} else {
echo json_encode(array('status' => 'user is duplicate'));
}
} else {
echo json_encode(array('error' => 'Requests must be made using HTTP POST'));
}
}
}
?>
作爲第二個問題,是uniqid()
分配臨時的客戶數的好方法嗎?
由於一噸! – drewwyatt 2012-04-29 16:54:57