1

HI i never faced this type of problem. Please clarify me when i did wrong. I tried to generate 2 model which have relation but it didn't come in models. Here are the Db structure.

CREATE TABLE IF NOT EXISTS `property` (
    `property_id` int(11) NOT NULL AUTO_INCREMENT,
    `ListPrice` int(11) NOT NULL,
    `ListingURL` text NOT NULL,
    `ProviderName` varchar(255) NOT NULL,
    `ProviderURL` text NOT NULL,
    `modificationTimestamp` text NOT NULL,
    PRIMARY KEY (`property_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `location` (
    `loc_id` int(11) NOT NULL AUTO_INCREMENT,
    `property_id` int(11) NOT NULL,
    `latitude` varchar(255) NOT NULL DEFAULT '0.0000',
    `longitude` varchar(255) NOT NULL DEFAULT '0.0000',
    PRIMARY KEY (`loc_id`),
    KEY `property_id` (`property_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

--
-- Constraints for table `location`
--
ALTER TABLE `location`
ADD CONSTRAINT `location_ibfk_1` FOREIGN KEY (`property_id`)
     REFERENCES `property` (`property_id`) ON DELETE CASCADE ON UPDATE CASCADE;

In model no relation in there :

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
    );
}
nazim
  • 1,439
  • 2
  • 16
  • 26
uiTeam324
  • 1,215
  • 15
  • 34
  • I just tried running your three SQL queries and checked the "Location" model with Gii. The relation is in there. I get `'property' => array(self::BELONGS_TO, 'Property', 'property_id'),`. – Jerome Oct 16 '14 at 15:23
  • strange. it's the first time when i face this. – uiTeam324 Oct 16 '14 at 15:37
  • 1
    Does your database use InnoDB by default? – Jerome Oct 16 '14 at 18:03
  • i never understood how to make relations on database, so i use a designer that makes them for me, you could try `MySQL Workbench` – Oscar Reyes Oct 17 '14 at 05:30
  • Yes @Jerome , by default it's InnoDB – uiTeam324 Oct 17 '14 at 09:14
  • @SRana - Can you confirm that you've selected the option to "Build Relations" in gii? Are you also using the latest version of the framework? – Jerome Oct 17 '14 at 13:00
  • @jerome- the yii version is 1.1.15 and "Build Relations" is selected by default. – uiTeam324 Oct 17 '14 at 13:06
  • @SRana - I'm sorry, it seems like it should work based on all the evidence you presented. The only thing you're doing different from how I build my queries is that I add the constraint as part of the `CREATE TABLE` as opposed to altering the table after the fact, but I really can't see that as the problem. If gii can't see the relations, it may be because the relation is not created properly in MySQL. If you used a GUI client to try manually adding rows, do you get errors creating a location record if you supplied a property id that doesn't exist in the property table? – Jerome Oct 17 '14 at 18:57

1 Answers1

2

This happens sometimes with the gii tool. What you need to do is to comment the relation in the table itself. gii misses the rleationship sometimes but mostly picks it up if available in the comment. So, modify your tables and comment the foreign key as below, it should work.

CREATE TABLE IF NOT EXISTS `location` (
    `loc_id` int(11) NOT NULL AUTO_INCREMENT,
    `property_id` int(11) NOT NULL COMMENT 'Foreign Key (property_id) references property(property_id )',
    `latitude` varchar(255) NOT NULL DEFAULT '0.0000',
    `longitude` varchar(255) NOT NULL DEFAULT '0.0000',
    PRIMARY KEY (`loc_id`),
    KEY `property_id` (`property_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
jdkraftz
  • 71
  • 1
  • 3