Forums / Developer / Add user script doesn't work

Add user script doesn't work

Author Message

Willie Seabrook

Thursday 02 October 2003 11:28:56 pm

Hi, I'm trying to import users from an old website database into ez publish and am running into hoardes of troubles. The code is below but no matter what I do I keep getting "Call to member function on a non object", "invalid argument supplied to foreach()" errors. This script is run from the console. When I do a var dump on $user =& eZUser::instance( 14 ); or $contentClass or contentobject etc for that matter I get null values back for every variable, so I suspect that it isn't connecting to the database properly or something. My db settings are fine because everything works perfect in a web environment - it just flops when I try to do something from the command line.

I get these sort of errors when I try run php -C workflow.php from the command line also. I suspect this is a simple set up/initialization problem but I have no idea what to do...

Help!!!

Regards,
Willie

include_once( "lib/ezutils/classes/ezmodule.php" );
include_once( "lib/ezdb/classes/ezdb.php" );
include_once( 'lib/ezutils/classes/ezini.php' );
include_once( 'lib/ezutils/classes/ezcli.php' );
include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
include_once( 'kernel/classes/datatypes/ezuser/ezusersetting.php' );
include_once( 'kernel/classes/ezcontentobject.php' );
include_once( 'kernel/classes/ezcontentobjectattribute.php' );
include_once( 'kernel/classes/ezcontentclass.php' );
include_once( 'kernel/classes/eznodeassignment.php' );
include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
include_once( "lib/ezutils/classes/ezextension.php" );
include_once( 'kernel/classes/ezscript.php' );

eZModule::setGlobalPathList( array( "kernel" ) );

///////////////////////////////////////////////////////////////////////////////
//Variables
///////////////////////////////////////////////////////////////////////////////

$hostname = "localhost";
$username = "ezuse";
$password = "blabla";
$database = "memberdb";
$tablename = "member";

/*
$script =& eZScript::instance();
$script->startup();

// Read arguments and modify script accordingly

$script->initialize();
*/

// Do the actual script here

$db =& eZDB::instance();
$db->setIsSQLOutputEnabled(true);

$user =& eZUser::instance( 14 );
var_dump($user);
//exit();

$cli = eZCLI::instance();
$cli->output("Collecting member details from mysql database");

$link = mysql_connect($hostname, $username, $password) or
DIE("Failed to connect to the legacy database");
//select database
mysql_select_db($database, $link) or
DIE("Failed to find legacy database");

$cli->output("Connected to the legacy database... retrieving data");

$query = "SELECT name_first, name_last, email, username, password FROM member";
$result = mysql_query($query);

if($result) {
$cli->output("Executed member query and result table built");
}
else $cli->output("Error: ".mysql_error());

$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
//importUser($row['username'], $row['password'], $row['name_first'], $row['name_last'], $row['email']);

///////////////////////////////////////////////////////////////////////////////
//Create the base Content Object
///////////////////////////////////////////////////////////////////////////////

//Fetch the ezUser class
$class =& eZContentClass::fetch(4); //3

//var_dump($class);

//exit();

//Instantiate an object of the ezUser class with root userid 14 and put it in section 9,
//the users section.
$contentObject =& $class->instantiate(14, 9);

var_dump($contentObject);

$contentObject->setAttribute( 'name', "User Test" );

$parentNodeID = 168; //This is the the 'Member' Directory in the Users Section

$nodeAssignment =& eZNodeAssignment::create( array(
'contentobject_id' => $contentObject->attribute( 'id' ),
'contentobject_version' => $contentObject->attribute( 'current_version' ),
'parent_node' => $parentNodeID,
'sort_field' => 2,
'sort_order' => 0,
'is_main' => 1
));

$nodeAssignment->store();

//Get the version and make it a draft.
$version =& $contentObject->version( 1 );
$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
$version->store();

$contentObjectID = $contentObject->attribute( 'id' );
$contentObjectAttributes =& $version->contentObjectAttributes();

