Forums / Developer / Inserting article through code problem

Inserting article through code problem

Author Message

Lior Solomon

Wednesday 30 June 2004 10:55:06 am

Hi,
I wrote few lines of code
that adds an article to the site

$currentUser=& eZUser::currentUser();
$user_id = $currentUser->attribute('contentobject_id');
$parentNodeID = 624; // testing folder
$parentContentObjectTreeNode = eZContentObjectTreeNode::fetch($parentNodeID);
$parentContentObject = $parentContentObjectTreeNode->attribute('object');
$sectionID=$parentContentObject->attribute('section_id');
$class =& eZContentClass::fetch( 2 );//artcile 
$contentObject =& $class->instantiate( $userID, $sectionID );
$nodeAssignment =& eZNodeAssignment::create( array(
        'contentobject_id' => $contentObject->attribute( 'id' ),
        'contentobject_version' => $contentObject->attribute( 'current_version' ),'parent_node' => 624,'is_main' => 1)
);
$version =& $contentObject->version( 1 );
$contentObjectAttributes =& $version->contentObjectAttributes();

$loopLength = count($contentObjectAttributes);
for($i=0;$i<$loopLength;$i++)
{
   srand(time()); //just a random number to add to fields
   $random = (rand()%9);

   $text = "sometext " . $random;

   $datatype = $contentObjectAttributes[$i]->datatype();
   if($datatype->attribute('data_type_string' ) == 'eztext' || $datatype->attribute('data_type_string' ) == 'ezstring'){ 
      $contentObjectAttributes[$i]->setContent($text);
      $contentObjectAttributes[$i]->store();
   }
}

$nodeAssignment->store();
$contentObject->store();
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObject->attribute( 'id' ), 'version' => 1 ) );

the article apears on the admin side
but while trying to edit the item or preview the article non of the fields are shown
what am i doing wrong
please!!!i've been waisting 2 days on this issue
:-)

thanks

Paul Forsyth

Wednesday 30 June 2004 11:23:58 am

You might need to store the version, eg:

      // Set a status for the content object version
      $version =& $contentObject->version($contentObject->attribute( 'current_version' ) );
      $version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT);
      $version->store();

Plus at the end try this:

$operationResult = eZOperationHandler::execute( 'content', 
                                                'publish', 
                                                array( 'object_id' => $contentObject->attribute( 'id' ), 
                                                       'version' => $contentObject->attribute('current_version' ) ) );

for good measure.

paul

--
http://www.visionwt.com

Lior Solomon

Wednesday 30 June 2004 7:21:40 pm

thanks
i tried your suggestion
but still no change
on the admin panel i see a new article created called "New Article"
i click on the item to see the content and get an empty object.
if i press the update button i get the "Edit Article - New Article" page
but there are no fields to edit!!! :-(
this module is executed as an extention and the user that executes it has administrative permissions(just if you wondered about this option)

any ideas?

Paul Forsyth

Thursday 01 July 2004 12:47:50 am

Im not sure.

Here is the code I have for creating an folder Maybe you can adapt it instead of yours to see if it works?

    function createFolderObject($user, $sectionID, $userNodeID=2, $folderTitle="No title")
    {
      $folderClassID=1;
      
      $class =& eZContentClass::fetch( $folderClassID );
      $folderContentObject =& $class->instantiate( $user->id(), $sectionID );
      
      // Create a node for the object in the tree.
      $nodeAssignment =& eZNodeAssignment::create( array(
                             'contentobject_id' => $folderContentObject->attribute( 'id' ),
                             'contentobject_version' => 1,
                             'parent_node' => $userNodeID,
                             'sort_field' => 2, //Published
                             'sort_order' => 1, //Descending
                             'is_main' => 1));
      $nodeAssignment->store();
        
      // Set a status for the content object version
      $folderContentObjectVersion =& $folderContentObject->version($folderContentObject->attribute( 'current_version' ) );
      $folderContentObjectVersion->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT);
      $folderContentObjectVersion->store();

      // Set the title of the folder.
      $folderContentObjectAttributes =& $folderContentObjectVersion->contentObjectAttributes();
      foreach ($folderContentObjectAttributes as $folderAttribute)
      {
        // Each attribute has an attribute called 'identifier' that identifies it.    

        if ($folderAttribute->attribute("contentclass_attribute_identifier") == "title")
        {
          $folderAttribute->setAttribute("data_text",$folderTitle);
          $folderAttribute->store();
        }
      }
          
      // Now publish the object.
      $operationResult = eZOperationHandler::execute( 'content', 'publish',
                              array( 'object_id' => $folderContentObject->attribute( 'id' ),
                                     'version' => $folderContentObject->attribute('current_version' ) ) );
      
      return;
    }

