Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 12-01-2012, 12:25 PM
inmateaid inmateaid is offline
 
Join Date: Nov 2012
Posts: 5
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default datamanager_init('User') save() "Duplicate entry" error in "usertextfield" table

I have spent the last 2 days trying to integrate vB 4.2 into CI 2.1.2...

Everything seems fine, but the datamanager outputs an SQL error page when saving a new user (outside of vB).

My index.php...

PHP Code:
<?php

// vBulletin
define('REGISTERED_USERGROUP'2); // typical default for registered users
define('PERMANENT_COOKIE'false); // false=session cookies (recommended)
define('THIS_SCRIPT'__FILE__); 
define('VB_AREA''External');
$dir getcwd();
chdir('forum/');
require_once(
'./includes/init.php'); // includes class_core.php
require_once('./includes/class_dm.php'); // for class_dm_user.php
require_once('./includes/class_dm_user.php'); // for user functions
require_once('./includes/functions.php'); // vbsetcookie etc.
require_once('./includes/functions_login.php'); // process login/logout 
chdir($dir);

My library...

PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Forum2 {
    
    private 
$dmuser;
    
    public function 
__construct()
    {
        global 
$vbulletin;
        
$this->dmuser =& datamanager_init('User'$vbulletinERRTYPE_ARRAY);
    }
    
    public function 
register($data = array(), $login FALSE)
    {
        global 
$vbulletin;
        foreach (
$data as $key => $value) {
            if (!
is_array($value)) {
                
$this->dmuser->set($key$value);
            }
        }
        if (isset(
$data['options']) && is_array($data['options'])) {
            foreach (
$data['options'] as $key => $value) {
                
$this->dmuser->set_bitfield('options'$key$value);
            }
        }
        
$this->dmuser->pre_save();
        
//print_r($this->dm);
        
if (count($this->dmuser->errors) > 0) {
            return 
implode('<li>'$this->dmuser->errors);
        }
        return 
$this->dm->save();
    }
}

My controller...

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Sandbox extends CI_Controller {

    public function 
__construct()
    {
        
parent::__construct();
    }
    
    public function 
vb_test()
    {
        
$this->load->library('forum2');
        
$this->forum2->register(array(
            
'username'    => 'testing5',
            
'email'        => 'test@test5.com',
            
'password'    => 'password5'
        
));
    }

}

Loading mydomain.com/sandbox/vb_test generates the vB sql error page and I receive the following email...

Code:
Database error in vBulletin 4.2.0:

Invalid SQL:
INSERT INTO usertextfield
	(userid)
VALUES
	(29);

MySQL Error   : Duplicate entry '29' for key 1

I am at a loss. Why is the datamanager is trying to insert the "usertextfield" record twice? The records for user, userfield, and usertextfield are all added to the database. I simply can't figure out why the sql error is coming through using the datamanager. I've rewritten this library a few times already to make sure it was an issue with me providing incorrect data to the manager, but nothing gets around this problem.

Please help

Notes: We have a custom cms in place that already has users. Any new registrations will be processed on the main site, and our script adds the forum user to the vB database using the datamanager. Registration and login are handled externally. Login/logout works fine so I did not include those methods in the code above.
Reply With Quote
  #2  
Old 12-01-2012, 01:09 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

When updating an existing user the vb code uses the datamanager function set_existing() and passes an array of the data, instead of calling set() for each field. This calls set_condition(), and if a condition is set, when save() is called it does an update instead of an insert. So you could try either using set_existing(), or else call set_condition() if you're trying to update an existing user.

If you're adding a new user, then you could try not setting the userid (and it will be assigned the next highest userid).
Reply With Quote
  #3  
Old 12-01-2012, 01:11 PM
inmateaid inmateaid is offline
 
Join Date: Nov 2012
Posts: 5
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Adding a new user...

only username, email and password are sent to the datamanager (as shown in the "controller" code above)
Reply With Quote
  #4  
