Thank you for your reply! Table req_questions is a table to define
questions, and table books just has the fields questions and
req_questions to list question_ids in these two fields. Table books
has other fields like category, articles, sections and segments, I
didn't include them because I just want to edit questions and
req_question fields, and delete question_ids in these 2 fields when I
delete question_id in table questions. I am not allowed to change
table. At first I was using $hasMany in model Question. For example,
var $hasMany = array('Book' =>'Book');
it gave me empty array because it requires these two tables associated
by a foreign key, question_id is not a foreign key in table books.
Below are my table look like:
books
book_id questions req_questions title
1 11, 23, 33 23, 33 This is a
book
2 18, 23, 39 23 This is
another book
questions
question_id question_title question_data
11 question 11 this is a question
18 question 18 this is another questions
23 question 23 this is another questions
33 question 33 another questions
If I set var primaryKey ='question_id' in Model Question, I think cake
will recognize $question_id is $id.
I am not allowed to change the table structures, I am hoping someone
can help me to find a way to connect fields questions and
req_questions to question_id.
Thanks!
Lynda
On Feb 13, 2:42 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> Hi Lynda,
>
> I think the association you need is a hasMany since book can have many
> questions. I assume a question is specific to a book, so the question
> model would have a belongsTo association with book.
>
> However you have two there pointing to the same table so it's a bit
> tricky. What is the purpose of the questions and req_questions?
> Maybe there's another way to achieve the same result? Can you simply
> put a value (1 or 0) in the questions table to denote whether it's a
> normal quesiton or req_question?
>
> Also as an aside, the primary key for "books" and "questions" should
> be "id" in both cases not "book_id" and "question_id"
> It took me a while to understand this as well. :-) Cake expects the
> primary key of every table to be "id" and you can then refer to it
> from other tables as "modelname_id"
>
> e.g.
> You could have a table "notes" to add notes about each book.
> This table would have a field "book_id" which cake automatically links
> to the "id" field in "books" if you setup a belongsTo association in
> the "notes" model and a hasMany association in the "books" model.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" 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
-~----------~----~----~----~------~----~------~--~---