Forums

Writing the 'current' user_id to jos_comprofiler

TOTKO 09 Mar, 2010
I hope you won't mind me posting this!

I posted a problem with the CB plugin a few days ago and have now managed half a solution. I used the Joomla registration plugin and linked to the jos_comprofiler table at the same time. Both tables are populated correctly but the user_id field isn't carried over to the CB table and therefore CB doesn't see the full record. Manually entering the value in the back-end successfully closes the loop however.

I am certain I saw a posting (but despite numerous searches, can't find it again) which showed how to copy a value from one table to another. Please can someone point me in the right direction or repost the code I need?

Thanks

Tony
GreyHead 09 Mar, 2010
Hi Tony,

There are several threads about that, mostly they involve creating second MySQL query by hand.

After registration the new user id is in the Joomla User Object so you might be able to do this by adding something like this before the database save.
<?php
$user =& JFactory::getUser();
JRequest:: setVar('user_id', $user->id);
?>

Bob
TOTKO 09 Mar, 2010
Bob,

Many thanks for your, as usual, prompt response.

Tony
TOTKO 13 Mar, 2010
I've fianlly got round to trying to close this loop and have to admit I'm beaten!

I'm not a php programmer so I don't know the exact code I need or where to run it from.

Please can you give me step-wise instructions?

Thanks

Tony
nml375 14 Mar, 2010
Hi Tony,
Using the JUser object returned from the JFactory::getUser() as suggested by Bob would only work if you've enabled the auto-login feature of the cfplugin. Otherwize, you'd have to fetch the newly created JUser object stored in the $MyPlugins object under the cf_joomla_registration['user'] property. I'd add the following code to the plugin's "Extra after Registration code":
<?
$user =& $MyPlugins->cf_joomla_registration['user'];
JRequest::setVar('user_id', $user->id);
?>

One other approach, which makes use of the fact that the plugin will store the newly created users' id in the POST-variable 'cf_user_id', is something like this:
<?
JRequest::setVar('user_id', JRequest::getVar('cf_user_id', 0));
?>

This could go into the normal "on submit - after email".

Using either code, the important thing is the settings on the RunOrder tab. Most important, the "Autogenerated Code" must be the last of the three blocks to be run (number 3). Further, if you set the 'user_id' POST-variable in the normal "on submit - after email" code, you'll need to make certain the plugins are run before the onsubmit code. Thus my recommendations is the following setup:
Auto Generated block: 3
OnSubmit block: 2
Plugins block: 1

/Fredrik
GreyHead 14 Mar, 2010
Hi Fredrik,

The User object is actually available after the Registration process without doing anything else. The user_id is set. The only difference is that it is usually blocked.

Bob
nml375 14 Mar, 2010
Hi Bob,
Unless you specify a specific user id to load, JFactory::getUser() will retrieve the JUser object of the currently logged in user. If the current session does not have a logged on user, a default "guest"-instance (id = 0) will be created instead, and store it in a static array of the JUser class. The joomla registration plugin clones this "guest"-object in order to create the new user record, and will thus leave the original object untouched.

As such, if you do not logon, any subsequent calls to JFactory::getUser() would return the "guest"-instance, not the clone used to create the new user record. Feel free to test this "on submit" code in a registration form:
<?
$user = JFactory::getUser();
print_r($user);
?>

The output will change depending on whether you've enabled the autologon feature or not.

You could load a new copy of the JUser object for the newly created user, by passing the user id or login name to the JFactory::getUser() method, or you could retrieve the original object created by the plugin using $MyPlugins->cf_joomla_registration['user'] array indice. Further, the user id is added to the POST variables as 'cf_user_id' within the plugin.

/Fredrik
GreyHead 15 Mar, 2010
Hi Fredrik,

My apologies, you are correct and I'm wrong.

But no I need to work out how I got this wrong because I'm sure that I've been able to use that data before - or maybe just my memory is faulty . . .

I have been re-working the plugins over the weekend to bring them all up to the same layout and add a few features, I'll take a look and make sure that we have a good way of accessing the user-id or the user object afrer registration.

Bob
TOTKO 17 Mar, 2010
Guys,

Thanks for all the help so far. I am sure I am (we are) nearly there but ..

The field I need to populate in jos_comprofiler is the 'id' field, not the 'user_id'. When I manually enter the value (directly into the database table) that now gets populated into the 'user_id', all the field data is visible in the CB User Manager screen.

As I said, I'm a total novice and naively changed the code to 'JRequest:: setVar('id', $user->id); but this resulted in no data being populated int the jos_comprofiler table. I guess a little knowldege is a dangerous thing!

I hope you'll be able to get me that extra step forward.

Thanks

Tony
nml375 17 Mar, 2010
Hi Tony,
It's been a long time since I used Community Builder, but as I recall, the id field of the table is merely a unique id (and primary key), and not linked to the user id. Changing 'user_id' to 'id' will not work, as it is the primary key, and tells the JTable class (does the actual db storage) that we're supposed to change an already existing record, rather than creating a new one. I'd have to look further into the CB code to advice what to do next.

/Fredrik
GreyHead 17 Mar, 2010
Hi Fredrik,

I had a look a the cb_registration plugin this week and it seemed to work fairly well*. The first two fields in jos_comprofiler (id & user_id) are both set to equal the user_id. I don't know if this is normal or a quirk of Max's coding but as long as the user_id is unique I guess that it will work.

Bob

* Tested a cleaned up version with a current download of CB.
TOTKO 17 Mar, 2010
You're correct. Both the user_id and id fields in the jos_comprofiler table take the same value. This is the case if you enter the user through the standard CB registration screen.
nml375 17 Mar, 2010
Hi Bob & Tony,
That seems to correspond well with what I've managed to find sofar. It would appear CB uses a relationship between the primary keys of both #__users and #__comprofiler, not the primary key of #__users to #__comprofiler.user_id. It would also seem #__comprofiler.id and #__comprofiler.user_id are expected to be equal, as there seems to be specific code to sync these.

This is a troublesome database-design, and renders the JTable classes useless for new records. I would say your options are limited to either use the cb_registration cfplugin, or add some custom php-code (on submit before/after email) to manually store the data in the database.

/Fredrik
TOTKO 18 Mar, 2010
Fredrik,

Thanks for the update.

I've tried the plug-in but this doesn't (in my environment) write to the jos_users table or populate the jos_comprofiler.id or jos_comprofiler..user_id fields.

Bob - were you 'experimenting' with a new version (not currently available) when you refer to the plug-in in a previous message?

The php code route is way above my level of knowledge at the moment. I'm just about to be made redundant - perhaps a good training course would be an appropriate career move :-)

Tony
GreyHead 18 Mar, 2010
Hi Tony,

Yes it's an updated version - though I don't think that I made any major changes.

Do you want to test a copy? Are you using any other plugins?

Bob
TOTKO 18 Mar, 2010
Hi Bob,

I'd be happy to test the new plug-in.

I don't use any others.

Thanks

Tony
TOTKO 19 Mar, 2010
Bob provided me with a test version of the new CB Registration plug-in yesterday and I spent 5 minutes with it last evening and have spent a couple of hours testing this morning.

The reason for the long period was due to me needing to work out the code for a couple of radio buttons I had on my form.

In the last few minutes I have completed my coding and I am pleased to say that the plug-in works perfectly. I added code to concatentate the firstname and last name fields and populate the new field into the jos_users table and this works well too.

Bob - many thanks for your work and responsiveness.

Tony
This topic is locked and no more replies can be posted.