コード例 #1
0
	public function actionCreate($mode)
	{
		switch($mode)
		{
			case 'user':
				$model = new YumUser;
				$profile = new YumProfile;
				break;
			default:
		}
		$username = $_POST['username'] or $this->_sendResponse(
				501, 'Username missing');
		$password = $_POST['password'] or $this->_sendResponse(
				501, 'Password missing');

		foreach($_POST as $key => $value)
			if($profile->hasAttribute($key))
				$profile->$key = $value;

		if($profile->validate() && $model->register($username, $password, $profile))
			$this->_sendResponse(200, 
					CJSON::encode(array($model, $profile)));	
		else {
			$msg = "<h1>Error</h1>";
			$msg .= sprintf("Couldn't create %s", $mode);
			$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>";
			}

			foreach($profile->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 );

		}
	}