How to programmatically insert a new field in comprofiler and comprofiler_fields

6 years 9 months ago #295194 by llau
Hello,

I'm trying to use the Joomla API to add a new column to the comprofiler table, and I believe I also need to add a new row for this column in the comprofiler_fields table, please correct me if I'm wrong. I'm just trying to add a new field to be displayed on the user's profile and do all this programmatically. I think doing this through the CB backend, creates a new column in the comprofiler table as well as a new row in the "fields" table.

I'm wondering what is the "recommended" way to do this using the Joomla API, or is it easier to do through the CB API?

Cheers,
V

Please Log in to join the conversation.

6 years 9 months ago #295215 by krileon
Simply put, you don't. Use CB API to do so. Once CB API has been included you can make an instance of FieldTable class (note this is a namespaced class) where you'll be able to do a ->store to create a field. See the below on how to include CB API.

www.joomlapolis.com/documentation/279-community-builder/tutorials/18357-including-cb-api-for-usage-outside-of-cb

Below is where the FieldTable class is located.

libraries/CBLib/CB/Database/Table/FieldTable.php


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You: llau

Please Log in to join the conversation.

6 years 9 months ago - 6 years 9 months ago #295222 by llau
Ok, thanks for the link, Krilieon.

Would I create an instance of the FieldTable class and then use the store function to create the new field, and will it also add the associated columns in the comprofiler table? I'm planning on adding the new fields onUserLogin....

Thanks again,
V

Please Log in to join the conversation.

6 years 9 months ago #295223 by llau
Is there code I should be usinig to close database connection as well or does CB already do that after the store?

Thank you kindly,
V

Please Log in to join the conversation.

6 years 9 months ago #295236 by krileon

Would I create an instance of the FieldTable class and then use the store function to create the new field, and will it also add the associated columns in the comprofiler table?

Yes.

Is there code I should be usinig to close database connection as well or does CB already do that after the store?

With Joomla/CB APIs you don't manually open or close the connection. That's all handled further up the chain in the database APIs. Basically doing a ->store opens and closes the connection automatically.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

6 years 9 months ago - 6 years 9 months ago #295404 by llau
Thanks for you quick response Krileon.

Sorry I have one more question, I had included the code below
if ( ( ! file_exists( JPATH_SITE . '/libraries/CBLib/CBLib/Core/CBLib.php' ) ) || ( ! 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' );

and it was working when I called it through a CB auto action.

When I called my function through another page, I now get this error when it tries to add new columns:
PHP Fatal error:  Call to a member function loadPluginGroup() on a non-object in C:\WebRoot\website\joomla\libraries\CBLib\CB\Database\Table\FieldTable.php on line 234

I'm creating the new object using this code here:
	$field_Table = new \CB\Database\Table\FieldTable();
	$field_Table->set( 'name', 'cb_newfield');
	$field_Table->set( 'title', 'New field');
	$field_Table->set( 'profile', '0');
	$field_Table->set( 'readonly', '1');
	$field_Table->set( 'published', '1');
	$field_Table->set( 'type', 'date');
	$field_Table->set( 'tabid', '11');
	$field_Table->set( 'table', '#__comprofiler'); 
It seems like it is happening inside the field table's store function on this line here: $_PLUGINS->loadPluginGroup( 'user' );

Any insights would be greatly appreciated.

Thanks again.
V

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.216 seconds

Facebook Twitter LinkedIn