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