vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 3 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=187)
-   -   Creating Custom Datamanagers (https://vborg.vbsupport.ru/showthread.php?t=119376)

Alan @ CIT 06-21-2006 10:00 PM

Creating Custom Datamanagers
 
1 Attachment(s)
Note: Originally posted here: https://vborg.vbsupport.ru/showthrea...43#post1013143

-------------------------------------------------

Creating Custom Datamanagers


This article will show you the basics of creating your own fully working Datamanager.
To start with, create your new PHP file to hold your DM code and save it as
includes/class_dm_example.php

Now, the first thing we need to do is check that the vB_Datamanger class exists - without that, we can't do much of anything.

At the top of your new PHP file, put:

PHP Code:

if (!class_exists('vB_DataManager'))
{
exit;


This will end the script if the vB_Datamanger class doesn't exist.

Now we need to extend the vB_DataManager class to make our own Datamanager.

On the next line down in your PHP file, put:

PHP Code:

class vB_DataManager_Example extends vB_DataManager


This tells PHP that we want to create a Class called "vB_DataManager_Example", and that we want to base it on the vB_DataManager class. This means that our new DM class will inherit all of the vB_DataManagers variables and methods (functions) so we don't have to make our own save() method for example :)

Now we need to give our new Datamanager some fields to update. For example, the User DM would have fields such as "userid", "username", "email", etc. These fields will usually be the same as the columns in your database table.

These fields all go in an array called $validfields. The $validfields array is in the following format:

PHP Code:

'fieldname' => array(typerequired?, verify data?, function name to verify data

The verificatoin options (the last 2) are optional.

With that in mind, lets start adding to our own $validfields array. Our example datamanager will have 4 fields:

'exampleid' - this is an auto increment field from the database which is incremented automaticly.
'userid' - a vBulletin user id
'username' - a vBulletin username
'exampletext' - some random text

First things first, lets create our $validfields array variable.

PHP Code:

var $validfields = array( 

Now we can add our first field:

PHP Code:

'exampleid' => array(TYPE_UINTREQ_INCRVF_METHOD'verify_nonzero'), 

Lets go through this line bit by bit.

First we have the field name - in this case 'exampleid'.
Next we have the type - in this case we're saying it will be an Unsigned Int (ie, a number). A list of valid types can be found at the bottom of this post.
Next we specify that it is required, and that it is an auto-increment value with REQ_INCR. Valid options for this field can be found at the bottom of this post.
Next we tell it that we want to verify the data, with VF_METHOD
And finally, we give it the name of a function to verifiy the data with. In this case, we are using the verify_nonzero() function which is a standard function in the vB_DataManager class.

Now we can add our other 3 fields to the $validfields array:

PHP Code:

'userid' => array(TYPE_UINTREQ_YESVF_METHOD'verify_nonzero'),
'username' => array(TYPE_STRREQ_YES),
'exampletext' => array(TYPE_STRREQ_NO)
); 

This has added a further 3 fields to the $validfields, and closed the $validfields array :)

Now, the next step is to tell vBulletin what table to save our data in. Your table should match the $validfields array in terms of layout and column names.

In this case, we'll use a table called "example":

PHP Code:

var $table 'example'

Now we have to give vBulletin a temporary array that it uses to store the data we give it when it's saving to out database table that we specified above. It doesn't really matter what this is called, but for convention, try to stick with the name of your DM class :)

PHP Code:

var $example = array(); 

The final step in our very basic Datamanager is to create our Class Initator method. This is a function that is run automaticly when our Example Datamanager is created using datamanager_init().

PHP Code:

 function vB_DataManager_Example(&$registry$errtype ERRTYPE_STANDARD)
{
parent::vB_DataManager($registry$errtype);


The name of this functoin should be exactly the same as your class name. So in this case, we've called it "vB_DataManager_Example".

You don't need to worry about the rest of the code, and if you understand OOP, you'll know what it means :)

Now, all we've got left to do is add our closing bracket for our class, and we're done:

PHP Code:



You can now create your new Datamanager as you would any standard Datamanager by using the datamanager_init() function :)

That's the gist of it anyway, take a look at some of the existing Datamanagers for more advanced options such as Bitfields, custom verify methods, etc.

