-
Notifications
You must be signed in to change notification settings - Fork 2
Salvando um Model
Carlos Eduardo edited this page Mar 23, 2016
·
3 revisions
Model /root/app/example/modal/usermodel.class.php
<?php
namespace Model;
use \Core\Model;
class UserModel extends Model {
protected $id;
protected $name;
protected $password;
public function getId() {
return $this->id;
}
protected function setId($id) {
$this->id = $id;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getPassword() {
return $this->password;
}
public function setPassword($password) {
$this->password = $password;
}
protected function preSave() {
$this->_setError(NULL);
if (empty($this->name)) {
$this->_setError('Informe um nome...');
return FALSE;
}
if (empty($this->email)) {
$this->_setError('Informe um e-mail...');
return FALSE;
}
if (empty($this->senha)) {
$this->_setError('Informe uma senha...');
return FALSE;
}
return TRUE;
}
}
?>
View /root/app/example/view/page/cadastrar.phtml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cadastrar</title>
</head>
<body>
<form action="<?=self::href('user/register')?>">
<input type="hidden" name="mvc:model" value="User">
<label>Nome:</label>
<input type="text" name="name"><br><br>
<label>Senha:</label>
<input type="text" name="password"><br><br>
</form>
</body>
</html>
Controller /root/app/example/controller/usercontroller.class.php
<?php
namespace Controller;
use \Core\Controller;
use \Model\UserModel;
class UserController extends Controller {
public function cadastrar() {
return $this->load->view('page/cadastrar');
}
public function register(UserModel $user = NULL) {
if (is_null($user))
return 'Erro...';
if ($user->save())
return 'Salvo com sucesso.';
else
return 'Erro ao tentar salvar: ' . $user->_getError();
}
}