//$parentNodeID = $contentObject->attribute( 'main_parent_node_id' );
//$currentVersion = $contentObject->attribute( 'current_version' );

$version =& $contentObject->attribute( 'current' );
$contentObjectAttributes =& $version->contentObjectAttributes();

//0 = First Name
$contentObjectAttributes[0]->setAttribute( 'data_text', $firstName );
$contentObjectAttributes[0]->store();

//1 = Last Name
$contentObjectAttributes[1]->setAttribute( 'data_text', $lastName );
$contentObjectAttributes[1]->store();

//2 = Email Address
$contentObjectAttributes[2]->setAttribute( 'data_text', $emailAddress );

///////////////////////////////////////////////////////////////////////////////
//ezUser Object
///////////////////////////////////////////////////////////////////////////////

$existUser =& eZUser::fetch( $contentObjectID );
$existUser->setAttribute('email', $emailAddress );
$existUser->setAttribute('password_hash', "" );
$existUser->setAttribute('password_hash_type', 0 );
$existUser->store();

var_dump($existUser);
exit();

$contentObject->store();

// Publish it to make it valid instantly
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectID,
'version' => 1 ) );


//Print the result to the console
$cli = eZCLI::instance();
$cli->output("Imported user with details:");
$cli->output("Username: ".$username);
$cli->output("Password: ".$password);
$cli->output("First Name: ".$firstName);
$cli->output("Last Name: ".$lastName);
$cli->output("Email Address: ".$emailAddress);
$cli->output("\n");

if($i > 20) {
exit();
}
$i++;
}

mysql_free_result($result);

Willie Seabrook

Thursday 02 October 2003 11:30:31 pm

By the way that code is not exact so don't bother picking out little errors in code as its just a kind of snapshot of what I've been doing.... no matter what I do the general errors I get remain, even in other scripts.

Marco Zinn

Saturday 04 October 2003 5:18:08 am

Hi,

some notes:
The import scripts from wenyue did not read the DB settings from override files like settings/override/site.ini.append.
It only checked for the DB settings in settings/site.ini.
I didn't read your code now, but can you check, if your (source) data is read currectly from your source-DB?

About the errors: "Call to member function on a non object", "invalid argument supplied to foreach()", you should read my comment and/or forum thread, IF you are importing to a non-english-DB:
http://ez.no/developer/ez_publish_3/contributions/import_script_for_data_import_from_22_to_3x#msg35392

Marco
http://www.hyperroad-design.com

eZ debug

Timing: Jan 18 2025 18:33:30
Script start
Timing: Jan 18 2025 18:33:30
Module start 'content'
Timing: Jan 18 2025 18:33:31
Module end 'content'
Timing: Jan 18 2025 18:33:31
Script end

Main resources:

Total runtime0.9516 sec
Peak memory usage4,096.0000 KB
Database Queries194

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0065 587.7109180.8359
Module start 'content' 0.00650.7837 768.5469583.0469
Module end 'content' 0.79020.1612 1,351.5938341.3750
Script end 0.9514  1,692.9688 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00420.4429210.0002
Check MTime0.00150.1539210.0001
Mysql Total
Database connection0.00080.081410.0008
Mysqli_queries0.843688.64341940.0043
Looping result0.00210.22471920.0000
Template Total0.919196.620.4595
Template load0.00190.195720.0009
Template processing0.917296.380720.4586
Template load and register function0.00020.022810.0002
states
state_id_array0.00140.144310.0014
state_identifier_array0.00130.138220.0007
Override
Cache load0.00190.20231710.0000
Sytem overhead
Fetch class attribute can translate value0.00160.166830.0005
Fetch class attribute name0.00200.207540.0005
XML
Image XML parsing0.00100.103830.0003
class_abstraction
Instantiating content class attribute0.00000.001240.0000
General
dbfile0.00740.7774270.0003
String conversion0.00000.000730.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
15content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
12content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 33
 Number of unique templates used: 6

Time used to render debug report: 0.0002 secs