Yii模型没有使用gii自动建立关系


Yii model not bulid relation automatically using gii

HI我从未遇到过这种类型的问题。请澄清我什么时候做错了。我试图生成两个有关系的模型,但它没有出现在模型中。这是Db结构。

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;

在模型中没有关系:

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(
    );
}

gii工具有时会发生这种情况。您需要做的是在表本身中注释关系。gii有时会错过这个话题,但如果可以在评论中找到的话,大部分都会接受。因此,修改您的表并注释外键如下,它应该可以工作。

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;