⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Database relationship question



ufodisko
  • profile picture
  • Member

Posted 30 September 2012 - 19:38 PM

Here's the situation.

I have 3 tables; contact, properties and property_contact (the relation table)
the relational table is there to allow me to save all the properties that a certain contact has seen so far.

This is my DB schema


DROP TABLE IF EXISTS `contacts`;
CREATE TABLE IF NOT EXISTS `contacts` (
`contactID` int(11) NOT NULL AUTO_INCREMENT,
`firstName` varchar(50) NOT NULL,
`lastName` varchar(50) NOT NULL,
`email` varchar(30) NOT NULL,
`website` varchar(30) NOT NULL,
`phone` varchar(20) NOT NULL,
`cell` varchar(20) NOT NULL,
`fax` varchar(20) NOT NULL,
`cityID` int(11) NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`contactID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;


DROP TABLE IF EXISTS `properties`;
CREATE TABLE IF NOT EXISTS `properties` (
`propertyID` int(11) NOT NULL AUTO_INCREMENT,
`ownerID` int(10) NOT NULL,
`propertyBedrooms` int(11) NOT NULL,
`propertyBathrooms` int(11) NOT NULL,
`propertySize` int(11) NOT NULL,
`propertyStatus` varchar(20) NOT NULL,
`propertyDescription` varchar(1000) NOT NULL,
`propertyPrice` int(11) NOT NULL,
`propertyType` varchar(30) NOT NULL,
`propertyLocation` varchar(50) NOT NULL,
PRIMARY KEY (`propertyID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


DROP TABLE IF EXISTS `property_contact`;
CREATE TABLE IF NOT EXISTS `property_contact` (
`propertyID` int(100) NOT NULL,
`contactID` int(100) NOT NULL,
`priority` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;



CREATE TABLE IF NOT EXISTS `city` (
`cityID` int(11) NOT NULL AUTO_INCREMENT,
`cityName` varchar(50) NOT NULL,
PRIMARY KEY (`cityID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


CREATE TABLE IF NOT EXISTS `types` (
`typeID` int(11) NOT NULL AUTO_INCREMENT,
`typeName` varchar(30) NOT NULL,
PRIMARY KEY (`typeID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


And this is my code

public function contacts()
{
if (!$this->ag_auth->logged_in())
{
redirect('login');
}

$crud = new grocery_CRUD();

//$crud->set_table('contacts');
$crud->set_relation_n_n('properties', 'property_contact', 'properties', 'contactID', 'propertyID', '{propertyType} {propertyLocation}', 'priority');

$crud->display_as('cityID','City');
$crud->set_subject('contact');

$crud->set_relation('cityID','city','cityName');


//$crud->set_relation('propertyType','types','typeName');



$output = $crud->render();

$this->_example_output($output);
}


It all works perfectly, no problems there.

The problem I'm having is that the {propertyType} and {propertyLocation} in my set_relation_n_n function are being displayed as ids while the actual 'type' and 'location' of a property are being fetched from 2 other tables; city and types which have a designated name that I would like to display instead.

[attachment=304:grid.jpg]

Is there a way to get the Type and Location columns to show the actual name instead of the ID?

I hope I made sense, thank you.

ufodisko
  • profile picture
  • Member

Posted 04 October 2012 - 23:08 PM

Bump :(

alvarito
  • profile picture
  • Member

Posted 12 October 2012 - 11:00 AM

why do you have twice written the word properties? And another question, where are the foreign keys when you created the tables ?

alvarito
  • profile picture
  • Member

Posted 12 October 2012 - 11:10 AM

You would have to create a foreign key in contacts and another one in properties and link them to the associative table. How is the query going to make the lookup if they are not related?

alvarito
  • profile picture
  • Member

Posted 12 October 2012 - 11:27 AM

Table properties should have a foreign key, type_id linked to the PK of 'types' and city_id in contacts should link to city_id in city, because I suppose you mean property type and property location being shown by type and city

you chose myisam engine, and it would be better Innodb, which allows for foreign keys and referential integrity.

also, you don't need to put 100 digits as the limit for your int, you mean you are going to have a number of properties that is up to 100 digits long ?