Teste Prático Flashcards

(31 cards)

1
Q

ActiveRecord\Config::initialize(function($cfg)
{
$cfg->set_model_directory(‘./models’);
$cfg->set_connections(
array(
‘development’ => ‘mysql://root@localhost/______’,
)
);
});

A

Nome da base de dados

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Criar o modelo

A

class Book extends \ActiveRecord\Model
{

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Criar as validações

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

validates_presence_of

A

static $validates_presence_of = array(
array(‘title’),
array(‘cover_blurb’, ‘message’ => ‘must be present and witty’)
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

validates_size_of/validates_length_of

A

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’)
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

validates_(in|ex)clusion_of

A

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’)
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

validates_format_of

A

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’)
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

validates_numericality_of

A

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)
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

validates_uniqueness_of

A

static $validates_uniqueness_of = array(
array(‘name’),
array(array(‘blah’,’bleh’), ‘message’ => ‘blah and bleh!’)
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

validate (custom)

A

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”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Métodos CRUD simples

A

index(), show($id), create(), store(), edit($id), update($id) e delete($id)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

public function index()
{
$books = Book::all();
//mostrar a vista index passando os dados por parâmetro
}

A

$this->renderView(‘book’,’index’, [‘books’ => $books]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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
}
}

A

$this->renderView(‘book’,’show’, [‘book’ => $book]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

public function create()
{
//mostrar a vista create
}

A

$this->renderView(‘book’,’create’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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
}
}

A

$this->redirectToRoute(‘book’,’index’);

$this->renderView(‘book’,’create’, [‘book’ => $book]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

public function edit($id)
{
$book = Book::find($id);
if (is_null($book)) {
//TODO redirect to standard error page
} else {
//mostrar a vista edit passando os dados por parâmetro
}
}

A

$this->renderView(‘book’, ‘edit’, [‘book’ => $book]);

17
Q

public function update($id)
{
$book = Book::find($id);
$book->update_attributes($this-> getHTTPPost());
if($book->is_valid()){
$book->save();
//redirecionar para o index
} else {
//mostrar vista edit passando o modelo como parâmetro
}
}

A

$this->redirectToRoute(‘book’,’index’);

$this->renderView(‘book’,’edit’, [‘book’ => $book]);

18
Q

public function delete($id)
{
$book = Book::find($id);
$book->delete();
//redirecionar para o index
}

A

$this->redirectToRoute(‘book’,’index’);

19
Q

Vista Create Simples
__________
__________________
<input></input>
<?php if(isset($book->errors)){ echo $book->errors->on(‘name’); }?>
_____

A

<div>
<label>Nome:</label>

</div>

20
Q

Vista Edit Simples
__________
__________________
<input></input>
<?php if (isset($book->errors)) { echo $book->errors->on(‘name’); } ?>

_____

A

<div>
<label>Nome:</label>

</div>

21
Q

Relacionamentos

A

static $belongs_to = array(
array(‘book’)
);

static $has_many = array(
array(‘chapters’)
);

22
Q

Vista Create com Relacionamentos

A

<label>Genre:</label><br></br>
<select>
<?php foreach($genres as $genre){?>
<option> <?= $genre->name; ?></option>
<?php } ?>
</select>

23
Q

Vista Edit com Relacionamentos

A

<label>Genre:</label><br></br>
<select>
<?php foreach($genres as $genre){?>
<?php if($genre->id == $book->genre_id) { ?>
<option><?= $genre->name;
?> </option>
<?php }else { ?>
<option> <?= $genre->name;
?></option>
<?php }
} ?>
</select>

24
Q

Método Index com Relacionamentos

A

public function index($id){
$book = Book::find($id);
$chapters = $book->chapters;
$this->renderView(‘chapter’, ‘index’, [‘chapters’ =>
$chapters]);
}

25
Vista Create Com Relacionamentos passar id
26
Método Store com Relacionamentos
public function store(){ $chapter = new Chapter($this-> getHTTPPost()); if($chapter->is_valid()){ $chapter->save(); $this->redirectToRoute('chapter','index',['id'=>$chapter->book_id]); } else { $this->renderView('chapter', 'create', ['chapter'=>$chapter]); } }
27
Método Create com Relacionamentos
public function create($id) { $book = Book::find($id); $this->renderView('chapter', 'create', ['book' => $book]); }
28
Metodo Show com Relacionamentos
public function show($id) { $chapter = Chapter::find($id); if (is_null($chapter)) { //TODO redirect to standard error page } else { $book_id = $chapter->book_id; $book = Book::find($book_id); // Show the "show" view and pass the data as parameters $this->renderView('chapter', 'show', ['book' => $book, 'chapter' => $chapter, 'book_id' => $book_id]); } }
29
Método Edit com Relacionamentos
public function edit($id) { $chapter = Chapter::find($id); if (is_null($chapter)) { //TODO redirect to standard error page } else { $this->renderView('chapter', 'edit', ['chapter' => $chapter]); } }
30
Método Update com Relacionamentos
public function update($id) { $chapter = Chapter::find($id); $chapter->update_attributes($this->getHTTPPost()); if ($chapter->is_valid()) { $chapter->save(); $this->redirectToRoute('chapter', 'index', ['id' => $chapter->book_id]); } else { $this->renderView('chapter', 'edit', ['chapter' => $chapter]); } }
31
Método Delete com Relacionamentos
public function delete($id) { $chapter = Chapter::find($id); $book_id = $chapter->book_id; $chapter->delete(); $this->redirectToRoute('chapter', 'index', ['id' => $book_id]); }