good luck!

paul

--
http://www.visionwt.com

Lior Solomon

Thursday 01 July 2004 2:23:47 am

well i added the function
and i see the same simptoms
the folder is created
but when i try to edit it
there are no fields to edit - the description field is missing on this case
but if i create a folder manualy on the admin pannel i can edit the field

Paul Forsyth

Thursday 01 July 2004 3:47:53 am

Are you getting any errors?

Clear cache and try to edit again.

paul

--
http://www.visionwt.com

Lior Solomon

Thursday 01 July 2004 4:50:59 am

well not helped at all
just to make the problem clear
the folowing image is a snapshot of a created manualy folder object - edit screen:
http://www.kabbalahmedia.info/~testworkarea/folder-by-manualy.jpg

and this one for created by module(code generated) - edit screen:
http://www.kabbalahmedia.info/~testworkarea/folder-by-code.jpg

somehow it doesn't alows me to view or edit fields on the created object

Paul Forsyth

Thursday 01 July 2004 5:02:06 am

Are you getting any errors?

paul

Lior Solomon

Thursday 01 July 2004 5:12:05 am

i just uploaded images that includes the debug info
go to the bottom of the image(it is scrolable now)

thanks for the support

Paul Forsyth

Thursday 01 July 2004 5:16:26 am

Ok, first try switching of the online editor to test whether that has anything to do with it.

Make sure your clear the cache before looking at the errors. Cached pages wont have errors.

Are you able to look at the database with a tool like phpmyadmin?

paul

Lior Solomon

Thursday 01 July 2004 5:36:26 am

i'm using postgre db
but i can try and scroll the db tables
do you know the ez db scheme?
or where can i find documantation for that?

Paul Forsyth

Thursday 01 July 2004 5:45:43 am

One thing you could try is to edit the file:

design/standard/templates/content/edit.tpl

and add in near the top:

{$object|attribute(show)}

Try this for a normal folder and your created folder. Are there differences?

Have you switched off the online editor? Switch it off in the site.ini.

paul

Lior Solomon

Thursday 01 July 2004 6:24:45 am

THE PLOT IS PLOTING

folder - code
>contentobject_attributes array Array(0)
>data_map array Array(0)

folder - manual
>contentobject_attributes array Array(2)
>data_map array Array(2)

