cakePHP - Category_ID - brauche die Ausgabe von dem Kategorienamen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • cakePHP - Category_ID - brauche die Ausgabe von dem Kategorienamen

    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.

    img3.imageshack.us/img3/2861/localhostcakeposts.png

    Was ich will sind die Kategoriennamen.


    Der PostView

    Quellcode

    1. <h1>Mein erstes Blog</h1>
    2. <?php echo $html->link('Post hinzufügen',array('controller' => 'posts', 'action' => 'add'))?>
    3. <table>
    4. <tr>
    5. <th>Titel</th>
    6. <th>Inhalt</th>
    7. <th>Erstellt</th>
    8. <th>Löschen</th>
    9. <th>Kategorie</th>
    10. </tr>
    11. <!– Hier wird nun eine Schleife eingefügt, die das $posts Array abarbeitet und pro Array-Zeile eine Tabellenzeile ausgibt –>
    12. <?php foreach($posts as $post):?>
    13. <tr>
    14. <td><?php echo $html->link($post["Post"]["titel"], "/posts/view/".$post["Post"]["id"]);?></td>
    15. <td><?php echo $post["Post"]["inhalt"];?></td>
    16. <td><?php echo $post["Post"]["created"];?></td>
    17. <td>
    18. <?php echo $html->link('Delete', array('action' => 'delete', $post['Post']['id']), null, 'Are you sure?' )?>
    19. <?php echo $html->link('Edit', array('action'=>'edit', $post['Post']['id']));?>
    20. </td>
    21. <td><?php echo $post["Post"]["category_id"];?></td>
    22. </tr>
    23. <?php endforeach;?>
    24. </table>
    Alles anzeigen


    Der PostController

    Quellcode

    1. <?php
    2. class PostsController extends AppController
    3. {
    4. var $name = "Posts";
    5. function index() {
    6. $eintraege = $this->Post->find('all');
    7. $this->set("posts",$eintraege);
    8. }
    9. function view($id = NULL) {
    10. $this->Post->id = $id;
    11. $this->set("post",$this->Post->read());
    12. }
    13. function add() {
    14. $categories = $this->Post->Category->getCategories();
    15. $this->set("cat", $categories);
    16. if (!empty($this->data)) {
    17. if ($this->Post->save($this->data)) {
    18. $this->Session->setFlash("Der Beitrag wurde erfolgreich gespeichert");
    19. $this->redirect("/posts");
    20. } else $this->Session->setFlash("Fehler");
    21. }
    22. }
    23. function delete($id) {
    24. $this->Post->delete($id);
    25. $this->Session->setFlash('The post with id: '.$id.' has been deleted.');
    26. $this->redirect(array('action'=>'index'));
    27. }
    28. function edit($id = null) {
    29. $this->Post->id = $id;
    30. if (empty($this->data)) {
    31. $this->data = $this->Post->read();
    32. } else {
    33. if ($this->Post->save($this->data)) {
    34. $this->Session->setFlash('Your post has been updated.');
    35. $this->redirect(array('action' => 'index'));
    36. }
    37. }
    38. }
    39. }
    40. ?>
    Alles anzeigen



    CatController

    Quellcode

    1. <?php
    2. class CategoryController extends AppController
    3. {
    4. var $name = "Category";
    5. function index() {
    6. $eintraege = $this->Category->find('all');
    7. $this->set("categories",$eintraege);
    8. }
    9. }
    10. ?>
    Alles anzeigen


    CatModdel

    Quellcode

    1. <?php
    2. class Category extends AppModel
    3. {
    4. var $name = "Category";
    5. function getCategories() {
    6. $cats = $this->find('all');
    7. $output = array();
    8. foreach($cats as $cat) {
    9. $output[$cat['Category']['id']] = $cat['Category']['name'];
    10. }
    11. return $output;
    12. }
    13. }
    14. ?>
    Alles anzeigen



    PostModdel

    Quellcode

    1. <?php
    2. class Post extends AppModel
    3. {
    4. var $name = "Post";
    5. var $validate = array(
    6. 'titel' => array('rule' => 'notEmpty'),
    7. 'inhalt' => array('rule' => 'notEmpty'),
    8. );
    9. var $belongsTo = array('Category');
    10. }
    11. ?>
    Alles anzeigen



    Zudem würde ich auch gerne wissen wie ich den Select von der Kategorie validieren kann.

    gruß