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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 31 2025 05:58:22
Script start
Timing: Jan 31 2025 05:58:22
Module start 'layout'
Timing: Jan 31 2025 05:58:22
Module start 'content'
Timing: Jan 31 2025 05:58:24
Module end 'content'
Timing: Jan 31 2025 05:58:24
Script end

Main resources:

Total runtime1.2426 sec
Peak memory usage4,096.0000 KB
Database Queries57

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0072 588.1328151.2109
Module start 'layout' 0.00720.0029 739.343836.6484
Module start 'content' 0.01001.2315 775.9922973.7266
Module end 'content' 1.24160.0010 1,749.718816.4766
Script end 1.2426  1,766.1953 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00310.2472160.0002
Check MTime0.00130.1056160.0001
Mysql Total
Database connection0.00110.091410.0011
Mysqli_queries1.153492.8225570.0202
Looping result0.00050.0381550.0000
Template Total1.201096.620.6005
Template load0.00180.144520.0009
Template processing1.199196.501420.5996
Template load and register function0.00150.119010.0015
states
state_id_array0.00200.159910.0020
state_identifier_array0.00100.083920.0005
Override
Cache load0.00180.14621710.0000
Sytem overhead
Fetch class attribute can translate value0.00050.040220.0002
Fetch class attribute name0.00200.164140.0005
XML
Image XML parsing0.00320.261120.0016
class_abstraction
Instantiating content class attribute0.00000.000840.0000
General
dbfile0.00590.4771160.0004
String conversion0.00000.000740.0000
Note: percentages do not add up to 100% because some accumulators overlap

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
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 33
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs