Forums

Using all memory with this code

fribse 25 Apr, 2016
In v4 I have this code to prepopulate my form with userdata:
<?php
global $_CB_framework, $mainframe;
 
if ( defined( 'JPATH_ADMINISTRATOR' ) ) {
	if ( ! file_exists( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' ) ) {
		echo 'CB not installed!';
		return;
	}
 
	include_once( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' );
} else {
	if ( ! file_exists( $mainframe->getCfg( 'absolute_path' ) . '/administrator/components/com_comprofiler/plugin.foundation.php' ) ) {
		echo 'CB not installed!';
		return;
	}
 
	include_once( $mainframe->getCfg( 'absolute_path' ) . '/administrator/components/com_comprofiler/plugin.foundation.php' );
}
 
$user =& CBuser::getUserDataInstance( $_CB_framework->myId() );

$form->data['navn1'] = $user->name;
$form->data['email1'] = $user->email;
$form->data['mobil1'] = $user->cb_cell;
$form->data['adresse1'] =& $user->cb_adresse;
$form->data['post1'] = $user->cb_postnummer;
?>

But when I try to use it in v5 it exhausts the memory.
Am I doing something wrong?
GreyHead 26 Apr, 2016
Hi Fribse,

Yes something is wrong - though I'm not clear what is.

I assume that you have CFv5.0.11 installed and not 5.0.1 ?

Some of the & you have are unnecessary with more recent versions of PHP and declaring $mainframe as global might be a problem.

I suggest that you set your site Error Reporting to Maximum; then comment out all of your code and check, then remove the comments bit by bit (keeping the logic correct) until you can pin down where the error is caused. Most often it is some kind of loop but I don't see any obvious one here.

Bob
fribse 26 Apr, 2016
Yes it's 5.0.11, sorry for the typo.
I'll go and ask Community Builder, because I don't have a clue on what is the 'right' way of getting a CB user object.
Thankyou!
fribse 27 Apr, 2016
It seems that the code needed has changed with the later CB's.

I got a result with another tutorial, but Kyle gave me a third suggestion, so now I need to find out which is best.
fribse 29 Apr, 2016
Answer
This is the new way of getting a CB user object:
<?php

if ( ( ! file_exists( JPATH_SITE . '/libraries/CBLib/CBLib/Core/CBLib.php' ) ) || ( ! file_exists( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' ) ) ) {
	return;
}
 
include_once( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' );
 
cbimport( 'cb.html' );
 
$user		=	CBuser::getMyUserDataInstance();


The proper way of prepopulating a field is this way (name is called navn1 in the form)

$form->data['navn1'] = $user->get( 'name' ) ;


This seems to work as it should
This topic is locked and no more replies can be posted.