Discussion:
[bdbxml] XmlModify and transactions in PHP
George Feinberg
2006-04-17 14:13:17 UTC
Permalink
Temo,
I'm working on a PHP manager to handle articles on line. I have been
using XmlModify in some scripts but when I create an environment to
modify a document within a transaction the XmlModify doesn't apply
the changes.
My cuestion is about how to handle a XmlModify into transactions in
PHP.
I'm think that I am doing something wrong. I trying to isolate the
transaction in a sigle scrips reading and using many of the basic
examples.
The problem is at least partly related to use of the transaction
to open the container. If you use the DBXML_TRANSACTIONAL
flag, you should not also pass a transaction. That, and if you
*do* pass a transaction to open or create a container, it must
be committed immediately after, and not used for anything else.

See below...
<?php
$env = new Db4Env();
$env->open();
$mgr = new XmlManager($env);
$txn = $mgr->createTransaction();
Don't do above, move it down...
$con = $mgr->openContainer($txn,"test.dbxml",DBXML_TRANSACTIONAL);
Change this to :
$con = $mgr->openContainer("test.dbxml",DBXML_TRANSACTIONAL);
$qc = $mgr->createQueryContext();
$uc = $mgr->createUpdateContext();
$mod = $mgr->createModify();
Now start your transaction for the modify:
$txn = $mgr->createTransaction();
$select = $mgr->prepare($txn,"/book",$qc);
$mod->addAppendStep($select,XmlModify_Element,"section","some
text");
$retdoc=$mgr->query($txn,"doc('test.dbxml/book1')/book");
$mod->execute($txn,$retdoc,$qc,$uc);
$txn->commit();
unset($con);
$env->close();
?>
See if that helps,

Regards,

George
In other side, the following script XmlModify works nicely without
<?php
$mgr = new XmlManager();
$con = $mgr->openContainer("test.dbxml");
$qc = $mgr->createQueryContext();
$uc = $mgr->createUpdateContext();
$mod = $mgr->createModify();
$select = $mgr->prepare("/book",$qc);
$mod->addAppendStep($select,XmlModify_Element,"section","some
text");
$retdoc=$mgr->query("doc('test.dbxml/book1')/book");
$mod->execute($retdoc,$qc,$uc);
unset($doc);
unset($con);
?>
The container and the document was created with the following
<?php
$book_name = 'book1';
$book_content = '<book><title>This the title of the document.</
title></book>';
$mgr = new XmlManager();
$con = $mgr->createContainer("test.dbxml");
$con->putDocument($book_name, $book_content);
$doc = $con->getDocument($book_name);
$s = $doc->getContentAsString();
print $doc->getName(). " = $s\n";
unset($doc);
unset($con);
?>
Thanks for any help you can offer!
Regards,
Temo
------------------------------------------
To remove yourself from this list, send an
email to xml-unsubscribe-***@public.gmane.org
Joseba Umbelina
2006-04-17 16:30:47 UTC
Permalink
Hi all. Related with this, I have a strange problem with this script for modify
a document (I have cuted the second query because was > 80 characters):

<?php

$mgr=new XmlManager(null);
$con=$mgr->openContainer("userscontainer.dbxml");
$qc=$mgr->createQueryContext();
$uc=$mgr->createUpdateContext();
$mod=$mgr->createModify();

$select=$mgr->prepare("/user[@uname='maria']/name", $qc);
$mod->addUpdateStep($select,"aaaa");
$doc=$mgr->query("doc('userscontainer.dbxml/userdb')/users");
$mod->execute($doc, $qc, $uc);

unset($doc);
unset($con);

//Print the modified database as XML document:
$con=$mgr->openContainer("userscontainer.dbxml");
$results=$mgr->query("collection('userscontainer.dbxml')
/users/user[@uname='maria']/name");
$results->reset();
$filename ="prueba.xml";
$file= fopen($filename,'w+');
while($results->hasNext())
{
$val=$results->next();
$doc=$val->asDocument();
fputs ($file, $val->asString()."\n");
}
$con->close();
header ("Location: prueba.xml");
fclose($file);
?>

The problem is that works fine the first time, modifing the 'name' content to
"aaaa". But If I try a second time replacing "aaaa" with other string, doent's
change anything at the document. I delete de "prueba.xml" file, restart the
browser, but nothing. The only way to get working again, it's to create again
the container at put the xml element, like the first time:

I do it with the dbxml shell:

dbxml> createContainer userscontainer.dbxml
Creating node storage container with nodes indexed
dbxml> openContainer userscontainer.dbxml
dbxml> putdocument userdb base.xml f
Document added, name = userdb
dbxml> exit

Do you know which can be the problem?

I tried with transactions and I have the same problem..

Thank you very much. Bye.







------------------------------------------
To remove yourself from this list, send an
email to xml-unsubscribe-***@public.gmane.org

Loading...