that explains it
so probably there is something preventing from the code to be executed proparly but no propare exceptions are thrown
actualy the attributes are not being stored
:-(

Paul Forsyth

Thursday 01 July 2004 6:51:05 am

Hmm, look at your logs, var/log/error.log, var/log/warning.log, var/log/debug.log.

Try a 'tail -f' on these files and watch them when you run your script. Something is going wrong here...

paul

Lior Solomon

Thursday 01 July 2004 7:20:40 am

on the error messages i get the following:

Error: eZTemplate Jul 01 2004 07:09:51 
No such attribute for array(3): node_id
Choose one of following: content, path, uri
 
Error: eZTemplate Jul 01 2004 07:09:51 
No such attribute for array(3): node_id
Choose one of following: content, path, uri
 
Error: eZTemplate Jul 01 2004 07:09:51 
No such attribute for array(3): node_id
Choose one of following: content, path, uri
 
Error: eZTemplate Jul 01 2004 07:09:51 
No such attribute for array(3): node_id
Choose one of following: content, path, uri
 
Error: eZTemplate Jul 01 2004 07:09:51 
No such attribute for array(3): node_id
Choose one of following: content, path, uri
 
Error: eZTemplate Jul 01 2004 07:09:51 
No such attribute for array(3): node_id
Choose one of following: content, path, uri


but this are template error that shouldn't be related to the problem.

warnings:

Warning: eZCodePage Jul 01 2004 07:09:51 
Couldn't load codepage file share/codepages/utf-8
 
Warning: ezi18n Jul 01 2004 07:09:51 
No translation for file(translation.ts) in context(kernel/classes/datatypes): 'XML Text field' with comment(Datatype name)
 
Warning: ezi18n Jul 01 2004 07:09:51 
No translation for file(translation.ts) in context(kernel/classes/datatypes): 'XML Text field' with comment(Datatype name)
 
Warning: ezi18n Jul 01 2004 07:09:51 
No translation for file(translation.ts) in context(kernel/classes/datatypes): 'XML Text field' with comment(Datatype name)
 
Debug: changing value of attribute_original_id to default Jul 01 2004 07:09:51 

 
Warning: ezi18n Jul 01 2004 07:09:51 
No translation for file(translation.ts) in context(kernel/classes/datatypes): 'XML Text field' with comment(Datatype name)
 
Warning: ezi18n Jul 01 2004 07:09:51 
No translation for file(translation.ts) in context(kernel/classes/datatypes): 'XML Text field' with comment(Datatype name)
 
....(alot of those)

Warning: eZModuleOperationInfo::execute Jul 01 2004 07:09:51 
Missing main operation memento for key: 6f0d86830744444ae0968edcc19e1706
 
Warning: PHP Jul 01 2004 07:09:51 
Undefined index:  user_id in /var/www/html/eznew/kernel/classes/ezworkflowprocess.php on line 156

Warning: PHP Jul 01 2004 07:09:51 
fread(): Length parameter must be greater than 0. in /var/www/html/eznew/lib/eztemplate/classes/eztemplatecompiler.php on line 1632
 

this workflow warning looks suspisious
what do you think?
man i'm so happy to have you helping
realy this is outstanding help

Lior

Paul Forsyth

Thursday 01 July 2004 7:28:15 am

Are the logs correct? I see times of 07.09. Is that your local time?

Im a little stumped here. Attributes should be created when the object is created, so why yours are not create im not sure.

eZ crew, can you help here?

paul

Kåre Køhler Høvik

Monday 05 July 2004 2:12:52 am

Hi

Whats the default language ?
Which PHP version are you using ?

--
Kåre Høvik

Kåre Høvik

Lior Solomon

Monday 05 July 2004 3:26:15 am

Thanks mate
the php version is: PHP 4.3.6
the local of the user side is : rus_RU
and for admin : eng_GB

any ideas?
dont tell me its related to the default language!!!
:)

Kåre Køhler Høvik

Monday 05 July 2004 3:41:13 am

I thnk it might be language related. From where do you run this import script ?
What siteaccess do you use on the import ? Try to make sure to set it to the one you use for admin.

--
Kåre Høvik

Kåre Høvik

Lior Solomon

Monday 05 July 2004 3:44:45 am

hmm
i'm not using any siteaccess
just running the create.php example you gave with your new book
there is no mention there for siteaccess

how should i do it ?

eZ debug

Timing: Jan 30 2025 21:37:49
Script start
Timing: Jan 30 2025 21:37:49
Module start 'content'
Timing: Jan 30 2025 21:37:49
Module end 'content'
Timing: Jan 30 2025 21:37:49
Script end

Main resources:

Total runtime0.2753 sec
Peak memory usage8,192.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0085 588.0313370.2891
Module start 'content' 0.00850.0127 958.32031,033.6563
Module end 'content' 0.02120.2540 1,991.97663,923.9922
Script end 0.2752  5,915.9688 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00431.5524200.0002
Check MTime0.00120.4388200.0001
Mysql Total
Database connection0.00210.752610.0021
Mysqli_queries0.179865.31361410.0013
Looping result0.00150.55671390.0000
Template Total0.253592.110.2535
Template load0.00070.240910.0007
Template processing0.252991.858510.2529
Override
Cache load0.00050.166810.0005
Sytem overhead
Fetch class attribute can translate value0.00200.744010.0020
XML
Image XML parsing0.00030.090910.0003
General
dbfile0.00541.9635200.0003
String conversion0.00000.002130.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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs