Drop down assocations

View: New views
4 Messages — Rating Filter:   Alert me  

Drop down assocations

by supergeek79@gmail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I'm new to cakePhp (and database design...I've been lazy over the
years and want to learn the "right" way to do things) and have a
question about saving the contents of a form that has a drop down
field.

Here are the tables that I have setup in my database:

Table 1: posts (contains id,title,description)

Table 2: categories (contains id,category_name)

Table 3: categories_posts (contains 2 foreign keys:
category_id,post_id)

I have 3 fields in the form I'm building

Field 1 (Category): Drop down field containing a list of categories.
The category id is the value and the category name is shown on the
drop down.

Field 2 (Title): text field

Field 3 (Description): textarea field

I'm having trouble figuring out how to save the Title and Description
fields to one database table, and save the category id and post id to
an assocation table, called categories_posts.

I have tried everything I can find an example of, and nothing has
seemed to work. I'm also not really sure if I need to use the save or
saveAll method.

In the past, I would have just saved the category name to the post
data table and been done with it, but I would like to at least attempt
to normalize my database (which is really foreign to me at this
point...I understand the basic idea but that's about it at this
point).

Can someone please point me in the right direction? If I need to
further explain myself, please let me know. Thank you.

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


Re: Drop down assocations

by Robert P :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Have another review of the concept of normalising, then read
http://book.cakephp.org/view/81/belongsTo

You have normalised your tables to a HABTM relationship, when what you
seem to be after is a hasMany/belongsTo relationship.
    Post belongsTo Category
    Category hasMany Post

When that is done, the dropdown can be automatically generated for you
in your Post views using:
    <?php echo $form->input('Category'); ?>

On Nov 7, 6:52 am, "supergee...@..." <cda...@...>
wrote:

> I'm new to cakePhp (and database design...I've been lazy over the
> years and want to learn the "right" way to do things) and have a
> question about saving the contents of a form that has a drop down
> field.
>
> Here are the tables that I have setup in my database:
>
> Table 1: posts (contains id,title,description)
>
> Table 2: categories (contains id,category_name)
>
> Table 3: categories_posts (contains 2 foreign keys:
> category_id,post_id)
>
> I have 3 fields in the form I'm building
>
> Field 1 (Category): Drop down field containing a list of categories.
> The category id is the value and the category name is shown on the
> drop down.
>
> Field 2 (Title): text field
>
> Field 3 (Description): textarea field
>
> I'm having trouble figuring out how to save the Title and Description
> fields to one database table, and save the category id and post id to
> an assocation table, called categories_posts.
>
> I have tried everything I can find an example of, and nothing has
> seemed to work. I'm also not really sure if I need to use the save or
> saveAll method.
>
> In the past, I would have just saved the category name to the post
> data table and been done with it, but I would like to at least attempt
> to normalize my database (which is really foreign to me at this
> point...I understand the basic idea but that's about it at this
> point).
>
> Can someone please point me in the right direction? If I need to
> further explain myself, please let me know. Thank you.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Drop down assocations

by supergeek79@gmail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks. I'll try that.

On Nov 7, 11:31 am, Robert P <shiftyrobs...@...> wrote:

> Have another review of the concept of normalising, then readhttp://book.cakephp.org/view/81/belongsTo
>
> You have normalised your tables to a HABTM relationship, when what you
> seem to be after is a hasMany/belongsTo relationship.
>     Post belongsTo Category
>     Category hasMany Post
>
> When that is done, the dropdown can be automatically generated for you
> in your Post views using:
>     <?php echo $form->input('Category'); ?>
>
> On Nov 7, 6:52 am, "supergee...@..." <cda...@...>
> wrote:
>
>
>
> > I'm new to cakePhp (and database design...I've been lazy over the
> > years and want to learn the "right" way to do things) and have a
> > question about saving the contents of a form that has a drop down
> > field.
>
> > Here are the tables that I have setup in my database:
>
> > Table 1: posts (contains id,title,description)
>
> > Table 2: categories (contains id,category_name)
>
> > Table 3: categories_posts (contains 2 foreign keys:
> > category_id,post_id)
>
> > I have 3 fields in the form I'm building
>
> > Field 1 (Category): Drop down field containing a list of categories.
> > The category id is the value and the category name is shown on the
> > drop down.
>
> > Field 2 (Title): text field
>
> > Field 3 (Description): textarea field
>
> > I'm having trouble figuring out how to save the Title and Description
> > fields to one database table, and save the category id and post id to
> > an assocation table, called categories_posts.
>
> > I have tried everything I can find an example of, and nothing has
> > seemed to work. I'm also not really sure if I need to use the save or
> > saveAll method.
>
> > In the past, I would have just saved the category name to the post
> > data table and been done with it, but I would like to at least attempt
> > to normalize my database (which is really foreign to me at this
> > point...I understand the basic idea but that's about it at this
> > point).
>
> > Can someone please point me in the right direction? If I need to
> > further explain myself, please let me know. Thank you.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Drop down assocations

by supergeek79@gmail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks! That worked like a charm.

On Nov 7, 11:31 am, Robert P <shiftyrobs...@...> wrote:

> Have another review of the concept of normalising, then readhttp://book.cakephp.org/view/81/belongsTo
>
> You have normalised your tables to a HABTM relationship, when what you
> seem to be after is a hasMany/belongsTo relationship.
>     Post belongsTo Category
>     Category hasMany Post
>
> When that is done, the dropdown can be automatically generated for you
> in your Post views using:
>     <?php echo $form->input('Category'); ?>
>
> On Nov 7, 6:52 am, "supergee...@..." <cda...@...>
> wrote:
>
>
>
> > I'm new to cakePhp (and database design...I've been lazy over the
> > years and want to learn the "right" way to do things) and have a
> > question about saving the contents of a form that has a drop down
> > field.
>
> > Here are the tables that I have setup in my database:
>
> > Table 1: posts (contains id,title,description)
>
> > Table 2: categories (contains id,category_name)
>
> > Table 3: categories_posts (contains 2 foreign keys:
> > category_id,post_id)
>
> > I have 3 fields in the form I'm building
>
> > Field 1 (Category): Drop down field containing a list of categories.
> > The category id is the value and the category name is shown on the
> > drop down.
>
> > Field 2 (Title): text field
>
> > Field 3 (Description): textarea field
>
> > I'm having trouble figuring out how to save the Title and Description
> > fields to one database table, and save the category id and post id to
> > an assocation table, called categories_posts.
>
> > I have tried everything I can find an example of, and nothing has
> > seemed to work. I'm also not really sure if I need to use the save or
> > saveAll method.
>
> > In the past, I would have just saved the category name to the post
> > data table and been done with it, but I would like to at least attempt
> > to normalize my database (which is really foreign to me at this
> > point...I understand the basic idea but that's about it at this
> > point).
>
> > Can someone please point me in the right direction? If I need to
> > further explain myself, please let me know. Thank you.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---