You say you are new to Cake - have you done any web or database development at
all? A select box is usually populated from a table that is joined or
associated with the table you are updating. You only need to store the id of
the related row in 'this' table. To find out what value that id relates to, you
do a query with a join. This is standard referential integrity stuff and not
just Cake specific.
So if you had a table that contained trees and you wanted to know what fruit
each tree grows, you'd have a 'trees' table with these fields called 'id,
'name', 'fruit_id'. Then you'd have a table called 'fruits' with fields called
'id' and 'name' (this is the Cake convention - and it is worth sticking to it).
If your fruits table looks like this:
id : name
1 : Apples
2 : Oranges
3 : Cherries
4 : Peaches
...and you have a row in your trees table like this:
id : name : fruit_id
1 : My cherry tree : 3
...you look up the fruit_id (3) in your fruits table, and you can see that 3 =
Cherries. Therefore the tree with the id of 1 grows cherries.
This gives you enormous flexibility in the future. I know it's highly unlikely
that you'd ever want to do this with fruits and trees, but let's say your tree
suddenly starts growing oranges instead of cherries. All you do update the row
of the trees table where the id is 1, changing the value in the fruit_id field
from 3 to 2. Now when you do the same look up you see that the tree grows
oranges.
Perhaps a better example is if you wanted to be more precise, and instead of
recording your fruit as 'Cherries' you want to record it as 'Red cherries'. All
you do is update the 'name' field in the 'fruits' table from 'Cherries' to 'Red
cherries'. Now when you do your query all trees that used to grow cherries now
grow red cherries. That's a single update. If you were to do what you want to
do and store the name of the fruit in the trees table, you'd have to update
multiple rows in the trees table instead. That sounds simple enough, but
imagine if you had to do that for your entire orchard - that's a lot of
updates. And then you have the potential problems caused by a random user
changing a couple of trees from 'Cherries' to 'Cheeries' by mistake.
If you are new to Cake and these concepts, I'd recommend you read the following
parts of the online guide. In particular, look at the section on Baking. This
is invaluable stuff. Al you do is build your database, point Cake at it and it
will build a simple app for you. Then you can look at the code and see how it
all works, and go on from there.
http://book.cakephp.org/view/1000/Models
http://book.cakephp.org/view/1522/Code-Generation-with-Bake
Jeremy Burns
jeremybu...@me.com
On 18 May 2010, at 20:22, csj16 wrote:
>
> I have 2 tables and models 'Friend' and 'Message'. I have a belongsTo
> association between them, and this is all working fine.
>
> The select box in my form is being populated with the correct field, that's
> perfect, BUT when the form is submitted it saves the ID of that field not
> the name. Which is obvious as its a INTEGAR field.
>
> But I would like to find out if it's at all possible to ALSO save the name
> that shows in the drop down-list. So let's say the drop-down list is
> populated with different types of fruits (apples, oranges, cherries and
> peaches) but when the form is submitted/saved it doesn't save the name of
> the fruit it saves the ID of that field. Is there anyway to also save the
> name of the fruit into the database?
>
> I am new to CakePHP so I am utterly clueless, but would really appreciate
> some help.
> --
> View this message in context:
> http://old.nabble.com/Save-select-box-value-not-ID-tp28599700p28599700.html
> Sent from the CakePHP mailing list archive at Nabble.com.
>
> Check out the new CakePHP Questions site http://cakeqs.org and help others
> with their CakePHP related questions.
>
> You received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com
> To unsubscribe from this group, send email to
> cake-php+unsubscr...@googlegroups.com For more options, visit this group at
> http://groups.google.com/group/cake-php?hl=en
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en