Можно переключить тип данных на целое число:
>>> n = 5.59
>>> int(n * 10) / 10.0
5.5
>>> int(n * 10 + 0.5)
56
И затем отображают число путем вставки десятичного разделителя локали.
Однако ответ Jimmy лучше.
AuthComponent :: identify ()
принимает два параметра: $ user
и $ conditions
if ($conditions === false) {
$conditions = null;
} elseif (is_array($conditions)) {
$conditions = array_merge((array)$this->userScope, $conditions);
} else {
$conditions = $this->userScope;
}
Глядя на приведенный выше фрагмент, если вы пройдете false
как $ conditions
, метод будет выполняться без модельных условий.
Кроме того, глядя на остальную часть кода, если вы передадите $ user
] значение типа строка
, он не будет выполнять большую часть кода, связанного с пользователем, пока не попадет сюда:
} elseif (!empty($user) && is_string($user)) {
$model =& $this->getModel();
$data = $model->find(array_merge(array($model->escapeField() => $user), $conditions));
if (empty($data) || empty($data[$this->userModel])) {
return null;
}
}
Здесь он запускает Model :: escapeField ()
, без parameters, который возвращает экранированную версию User.id
(по умолчанию) и сопоставляет это поле со строкой, которая была передана. Затем он объединяет это с массивом $ conditions
и выполняет a Model :: find ()
.
Можно с уверенностью сказать, что если строка является идентификатором пользователя и нет условий, она будет находить человека с этим идентификатором каждый раз.
Таким образом, вы должны иметь возможность расширить AuthComponent, чтобы делать то, что вы хотите так:
// app/controllers/components/app_auth.php
<?php
App::import('Component', 'Auth');
class AppAuthComponent extends AuthComponent {
/**
* Custom user identification
*/
function identify($user=null, $conditions=null) {
// get the model AuthComponent is configured to use
$model =& $this->getModel(); // default is User
// do a query that will find a User record when given successful login data
$user = $model->find('first', array('conditions' => array(
'EmailAddress.' . $this->fields['username'] => $user[$this->userModel][$this->fields['username']],
'User.' . $this->fields['password'] => $user[$this->userModel][$this->fields['password']],
));
// return null if user invalid
if (!$user) {
return null; // this is what AuthComponent::identify would return on failure
}
// call original AuthComponent::identify with string for $user and false for $conditions
return parent::identify($user[$this->userModel][$model->primaryKey], false);
}
}
?>
Вам придется заменить все ссылки на Auth на AppAuth в вашем приложении, если вы не последуете этому полезному совету (подход в комментариях хорош).