I recommend a hasAndBelongsToMany. Remember, a trip could have multiple locations, not just two.
Up vote 0 down vote favorite share g+ share fb share tw.
I am working on a setup where I am unsure how to get correct associations setup. Cakephp 1.3 I am thinking something like this, please correct me if I am wrong! Database: CREATE TABLE IF NOT EXISTS `trips` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `user_id` bigint(20) NOT NULL, `location_to` bigint(20) NOT NULL, `location_from` bigint(20) NOT NULL, `published` int(1) DEFAULT NULL, `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ; CREATE TABLE IF NOT EXISTS `locations` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL PRIMARY KEY (`id`) ); CREATE TABLE IF NOT EXISTS `locations_from` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `trip_id` bigint(20) NOT NULL, `location_id` bigint(20) NOT NULL PRIMARY KEY (`id`) ); CREATE TABLE IF NOT EXISTS `locations_to` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `trip_id` bigint(20) NOT NULL, `location_id` bigint(20) NOT NULL PRIMARY KEY (`id`) ); But how to setup the models TRIP and LOCATION for correct HABTM?
Trips belongsTo USER Trips has a from location and to location => From New York, To Miami Locations => New York, Miami +++ Use belongsTO og hasandbelongstomany? Working with several identifiers is a first for me. Anyone that can give some assistance?
Thanks! -Tom cakephp associations models habtm link|improve this question edited Mar 23 at 19:35Dave3,508420 asked Mar 23 at 18:02Tom617 75% accept rate.
I recommend a hasAndBelongsToMany. Remember, a trip could have multiple locations, not just two. I would re-construct your tables like this: CREATE TABLE IF NOT EXISTS `trips` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `user_id` bigint(20) NOT NULL, `published` int(1) DEFAULT NULL, `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ; CREATE TABLE IF NOT EXISTS `locations` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL PRIMARY KEY (`id`) ); CREATE TABLE IF NOT EXISTS `locations_trips` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `location_id` bigint(20) NOT NULL `trip_id` bigint(20) NOT NULL, PRIMARY KEY (`id`) ); and let CakePHP bake this and take care of the rest (it will automatically pick up the HABTM relationship from the common table).
EDIT: Sorry, based on your comments, I think this would be better: CREATE TABLE IF NOT EXISTS `trips` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `user_id` bigint(20) NOT NULL, `locationfrom_id` bigint(20) NOT NULL, `locationto_id` bigint(20) NOT NULL, `published` int(1) DEFAULT NULL, `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ; CREATE TABLE IF NOT EXISTS `locations` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL PRIMARY KEY (`id`) ); and then use multiple relations to the same model as below: http://book.cakephp.org/1.3/view/1046/Multiple-relations-to-the-same-model For your example, locationfrom_id and locationto_id would belongsTo two classes that would have the same "className", which is Locations.
Hi.. This is similar to what I was thinking, but my setup will be based on, USER is going FROM location to LOCATION at TIMEDATE (dont bother with timedate fields, will insert when I get habtm correct). IF possible to insert more location, like, Going from NY to Miami, passing trough DENVER. Like you say, can have multiple locations.
– Tom Mar 23 at 20:11 OK then your original setup was correct, I would just change the table names to "locationtos" and "locationfroms" and have the foreign key ids be "locationto_id" and "locationfrom_id" to make it consistent with CakePHP's naming conventions. – srs2012 Mar 23 at 21:23 And use hasAndBelongsToMany? But what in what model?
That is the question ;) Thanks for your time! – Tom Mar 23 at 21:29 Ah, no I don't think you need a HABTM in your setup. Just a simple belongsTo and hasMany would be fine.
If you name your tables and fields right, the baking script will actually take care of the relationship for you. :) – srs2012 Mar 23 at 21:35 Okei.. I will give it a go and see how far I get ;) Thanks =) – Tom Mar 23 at 22:06.
BelongsTo is the correct way to represent it because you have only 2 locations by trip and those 2 locations have different meanings. HABTM would have been correct if you had defined a trip this way : During a trip, you go to many locations and you don't know how many.
If you are really planing on using more than two locations for a single trip you should create a new table which associates the trips with the locations and give them an order. CREATE TABLE IF NOT EXISTS `location_trips` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `location_id` bigint(20) NOT NULL, `trip_id` bigint(20) NOT NULL, `number` bigint(20) NOT NULL PRIMARY KEY (`id`) ); The HABTM association would than be applied to the Trip model. If you want to find trips while using the Location model as well, you have to define it there, too.
;) Greetings func0der.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.