Teste Prático Flashcards
ActiveRecord\Config::initialize(function($cfg)
{
$cfg->set_model_directory(‘./models’);
$cfg->set_connections(
array(
‘development’ => ‘mysql://root@localhost/______’,
)
);
});
Nome da base de dados
Criar o modelo
class Book extends \ActiveRecord\Model
{
}
Criar as validações
- validates_presence_of
- validates_size_of/validates_length_of
- validates_(in|ex)clusion_of
- validates_format_of
- validates_numericality_of
- validates_uniqueness_of
- validate *custom
validates_presence_of
static $validates_presence_of = array(
array(‘title’),
array(‘cover_blurb’, ‘message’ => ‘must be present and witty’)
);
validates_size_of/validates_length_of
static $validates_size_of = array(
array(‘title’, ‘within’ => array(1,5), ‘too_short’ => ‘too short!’),
array(‘cover_blurb’, ‘is’ => 20),
array(‘description’, ‘maximum’ => 10, ‘too_long’ => ‘should be short and sweet’)
);
validates_(in|ex)clusion_of
static $validates_inclusion_of = array(
array(‘fuel_type’, ‘in’ => array(‘petroleum’, ‘hydrogen’, ‘electric’)),
);
static $validates_exclusion_of = array(
array(‘password’, ‘in’ => array(‘god’, ‘sex’, ‘password’, ‘love’, ‘secret’),
‘message’ => ‘should not be one of the four most used passwords’)
);
validates_format_of
static $validates_format_of = array(
array(‘email’, ‘with’ =>’/^[^0-9][A-z0-9]+([.][A-z0-9]+)[@][A-z0-9]+([.][A-z0-9]+)[.][A-z]{2,4}$/’)
array(‘password’, ‘with’ =>’/^.(?=.{8,})(?=.\d)(?=.[a-z])(?=.[A-Z]).*$/’, ‘message’ => ‘is too weak’)
);
validates_numericality_of
static $validates_numericality_of = array(
array(‘price’, ‘greater_than’ => 0.01),
array(‘quantity’, ‘only_integer’ => true),
array(‘shipping’, ‘greater_than_or_equal_to’ => 0),
array(‘discount’, ‘less_than_or_equal_to’ => 5, ‘greater_than_or_equal_to’ => 0)
);
validates_uniqueness_of
static $validates_uniqueness_of = array(
array(‘name’),
array(array(‘blah’,’bleh’), ‘message’ => ‘blah and bleh!’)
);
validate (custom)
public function validate()
{
if ($this->first_name == $this->last_name)
{
$this->errors->add(‘first_name’, “can’t be the same as Last Name”);
$this->errors->add(‘last_name’, “can’t be the same as First Name”);
}
}
Métodos CRUD simples
index(), show($id), create(), store(), edit($id), update($id) e delete($id)
public function index()
{
$books = Book::all();
//mostrar a vista index passando os dados por parâmetro
}
$this->renderView(‘book’,’index’, [‘books’ => $books]);
public function show($id)
{
$book = Book::find($id);
if (is_null($book)) {
//TODO redirect to standard error page
} else {
//mostrar a vista show passando os dados por parâmetro
}
}
$this->renderView(‘book’,’show’, [‘book’ => $book]);
public function create()
{
//mostrar a vista create
}
$this->renderView(‘book’,’create’);
public function store()
{
$book = new Book($this-> getHTTPPost());
if($book->is_valid()){
$book->save();
//redirecionar para o index
} else {
//mostrar vista create passando o modelo como parâmetro
}
}
$this->redirectToRoute(‘book’,’index’);
$this->renderView(‘book’,’create’, [‘book’ => $book]);