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

Trying to use one to one relationships

posted by anonymous 9 years ago

Hi there, im trying to do a very simple one to one relationship from my users table to my user details table, no matter how hard i try i cannot get it to work. Could any body please shed some light on this?

CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `instance_id` int(11) unsigned NOT NULL,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`user_id`),
  KEY `instance_id` (`instance_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=22 ;

CREATE TABLE IF NOT EXISTS `user_details` (
  `user_id` int(11) unsigned NOT NULL,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `user_details`
  ADD CONSTRAINT `user_details_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;

And ive tried

$user = new User();
$user->setEmail(fRequest::get('user::email'));
$user->setPassword(fRequest::get('user::password'));
$user->setInstanceId($instance->getInstanceId());
$user->store();

$user_details = $user->createUserDetail();
$user_details->setFirstName(fRequest::get('user_detail::first_name'));
$user_details->setLastName(fRequest::get('user_detail::last_name'));
$user_details->store();

and

$user = new User();
$user->setEmail(fRequest::get('user::email'));
$user->setPassword(fRequest::get('user::password'));
$user->setInstanceId($instance->getInstanceId());
			
$user_details = new UserDetail();
$user_details->setFirstName(fRequest::get('user_detail::first_name'));
$user_details->setLastName(fRequest::get('user_detail::last_name'));
$user_details->associateUser($user);
			
$user->store();

Someone please help, its doing my head in.

What error is occurring, or is nothing happening?

Can you try adding this code above your creation of the User object?

fORMDatabase::retrieve()->enableDebugging(TRUE);

This will print out all of the SQL statements that are being executed.

posted by wbond 9 years ago

Yo,

Heres what i called,

$user = new User();
	$user->setEmail('test@example.com');
	$user->setPassword('secret');
	$user->setInstanceId(1);
	$user->store();
 
	$user_details = $user->createUserDetail();
	$user_details->setFirstName('John');
	$user_details->setLastName('Doe');
	$user_details->store();
	

	$user = new User();
	$user->setEmail('test2@example.com');
	$user->setPassword('secret');
	$user->setInstanceId(1);
	
	$user_details = new UserDetail();
	$user_details->setFirstName('John2');
	$user_details->setLastName('Doe2');
	$user_details->associateUser($user);
            
	$user->store();

And here is the output

Query time was 0.00035619735717773 seconds for:
SHOW CREATE TABLE users
Query time was 0.00030994415283203 seconds for:
BEGIN
Query time was 0.00048422813415527 seconds for:
INSERT INTO "users" ("instance_id", "email", "password", "twitter", "status", "mobile", "token", "created_at", "updated_at", "active_at") VALUES (1, 'test@example.com', 'secret', NULL, NULL, NULL, NULL, NULL, NULL, NULL)
Query time was 0.00095105171203613 seconds for:
COMMIT
Query time was 0.00048685073852539 seconds for:
SHOW CREATE TABLE user_details
Query time was 0.038719892501831 seconds for:
SELECT * FROM "user_details" WHERE "user_details"."user_id" = 24
Query time was 0.00031280517578125 seconds for:
BEGIN
Query time was 0.00034213066101074 seconds for:
ROLLBACK
Uncaught fValidationException
-----------------------------
{doc_root}/bootstrap.php(18): fActiveRecord->store()
{doc_root}/libraries/flourish/fActiveRecord.php(2670): fActiveRecord->validate()
{doc_root}/libraries/flourish/fActiveRecord.php(2878)
<p>The following problems were found:</p>
<ul>
<li>User ID: Please enter a value</li>
</ul>
posted by anonymous 9 years ago

In the first example you aren't manually setting the user_id on the user_details table, which explains the error. In the second example you almost have it, except you are associating the user with the user detail, but then you save the user.

The associate action work by associating the passed record to the record you are calling the method on, then you have to save the record you called the method on. I can see how with a one-to-one relationship this might be slightly confusing. If you want to open a ticket, I can probably treat associate a little differently for one-to-one relationships.

Here is the code that should work:

$user = new User();
$user->setEmail('test2@example.com');
$user->setPassword('secret');
$user->setInstanceId(1);

$user_details = new UserDetail();
$user_details->setFirstName('John2');
$user_details->setLastName('Doe2');

$user->associateUserDetail($user_details);        
$user->store();
posted by wbond 9 years ago