php - OXID eShop: save() in an foreach does not work / don't understand save() function -


i want create copies of articles new oxid , new oxartnum. if call function create 1 new article works.

like that:

$this->copyarticle('my_oxid'); 

but call addarticle function in foreach loop. exception, saying there duplicate primary value (oxid primary field).

exception thrown

foreach($this->getatmp() $soxid => $iexcess){     $this->copyarticle($soxid); } 

the oxid generated correctly, there no duplicate. think problem oxids save function. article oxids copy selected in form. arrays contain correct values.

the problem $oarticle->save() function.

for understanding is:

  1. load article
  2. modify fields
  3. save article

my code:

<?php  class xnconnector2 { private $_soldid; private $_snewid; private $_sartnrpraefix = 'xn-'; private $_stitlesuffix; private $_irabatt; private $_atmp = array();  /**  * xnconnector2 constructor.  */ public function __construct(){     require_once dirname(__file__) . "/../bootstrap.php";      // set new field parameters     $this->setstitlesuffix($_post['titlesuffix']);     $this->setirabatt($_post['rabatt']);      // selected articles array     if (isset($_post['article'])) {         foreach ($_post['article'] $art) {             if (strlen($art['oxid']) > 0 && strlen($art['excess']) > 0) {                 $this->_atmp += array($art['oxid'] => $art['excess']);             }         }     } else {         die("es wurde keine oxid übergeben.");     }      foreach($this->getatmp() $soxid => $iexcess){         $this->copyarticle($soxid);     } }  /**  * copies article new article  *  * @param string $soxid old product id (default null)  */ public function copyarticle($soxid = null) {     $myconfig = oxregistry::getconfig();      $soldid = $soxid;     $this->setsnewid(oxutilsobject::getinstance()->generateuid());      $oarticle = oxnew("oxarticle");     $oarticle->disablelazyloading();      $oarticle->init('oxarticles');      // load article oxid $_post     if ($oarticle->loadinlang('0',$soldid)) {         $sparentid = $oarticle->oxarticles__oxparentid->value;         $oarticle->oxarticles__oxactive->setvalue('0');          $newartnum = $this->getsartnrpraefix().$oarticle->oxarticles__oxartnum->value;         $oarticle->oxarticles__oxartnum->setvalue($newartnum);          $oarticle->oxarticles__oxactivefrom->setvalue(0);         $oarticle->oxarticles__oxactiveto->setvalue(0);         $oarticle->oxarticles__oxparentid->setvalue('');         $stitlesuffix = $this->getstitlesuffix();         $oarticle->oxarticles__oxtitle = new oxfield($oarticle->oxarticles__oxtitle . " - " . $oarticle->oxarticles__oxvarselect->value . " (" . $stitlesuffix . ")");          // price - dicount         $dblalterpreis = $oarticle->oxarticles__oxprice->value;         $dblneuerpreis = $oarticle->oxarticles__oxprice->value - ($oarticle->oxarticles__oxprice->value * ($this->getirabatt() / 100));         $oarticle->oxarticles__oxprice  = new oxfield($dblneuerpreis,2);         $oarticle->oxarticles__oxtprice = new oxfield($dblalterpreis);         $oarticle->oxarticles__oxparentid->setvalue('');          // setting oxinsert/oxtimestamp         $inow = date('y-m-d h:i:s', oxregistry::get("oxutilsdate")->gettime());         $oarticle->oxarticles__oxinsert = new oxfield($inow);          // mantis#0001590: oxrating , oxratingcnt not set 0 when copying article         $oarticle->oxarticles__oxrating     = new oxfield(0);         $oarticle->oxarticles__oxratingcnt  = new oxfield(0);          // neue artikel oxid setzten         $oarticle->setid($this->getsnewid());          // save new article new oxid         $oarticle->save();          // if variant use oxparentid article details (used artextends)         if(strlen($sparentid) > 0){             $soldid =  $sparentid;         }          $oarticle->save();          // copy article extends (longdescription, tags)         $this->_copyartextends($soldid, $this->getsnewid());     } }  /**  * copying article extends  *  * @param string $soldid id old article  * @param string $snewid id new article  */ protected function _copyartextends($soldid, $snewid) {     $oext = oxnew("oxbase");     $oext->init("oxartextends");     $oext->load($soldid);     $oext->setid($snewid);     $oext->save(); }  /**  * copies article (with parameters) shop.  */ public function clonearticle() {     if ($soldid = $this->geteditobjectid()) {         $snewid = $this->copyarticle();          $snewarticle = oxnew("oxarticle");         $snewarticle->load($snewid);         $snewarticle->oxarticles__oxshopid->setvalue($this->getconfig()->getshopid());         $snewarticle->save();          $this->seteditobjectid($snewid);     } }  /**  * @return string  */ public function getsartnrpraefix() {     return $this->_sartnrpraefix; }  /**  * @param string $sartnrpraefix  */ public function setsartnrpraefix($sartnrpraefix) {     $this->_sartnrpraefix = $sartnrpraefix; }  /**  * @return mixed  */ public function getstitlesuffix() {     return $this->_stitlesuffix; }  /**  * @param mixed $stitlesuffix  */ public function setstitlesuffix($stitlesuffix) {     $this->_stitlesuffix = $stitlesuffix; }  /**  * @return mixed  */ public function getxnationstext() {     return $this->_xnationstext; }  /**  * @param mixed $xnationstext  */ public function setxnationstext($xnationstext) {     $this->_xnationstext = $xnationstext; }  /**  * @return mixed  */ public function getirabatt() {     return $this->_irabatt; }  /**  * @param mixed $irabatt  */ public function setirabatt($irabatt) {     $this->_irabatt = $irabatt; }  /**  * @return array  */ public function getatmp() {     return $this->_atmp; }  /**  * @return mixed  */ public function getsoldid() {     return $this->_soldid; }  /**  * @param mixed $soldid  */ public function setsoldid($soldid) {     $this->_soldid = $soldid; }  /**  * @return mixed  */ public function getsnewid() {     return $this->_snewid; }  /**  * @param mixed $snewid  */ public function setsnewid($snewid) {     $this->_snewid = $snewid; }  } $xnc = new xnconnector2(); 

edit: works, not , the duplicate primary exception thrown.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -