本文整理匯總了PHP中DefaultController::update方法的典型用法代碼示例。如果您正苦於以下問題:PHP DefaultController::update方法的具體用法?PHP DefaultController::update怎麽用?PHP DefaultController::update使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類DefaultController
的用法示例。
在下文中一共展示了DefaultController::update方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: actionUpdate
public function actionUpdate()
{
switch ($_GET['model']) {
// Find respective model
case 'Contacts':
$model = Contacts::model()->findByPk($_GET['id']);
$this->modelClass = "Contacts";
$temp = $model->attributes;
break;
case 'Actions':
$model = Actions::model()->findByPk($_GET['id']);
$this->modelClass = "Actions";
$temp = $model->attributes;
break;
case 'Accounts':
$model = Accounts::model()->findByPk($_GET['id']);
$this->modelClass = "Accounts";
$temp = $model->attributes;
break;
default:
$this->_sendResponse(501, sprintf('Error: Mode <b>update</b> is not implemented for model <b>%s</b>', $_GET['model']));
exit;
}
// Did we find the requested model? If not, raise an error
if (is_null($model)) {
$this->_sendResponse(400, sprintf("Error: Didn't find any model <b>%s</b> with ID <b>%s</b>.", $_GET['model'], $_GET['id']));
}
// Try to assign PUT parameters to attributes
foreach ($_POST as $var => $value) {
// Does the model have this attribute? If not raise an error
if ($model->hasAttribute($var)) {
$model->{$var} = $value;
} else {
$this->_sendResponse(500, sprintf('Parameter <b>%s</b> is not allowed for model <b>%s</b>', $var, $_GET['model']));
}
}
// Try to save the model
switch ($_GET['model']) {
// Get an instance of the respective model
case 'Contacts':
Yii::import("application.modules.contacts.controllers.DefaultController");
$controller = new DefaultController('DefaultController');
if ($controller->create($model, $temp, '1')) {
$this->_sendResponse(200, sprintf('Model <b>%s</b> was updated with name <b>%s</b>', $_GET['model'], $model->firstName . " " . $model->lastName));
} else {
// Errors occurred
$msg = "<h1>Error</h1>";
$msg .= sprintf("Couldn't update model <b>%s</b>", $_GET['model']);
$msg .= "<ul>";
foreach ($model->errors as $attribute => $attr_errors) {
$msg .= "<li>Attribute: {$attribute}</li>";
$msg .= "<ul>";
foreach ($attr_errors as $attr_error) {
$msg .= "<li>{$attr_error}</li>";
}
$msg .= "</ul>";
}
$msg .= "</ul>";
$this->_sendResponse(500, $msg);
}
break;
case 'Accounts':
Yii::import("application.modules.accounts.controllers.DefaultController");
$controller = new DefaultController('DefaultController');
if ($controller->update($model, $temp, '1')) {
$this->_sendResponse(200, sprintf('Model <b>%s</b> with ID <b>%s</b> was updated.', $_GET['model'], $model->id));
} else {
// Errors occurred
$msg = "<h1>Error</h1>";
$msg .= sprintf("Couldn't update model <b>%s</b>", $_GET['model']);
$msg .= "<ul>";
foreach ($model->errors as $attribute => $attr_errors) {
$msg .= "<li>Attribute: {$attribute}</li>";
$msg .= "<ul>";
foreach ($attr_errors as $attr_error) {
$msg .= "<li>{$attr_error}</li>";
}
$msg .= "</ul>";
}
$msg .= "</ul>";
$this->_sendResponse(500, $msg);
}
break;
case 'Actions':
Yii::import("application.modules.actions.controllers.DefaultController");
$controller = new DefaultController('DefaultController');
if ($controller->update($model, $temp, '1')) {
$this->_sendResponse(200, sprintf('Model <b>%s</b> with ID <b>%s</b> was updated.', $_GET['model'], $model->id));
} else {
// Errors occurred
$msg = "<h1>Error</h1>";
$msg .= sprintf("Couldn't update model <b>%s</b>", $_GET['model']);
$msg .= "<ul>";
foreach ($model->errors as $attribute => $attr_errors) {
$msg .= "<li>Attribute: {$attribute}</li>";
$msg .= "<ul>";
foreach ($attr_errors as $attr_error) {
$msg .= "<li>{$attr_error}</li>";
}
$msg .= "</ul>";
//.........這裏部分代碼省略.........