Share » Learn » eZ Publish » Creating eZ Publish Objects in PHP

Creating eZ Publish Objects in PHP

Friday 04 June 2010 8:09:02 am

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Step 2 - Who will be importing?

Before you import any data you need to work out which eZ Publish user will be carrying it out? If you are using a template which is calling this functionality then you can pull out the current user. If you are running the script from the command line, you do not have a current user and so you may want to choose or create a specific one

For the rest of the steps of this turotial, replace the following code with that you want to use for your import (starting with this step).

Code :

/*****************************
Our Functionality will go here
*****************************/

Current User

Pulling in the current user is easy enough, using the static method currentUser() from within eZUser class you can pull out the user currently logged in. If nobody is logged in, the anonymous account within the CMS is used (typically if you run the function in a custom PHP Script you will be returned the anonymous user. I would recommend using the current user if the import has been triggered by an action from the user on the site.

Code :

$user = eZUser::currentUser();

A Specific User

You will typically want to do this if you are running a PHP script and I would recommend creating an import user for the task. You can pull out a specific user by name or email. Alternatively you can statically use an ID for the creation process but you may find these vary across your dev and live environments so I wouldn't recommend it.

Specific User by Name

Code :

$user = eZUser::fetchByName( 'Import' );
if ( !$user )
{
    //if no user exists let's pull out the current user:
    $user = eZUser::currentUser();
}

Specific User by Email

Code :

$user = eZUser::fetchByEmail( 'Import@admin.com' );
if ( !$user )
{
    //again, default to current user if necessary
    $user = eZUser::currentUser();
}
 

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)