Valid field types:
  • TYPE_NOCLEAN - Any value - it won't be checked/cleaned
  • TYPE_INT - An signed integer
  • TYPE_UINT - An un-signed integer
  • TYPE_NUM - A number
  • TYPE_UNUM - An un-signed number
  • TYPE_UNIXTIME - A unix timestamp (time())
  • TYPE_STR - A string
  • TYPE_NOTRIM - A string that won't have trim() run on it
  • TYPE_NOHTML - A string that will have the HTML made-safe
  • TYPE_FILE - A file upload (ie, $_FILES)
  • TYPE_BOOL - A bool (true or false)
You can also pass Arrays of these items. For example to pass an array of INT's, use TYPE_ARRAY_INT. To pass an array of Strings, use TYPE_ARRAY_STR

Valid Requirement Options:

(The following is taken directly from vBulletin's DataManager documentation)

Quote:

REQ_YES - the field is required

REQ_NO - the field is not required

REQ_AUTO - the field can be automatically generated. The does not have any effect on code execution at this time. This is appropriate for things like post times that can be reasonably guessed before inserting the new data. (You will still need to write code to generate the appropriate value!)

REQ_INCR - this field is an AUTO_INCREMENT field in the database, and thus will be automatically generated upon insertion.
-------------------------------------------------

Good luck using your new found knowledge of the vBulletin Input Cleaner class, and remember: If you get stuck, just ask! Knowledge sharing is what vBulletin.org is all about!

(Note: If you want to reproduce this article anywhere, I have no objections, but I do request that you give me credit for writing it, and a PM letting me know would be appreciated :))

Paul M 06-22-2006 06:49 PM

Great Article Alan. :up:

-=Sniper=- 06-22-2006 06:56 PM

posting, so easier for me to find :) when needed, thanks again

AN-net 07-08-2006 01:42 PM

only one problem you should not be setting the array or table in the initialization of the variables. they should be set in the constructor.

Alan @ CIT 07-08-2006 01:49 PM

Quote:

Originally Posted by AN-net
only one problem you should not be setting the array or table in the initialization of the variables. they should be set in the constructor.

That's not true at all, either way is acceptable in OOP, it's all down to how you where taught :)

Thanks,
Alan.

Princeton 07-08-2006 04:16 PM

Just what our members need ... a great article!! :up:

Code Monkey 07-08-2006 10:28 PM

Interesting. Do you have to create your own presave, save, and postsave functions?

Alan @ CIT 07-09-2006 09:14 AM

Nope, not unless you wish to change the way they function.

Thanks,
Alan.

Dean C 07-09-2006 11:02 AM

May I suggest combining all the code into one example at the end of the article :)

Alan @ CIT 07-09-2006 12:51 PM

^^ you mean like the attached PHP file that's always been there? :D

Thanks,
Alan.

Code Monkey 07-09-2006 10:31 PM

Well, I tried it with a simple table and nothing got inserted. So there must be something more to this.

Alan @ CIT 07-10-2006 05:58 AM

Check the $errors array to see if there where any problems.

Alternativly, PM me your datamanager PHP file, and the PHP your using to test it, and I'll take a look.

The code above will (well, works here) work without changes though :)

Thanks,
Alan.

Guest190829 07-10-2006 07:14 AM

Alan,

Shouldn't

PHP Code:

'username' => array(TYPE_STRREQ_YESVF_METHOD), 

be:

PHP Code:

'username' => array(TYPE_STRREQ_YES), 

As verify_username does not exist in the base class. (It does in class_dm_user.php)

Alan @ CIT 07-10-2006 10:05 AM

That'll teach me to base my example of the User DM :D

Edited first post

Antivirus 07-17-2006 06:24 PM

So with the help of this tuitorial, I could create a data manager for creating User Notes about members, correct?

Alan @ CIT 07-17-2006 06:25 PM

Correct.

Thanks,
Alan.

Antivirus 07-17-2006 10:19 PM

sweet, going to give this a try shortly. thanks!

stryka 01-25-2007 08:39 PM

THis was a great tutorial... and I am able to use it for inserting records... But what about the UDPATE function?

Can you spell out how the UPDATE function works when using a Custom DataManager?

I am not sure how it works and how it gets the current record...

I'd like to also know when DM's should be used.... vs a simple INSERT statement within the file...

thanx

harmor19 03-17-2007 04:58 AM

Quote:

Originally Posted by stryka (Post 1167267)
THis was a great tutorial... and I am able to use it for inserting records... But what about the UDPATE function?


Can you spell out how the UPDATE function works when using a Custom DataManager?

