Zdravím, mám model který pracuje s feedem dodavatele.
public function zpracujXmlZbozi($xml) {
foreach ($xml->StoItem as $item) {
$idKat = $this->connection->query(sprintf("SELECT product_id FROM product WHERE code = '%s' LIMIT 1", $item->Code))->fetch();
$nazev = $idKat ? $this->connection->query(sprintf("SELECT title FROM product WHERE product_id = %s LIMIT 1", $idKat->product_id))->fetch() : false;
if (!$nazev) {
Debugger::log($item->Id . ': ' . $item->Name, 'import');
$dataCatalog = [
'product_id' => intval($item->Id),
'code' => (string) ($item->Code),
'url' => Strings::webalize($item->Name),
'title' => (string) $item->Name,
'short_description' => (string) $item->Note,
'description' => (string) $item->Note,
'price' => intval($item->PriceEU),
'old_price' => intval($item->PriceDea),
'rating_sum' => 0,
'ratings' => 0,
'stock' => intval($item->QtyFree),
'images_count' => 1,
'hidden' => 0
];
$this->connection->query('INSERT INTO product', $dataCatalog);
$lastId = $this->connection->getInsertId();
/* katerogie */
foreach (['Level1', 'Level2', 'Level3'] AS $_level) {
$tmp = explode(' ', $item->SPresentTree->{$_level});
unset($tmp[0]);
$level = implode(' ', $tmp);
$kategorieId = $this->connection->query(sprintf('SELECT category_id FROM category WHERE title = "%s" LIMIT 1', $level))->fetch();
if ($kategorieId) {
$dataKat = [
'product_id' => $lastId,
'category_id' => $kategorieId->category_id
];
$this->connection->query('INSERT INTO product_category', $dataKat);
}
}
if ($item->ImgUrl) {
$count = count(\Nette\Utils\Finder::findFiles(intval($item->Id) . '_*.jpg')->in($this->slozka));
$imageId = preg_replace("/(.*)stiid=(.*)/", "$2", (string) $item->ImgUrl);
$filename = intval($item->Id) . '_1.jpg';
if (!file_exists($this->slozka . '/' .$filename)) {
$components = $this->slozka . '/' . $lastId;
mkdir($components, 0777, true);
copy($item->ImgUrl, $this->slozka . $lastId . '/' . $filename);
$count++;
$this->connection->query('UPDATE product SET images_count= ' . $count . ' WHERE product_id = ' . $lastId);
}
$this->createThumb($item->Id, $this->slozka . $lastId . '/' . $filename);
}
} else {
$lastId = $this->connection->query(sprintf("SELECT product_id FROM product WHERE product_id = %s LIMIT 1", $idKat->product_id))->fetch()->product_id;
foreach (['Level1', 'Level2', 'Level3'] AS $_level) {
$tmp = explode(' ', $item->SPresentTree->{$_level});
unset($tmp[0]);
$level = implode(' ', $tmp);
if ($level) {
$kategorieId = $this->connection->query(sprintf('SELECT category_id FROM category WHERE title = "%s" LIMIT 1', $level))->fetch();
if ($kategorieId) {
$bindCat = $this->connection->query(sprintf("SELECT product_id FROM product_category WHERE product_id = %s AND category_id = %s LIMIT 1", $lastId, $kategorieId->category_id))->fetch();
if (!$bindCat) {
$dataKat = [
'product_id' => $lastId,
'category_id' => $kategorieId->category_id
];
$this->connection->query('INSERT INTO product_category', $dataKat);
}
}
}
}
/* UPDATE */
$this->connection->query(sprintf('UPDATE product SET stock = %s WHERE product_id = %s',intval($item->QtyFree), $lastId));
}
}
}
A mám tam udělaný i update skladových zásob ale všiml jsem si že můj dodavatel už některé produkty které já v databízi mám již nemá... Potřeboval bych tedy poradit jak udělat kontrolu že pokud není produkt v xml feedu tak se z db vymaže.. jelikož tam mám pak problém že mi haprujou Idčka a hádají se...
Nebo nějaký jiný nápad?
Díky všem za pomoc