Forums / Developer / Ajax attribute edition - help on transaction error ???

Ajax attribute edition - help on transaction error ???

Author Message

H-Works Agency

Thursday 26 February 2009 8:13:45 am

Hello,

I am using ajax to allow seemless content object attribute update.

My question is : "Is my technique the right one ?" Sometime it corrupt my database table and i don't know why. I have transaction errors that led one time to a total database restoration.

The problem i face is that sometimes it works and sometime it leed to a transaction error...which is very disturbing.

Here is the code in the page targeted by my ajax function :

<?php

$http = eZHTTPTool::instance();

// Execute code only if everybody is here
if ( $http->hasVariable( 'language' ) &&
     $http->hasVariable( 'contentObjectID' ) &&
     $http->hasVariable( 'attributeIdentifier' ) &&
     $http->hasVariable( 'value' )
   )
{
	$response = '';
	//$response = 'Work in progress';

	$value_new = htmlspecialchars(urldecode($http->variable( 'value' )));
	$contentObjectID = $http->variable( 'contentObjectID' );
	$attribute_identifier = $http->variable( 'attributeIdentifier' );
	$languageCode = $http->variable( 'language' );

	// Verify that language code exists in our siteaccess
	$language = eZContentLanguage::fetchByLocale( $languageCode );

	// Fetch object to update
	$contentObject = eZContentObject::fetch($contentObjectID);

	// Check if object exists && if new value is non empty && if language exists
	if($contentObject && $language)
	//if($contentObject && $language && $value_new != '')
	{
		// Retrieve attribute - returns an array
		$contentObjectAttributes = $contentObject->fetchAttributesByIdentifier(array($attribute_identifier), $contentObject->attribute('current_version'), $languageCode);

		//print_r($contentObject);

		// We check that this attribute exists
		if($contentObjectAttributes)
		{
			// Only one attribute so we have only one loop
			foreach($contentObjectAttributes as $key => $contentObjectAttribute)
			{
				$value_old = $contentObjectAttribute->toString();
			}

			// By default we return old value
			$response = $value_old;

			//echo $value_old, " : ",  $value_new,"<br>\n";

			// Create a new version & update attribute only if value has changed
			if($value_old != $value_new)
			{
				// CreateNewVersion is transaction unsafe
				$db = eZDB::instance();

				// Start to record
				$db->begin();

				// Get last version of those attributes
				$newVersion = $contentObject->createNewVersion( false, true, $languageCode, false );

				if($newVersion)
				{
					// It seems that "store" method, in function of datatype, implement his own begin/commit
					$new_version_id = $newVersion->attribute( "version" );

					$newContentObjectAttributes = $contentObject->fetchAttributesByIdentifier(array($attribute_identifier), $new_version_id, $languageCode);
					foreach($newContentObjectAttributes as $key => $newContentObjectAttribute)
					{
						$newContentObjectAttribute->fromString($value_new);
						$newContentObjectAttribute->store();
					}

					// Publish object new version
					$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectID,
														     'version' => $new_version_id ) );
					// If result is made then commit all
					if($operationResult['status'])
					{
						// If new version was created then return new value
						$response = $value_new;
					}
				}

				$db->commit();
			}
			else
			{
				//$response = 'No change';
			}
		}
		else
		{
			//$response = 'Attribute not found';
		}
	}
	else
	{
		//$response = 'No object, or language not found, or empty new value';
	}

	header( $_SERVER['SERVER_PROTOCOL'] . ' 200 ' );

	header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + MAX_AGE ) . ' GMT' );
	header( 'Cache-Control: cache, max-age=' . MAX_AGE . ', post-check=' . MAX_AGE . ', pre-check=' . MAX_AGE );
	header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $node->ModifiedSubNode ) . ' GMT' );
	header( 'Pragma: cache' );
	header( 'Content-Type: text/html' );
	header( 'Content-Length: '.strlen( $response) );

	echo $response;
}
else
{
	header( $_SERVER['SERVER_PROTOCOL'] . ' 404 ' );
}

eZExecution::cleanExit();

?>

EZP is Great

kracker (the)

Wednesday 15 July 2009 1:45:55 am

Sorry,

I didn't take time to read your code. It looks very clean at a glance and you have lots of eZp history under your own belt Martin.

But something new has been in the works which i've heard amazing reports via irc about from other developers working with eZ.

Checkout this new upcoming solution for creating content objects which is available now and by all reports i've heard it just simply -works-.

<i>http://pubsvn.ez.no/nextgen/trunk/tests/toolkit/extras/kernelwrapper/README
http://pubsvn.ez.no/nextgen/trunk/tests/toolkit/extras/kernelwrapper</i>

I hear this is functional right now, available for testing as separate and in the future will be integrated further (or something, possibly re-branded or something, not certain)

Happy hacking ...

Cheers,
<i>//kracker

Theme: Drake - Best I Ever Had ... (From me to eZ Publish)</i>

Member since: 2001.07.13 || http://ezpedia.se7enx.com/

H-Works Agency

Wednesday 15 July 2009 1:53:13 am

Uhm interesting i am gonna check this out.

Thanx !

EZP is Great

Matthieu Sévère

Wednesday 15 July 2009 3:51:39 am

Yes it seems very interesting but only for creating new content ... It seems that you can load a node and manipulate it, but it's a good start !

--
eZ certified developer: http://ez.no/certification/verify/346216

H-Works Agency

Wednesday 15 July 2009 4:24:07 am

The problem on this topic is, like often in ezp dev, the lack of documentation of the API.

- How to create an object with php ?
- How to modify an object attribute in the current version ?
- How to modify an object attribute and create a new version ?
- How to compare object versions ?

...etc

For now the response to those questions is "digg the code in /kernel & /lib and try".

At the end it usually works but first it dramatically impact developpement time and thus ezpublish adoption and second - without directives - we are never shure of using the right technique/function/class.

EZP is Great

eZ debug

Timing: Jan 30 2025 21:43:13
Script start
Timing: Jan 30 2025 21:43:13
Module start 'content'
Timing: Jan 30 2025 21:43:13
Module end 'content'
Timing: Jan 30 2025 21:43:13
Script end

Main resources:

Total runtime0.4007 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.0115 589.1953370.2734
Module start 'content' 0.01150.0152 959.46881,009.7031
Module end 'content' 0.02670.3739 1,969.17193,905.0391
Script end 0.4006  5,874.2109 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00451.1332200.0002
Check MTime0.00130.3234200.0001
Mysql Total
Database connection0.00100.243610.0010
Mysqli_queries0.263265.67771410.0019
Looping result0.00180.44791390.0000
Template Total0.373593.210.3735
Template load0.00080.211910.0008
Template processing0.372692.990710.3726
Override
Cache load0.00060.141010.0006
Sytem overhead
Fetch class attribute can translate value0.00230.566010.0023
XML
Image XML parsing0.00030.075610.0003
General
dbfile0.03598.9601200.0018
String conversion0.00000.002230.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