I am not sure how it works and how it gets the current record...

I'd like to also know when DM's should be used.... vs a simple INSERT statement within the file...

thanx


I believe you would use $var->condition = "exampleid = 1";

PHP Code:

$example =& vB_DataManager_Example($vbulletinERRTYPE_ARRAY);  
$example->condition "exampleid = 1";
$example->set('userid'1);
$example->set('username''harmor19');
$example->set('exampletext''Testing');
$example->save(); 

If I'm right it should update the table where exampleid is equal to "1".

Antivirus 06-02-2007 02:31 PM

So i have written two custom datamanagers, one saves/updates the main info, while I use the other (within a loop) to update the related records.

I have to call the database from within each of the DMs to update some other tables with related info as well, and while looking at vbulletin's existing datamanagers, i see more than one method of calling the database, for example:

Within class_dm_threadpost some calls to thge database are made like this:
Code:

$this->dbobject->query_write
However within class_dm_infraction, calls are made like this:
Code:

$this->registry->db->query_write
What I want to know, is why is one method used in one instance, and another in another? I think both acheive exactly the same results but why are jelsoft using different methods to call the database?

Antivirus 06-09-2007 12:31 AM

bump

Guest190829 07-11-2007 06:09 PM

You are correct, as they are both referencing the same db object. I do not know vBulletin is not consistent in your particular example.

If you were to choose one, I would go with

$this->registry->db

As there must always be a valid registry object in the registry.

Antivirus 07-16-2007 05:20 PM

thanks for clarifying Danny :)

Opserty 08-03-2007 09:13 PM

Great article, will come in useful in the future I'm sure! Bookmarked.

kjhkjh 08-10-2007 05:22 PM

Can this help me?? Or anyone here?

I'm in the process of trying to add many forums without the ACP, here's where I'm up to:

I have added forums to the table 'forum'
I have updated the parent ID's, descriptions etc of each of the forums that I've added.
I believe I now have to updates the datastore which is basically a series of records in an serialized form that has information about the forums and its permissions. (this was the advice vb.com gave but I can find no documentation about how to do this - especially how to do it safely)

They also said to look at http://members.vbulletin.com/api. In particular the Datamanager class for Forums but it's difficult to know where to start.

Any help here is much appreciated - I'm trying to save myself over 100 hours of adding and updating forums - please help :)

Antivirus 08-12-2007 04:42 AM

are you trying to create forum swith a datamanager? One doesn't exist ( i dont think) so you'll have to create the DM.

kjhkjh 08-12-2007 04:51 AM

thx for your reply.

I have no idea how to create them or what they are!

But I was tld they could help me duplicate forums or just help me make the process of adding almost 2000 forums easier.

Aparently they are the missing loop to make the forums that I added into the mysql table 'forum' appear by serializing the forums data and adding them to the datastore - I'm just repeating this from earlier advice but still no good because I have no idea.... :(

Really wanted it to work out and had offered throughout the forum to pay someone to help, but no takers!

So I've started adding the forums through the ACP... Just looks like its about 100 hours work (85%+ of the forums are redirects to an active forum) but this whole thing is still taking too much time. I'm about 20 hours into it...

If someone comes up with a solution then I am open eared and open walleted :)

jasharen 08-18-2007 03:21 AM

Any chance someone could give an example of this and cleansing using TYPE_ARRAY_INT or TYPE_ARRAY_STR?

I can get it to work normally but I'm having some trouble with the arrays.

Thanks!

jasharen 08-19-2007 07:20 PM

Figured it out :)


PHP Code:

    $vbulletin->input->clean_array_gpc('p', array(
        
'SELECTFIELD'            => TYPE_ARRAY_INT
    
));

foreach(
$vbulletin->GPC['SELECTFIELD'] as $value)
{
  echo 
$value."\r\n";



DragonBlade 11-28-2008 12:09 AM

Hey, thanks, this was a real help. ^_^

One thing I'd like to ask, by trial and error I found out that I was (stupidly) forgetting to include includes/class_dm.php on my page.

Now, I've got a page, g13.php, and two files I include, includes/class_dm_g13.php and includes/functions_g13.php. Should I include the includes/class_dm.php in my includes/class_dm_g13.php page, or just the main g13.php page? I'm currently doing the latter, but it would work jsut fine doing the former, right?


All times are GMT. The time now is 04:52 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01321 seconds
  • Memory Usage 1,837KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (14)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (30)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete