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

时间:2014-10-16 15:12:27

标签: php mysql yii gii

我从未遇到过这类问题。当我做错时请澄清我。我试图生成2个有关系的模型,但它没有进入模型。这是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(
    );
}

1 个答案:

答案 0 :(得分:2)

有时使用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;
相关问题