Hallo,
ich möchte schonmal vorab klären, dass ich mit cake gerade angefangen hab.
Folgendes Problem.
Ich habe mich an das Standard-Blog-Tutorial gehalten und bin gerade dabei, jedem Post ein Kategorie zu verpassen.
Die Ausgabe sieht so aus.
http://img3.imageshack.us/img3/2861/localhostcakeposts.png
Was ich will sind die Kategoriennamen.
Der PostView
|
PHP Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<h1>Mein erstes Blog</h1>
<?php echo $html->link('Post hinzufügen',array('controller' => 'posts', 'action' => 'add'))?>
<table>
<tr>
<th>Titel</th>
<th>Inhalt</th>
<th>Erstellt</th>
<th>Löschen</th>
<th>Kategorie</th>
</tr>
<!– Hier wird nun eine Schleife eingefügt, die das $posts Array abarbeitet und pro Array-Zeile eine Tabellenzeile ausgibt –>
<?php foreach($posts as $post):?>
<tr>
<td><?php echo $html->link($post["Post"]["titel"], "/posts/view/".$post["Post"]["id"]);?></td>
<td><?php echo $post["Post"]["inhalt"];?></td>
<td><?php echo $post["Post"]["created"];?></td>
<td>
<?php echo $html->link('Delete', array('action' => 'delete', $post['Post']['id']), null, 'Are you sure?' )?>
<?php echo $html->link('Edit', array('action'=>'edit', $post['Post']['id']));?>
</td>
<td><?php echo $post["Post"]["category_id"];?></td>
</tr>
<?php endforeach;?>
</table>
|
Der PostController
|
PHP Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<?php
class PostsController extends AppController
{
var $name = "Posts";
function index() {
$eintraege = $this->Post->find('all');
$this->set("posts",$eintraege);
}
function view($id = NULL) {
$this->Post->id = $id;
$this->set("post",$this->Post->read());
}
function add() {
$categories = $this->Post->Category->getCategories();
$this->set("cat", $categories);
if (!empty($this->data)) {
if ($this->Post->save($this->data)) {
$this->Session->setFlash("Der Beitrag wurde erfolgreich gespeichert");
$this->redirect("/posts");
} else $this->Session->setFlash("Fehler");
}
}
function delete($id) {
$this->Post->delete($id);
$this->Session->setFlash('The post with id: '.$id.' has been deleted.');
$this->redirect(array('action'=>'index'));
}
function edit($id = null) {
$this->Post->id = $id;
if (empty($this->data)) {
$this->data = $this->Post->read();
} else {
if ($this->Post->save($this->data)) {
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
}
}
}
}
?>
|
CatController
|
PHP Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
class CategoryController extends AppController
{
var $name = "Category";
function index() {
$eintraege = $this->Category->find('all');
$this->set("categories",$eintraege);
}
}
?>
|
CatModdel
|
PHP Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php
class Category extends AppModel
{
var $name = "Category";
function getCategories() {
$cats = $this->find('all');
$output = array();
foreach($cats as $cat) {
$output[$cat['Category']['id']] = $cat['Category']['name'];
}
return $output;
}
}
?>
|
PostModdel
|
PHP Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
class Post extends AppModel
{
var $name = "Post";
var $validate = array(
'titel' => array('rule' => 'notEmpty'),
'inhalt' => array('rule' => 'notEmpty'),
);
var $belongsTo = array('Category');
}
?>
|
Zudem würde ich auch gerne wissen wie ich den Select von der Kategorie validieren kann.
gruß