Old 12-01-2012, 01:23 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Oops, right, I see now. I also missed that the database error is for the usertextfield table. Did you by any chance delete users by truncating the user table? If so, you also should truncate the usertextfield and userfield tables.
Reply With Quote
  #5  
Old 12-01-2012, 01:30 PM
inmateaid inmateaid is offline
 
Join Date: Nov 2012
Posts: 5
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

This error occurred upon first use. The only existing record was the main admin and one test user account. The thing is, the record is added in lieu of the error so it seems like it's calling twice unless the call was actually successful and the error was after-the-fact.

I can truncate the tables to reset them if needed, but the initial user insert is where the userid comes from and is what's used as the primary key in the subsequent tables. So I don't see how that could be corrupted
Reply With Quote
  #6  
Old 12-01-2012, 01:37 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

OK, I tried this code and it worked for me (it inserted a user and there was no db error):

Code:
<?php

// vBulletin
define('REGISTERED_USERGROUP', 2); // typical default for registered users
define('PERMANENT_COOKIE', false); // false=session cookies (recommended)
define('THIS_SCRIPT', __FILE__); 
define('VB_AREA', 'External');
$dir = getcwd();
chdir('forum4.2.0/');
require_once('./includes/init.php'); // includes class_core.php
require_once('./includes/class_dm.php'); // for class_dm_user.php
require_once('./includes/class_dm_user.php'); // for user functions
require_once('./includes/functions.php'); // vbsetcookie etc.
require_once('./includes/functions_login.php'); // process login/logout 
chdir($dir); 

class Forum2 {
    
    private $dmuser;
    
    public function __construct()
    {
        global $vbulletin;
        $this->dmuser =& datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);
    }
    
    public function register($data = array(), $login = FALSE)
    {
        global $vbulletin;
        foreach ($data as $key => $value) {
            if (!is_array($value)) {
                $this->dmuser->set($key, $value);
            }
        }
        if (isset($data['options']) && is_array($data['options'])) {
            foreach ($data['options'] as $key => $value) {
                $this->dmuser->set_bitfield('options', $key, $value);
            }
        }
        $this->dmuser->pre_save();
        //print_r($this->dm);
        if (count($this->dmuser->errors) > 0) {
            return implode('<li>', $this->dmuser->errors);
        }
        return $this->dmuser->save();
    }
}

$f2 = new Forum2();

        $f2->register(array(
            'username'    => 'testing5',
            'email'        => 'test@test5.com',
            'password'    => 'password5'
        ));

other than removing your controller class and changing the directory, the only thing I changed was the part in red, which said "dm" instead of "dmuser".

ETA: I was thinking you might throw this is a php file and try it, to see if the problem is somewhere else.
Reply With Quote
  #7  
Old 12-01-2012, 01:53 PM
inmateaid inmateaid is offline
 
Join Date: Nov 2012
Posts: 5
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

i renamed some things for the example for clarity, must have missed the one "dm" reference I'll give this a shot in a standalone file as suggested. Guessing if it doesn't work, I'll rebuild the tables. Will post the results soon.

--------------- Added [DATE]1354373919[/DATE] at [TIME]1354373919[/TIME] ---------------

It threw the same error. Going to truncate the tables and reinsert the initial admin record after backing up what's already there. Will see if that helps

--------------- Added [DATE]1354374216[/DATE] at [TIME]1354374216[/TIME] ---------------

Ok, truncated the tables and manually reinserted the inital user, userfield, and usertextfield records and it works without error. I must have deleted a row in phpMyAdmin and thrown off the index at some point.
Reply With Quote
  #8  
Old 12-01-2012, 02:45 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

So does that solve the problem, or is there still an issue with your original code?
Reply With Quote
  #9  
Old 12-01-2012, 03:10 PM
inmateaid inmateaid is offline
 
Join Date: Nov 2012
Posts: 5
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I have to come back to it later today, but the code seems to be fine. I'm going to update my original library now that it's working standalone and will post the status here after that's done. Thanks for mentioning "delete users" as that made me remember doing so when I initially started working on this bridge
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.05135 seconds
  • Memory Usage 2,270KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_code
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete