Hi guys,

I've never been successful to get HABTM to work. I've tried so many
sites such as this: 
http://www.packtpub.com/article/working-with-complex-associations-using-cakephp
and never got it to work. According to the number of rows, it gets the
correct number of rows, and when I tried the resulting debug SQL in
the bottom (debug = 2), it returns properly, but the resulting array
doesn't contain the data that I want.

I'm getting crosseyed and frustrated and do not want to leave CakePHP
but I've got to be successful on HABTM or it's a deal breaker. Please
help :).

### model/author.php
class Author extends AppModel {
        var $name = 'Author';
        var $hasAndBelongsToMany = array(
                'Book' => array(
                        'className' => 'Book',
                        'joinTable' => 'authors_books',
                        'foreignKey' => 'author_id',
                        'associationForeignKey' => 'book_id'
                )
        );
}


### model/book.php ###
class Book extends AppModel {
        var $name = 'Book';
        var $hasAndBelongsToMany = 'Author';
}

### controller/books_controller.php
class BooksController extends AppController {
        var $name = 'Books';
        function index() {
                $this->Book->recursive = 1;
                $books = $this->Book->find('all');
                $this->set('books', $books);
        }
}

### controller/author_controller.php
class AuthorsController extends AppController {
        var $name = 'Authors';
        function index() {
                $this->Author->recursive = 1;
                $authors = $this->Author->find('all');
                $this->set('authors', $authors);
        }
}

### views/authors/index.ctp
<?php foreach($authors as $author): ?>
<h2><?php echo $author['Author']['name'] ?></h2>
<hr />
<h3>Book(s):</h3>
<ul>
<?php foreach($author['Book'] as $book): ?>
<li><?php echo $book['title'] ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>

### views/books/index.ctp
<?php foreach($books as $book): ?>
<h2><?php echo $book['Book']['title'] ?></h2>
<hr />
<h3>Author(s):</h3>
<ul>
<?php foreach($book['Author'] as $author): ?>
<li><?php echo $author['name'] ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>

These are my tables:
+------------------+
| Tables_in_cake01 |
+------------------+
| authors          |
| authors_books    |
| books            |
+------------------+
mysql> select * from authors;
+------+---------+--------------------+-----------------------+
| id   | name    | email              | website               |
+------+---------+--------------------+-----------------------+
| 0001 | author1 | [email protected] | http://www.author.com |
| 0002 | author2 | [email protected] | http://www.author.com |
| 0003 | author3 | [email protected] | http://www.author.com |
| 0004 | author4 | [email protected] | http://www.author.com |
| 0005 | author5 | [email protected] | http://www.author.com |
+------+---------+--------------------+-----------------------+
mysql> select * from books;
+------+-----------+-------+-----------------------------------+
| id   | isbn      | title | description                       |
+------+-----------+-------+-----------------------------------+
| 0001 | 123456789 | book1 | This is the description for book1 |
| 0002 |           | book2 | this is description for book2     |
| 0003 |           | book3 | this is description for book3     |
| 0004 |           | book4 | this is description for book4     |
| 0005 |           | book5 | this is description for book5     |
+------+-----------+-------+-----------------------------------+
mysql> select * from authors_books;
+-----------+---------+
| author_id | book_id |
+-----------+---------+
|         1 |       1 |
|         2 |       3 |
|         3 |       1 |
|         4 |       1 |
|         2 |       4 |
+-----------+---------+

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to