Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

How to populate a One to One relation

posted by php newbie 8 years ago

I have this Database

CREATE TABLE IF NOT EXISTS `countries` (
  `country_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(25) NOT NULL,
  PRIMARY KEY (`country_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `states` (
  `state_id` int(11) NOT NULL AUTO_INCREMENT,
  `country_id` int(11) NOT NULL,
  `name` varchar(25) NOT NULL,
  PRIMARY KEY (`state_id`),
  KEY `country_id` (`country_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(50) NOT NULL,
  `lastname` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(80) NOT NULL,
  `country_id` int(11) NOT NULL,
  `state_id` int(11) NOT NULL,
  `type` int(11) NOT NULL,
  `created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `active` int(11) NOT NULL,
  PRIMARY KEY (`user_id`),
  KEY `country_id` (`country_id`,`state_id`),
  KEY `state_id` (`state_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;


CREATE TABLE IF NOT EXISTS `details` (
  `user_id` int(11) NOT NULL,
  `description` text NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `details`ADD CONSTRAINT `details_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`);

ALTER TABLE `states` ADD CONSTRAINT `states_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `countries` (`country_id`);

ALTER TABLE `users` ADD CONSTRAINT `users_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `countries` (`country_id`);

ALTER TABLE `users` ADD CONSTRAINT `users_ibfk_2` FOREIGN KEY (`state_id`) REFERENCES `states` (`state_id`);

The Schema Class show this

Array
(
    [one-to-one] => Array
        (
            [0] => Array
                (
                    [table] => users
                    [column] => user_id
                    [related_table] => details
                    [related_column] => user_id
                    [on_delete] => no_action
                    [on_update] => no_action
                )
        
        )
  
    [many-to-one] => Array
        (
            [0] => Array
                (
                    [table] => users
                    [column] => state_id
                    [related_table] => states
                    [related_column] => state_id
                )
        
            [1] => Array
                (
                    [table] => users
                    [column] => country_id
                    [related_table] => countries
                    [related_column] => country_id
                )
        
        )
  
    [one-to-many] => Array
        (
        )
  
    [many-to-many] => Array
        (
        )
  
)

I have been using this code

$users = fRecordSet::build('User');
	
	foreach ($users as $user) {
		echo $user->getFirstname(), '<br />';
		echo $user->createCountry()->getName(), '<br />';
		echo $user->createState()->getName(), '<br />';
		echo $user->populateDetail()->getDescription();//Error Here
	}
		
	$countries = fRecordSet::build('Country');
	
	foreach ($countries as $country) {	
		echo $country->getName();
		echo '<br />';
		echo '<ul>';
		foreach ( $country->buildStates() as $state ) {
			echo '<li>', $state->getName(), '</li>';
		}
		echo '</ul>';
	}

I dont know how to get the description from details table.

It's a *-to-one relationship, so same as State and/or Country:

$users = fRecordSet::build('User');

foreach ($users as $user) {
    echo $user->createDetail()->getDescription();
}

Populate is an fActiveRecord method to "populate" a record with data, not to retrieve this data from a record.

posted by xoan 8 years ago