Okazuje sie, ze bardzo namieszalem w kodzie.
W tej chili mam cos takiego:
$row = 1;
if (($handle = fopen($path."szablon-last.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { $row++;
for ($c=0; $c < $num; $c++) {
$product = new Products();
$product_sku = iconv('windows-1250', 'utf-8', $data[0]);
$product->setSku($product_sku);
$product_name = iconv('windows-1250', 'utf-8', $data[1]);
$product->setName($product_name);
// Zdjęcia produktów
$product_images = iconv('windows-1250', 'utf-8', $data[2]);
if ($product_images != NULL) {
$images = new Images();
$images_chunks = explode("|", $product_images);
for ($i = 0; $i < count($images_chunks); $i++) { if (file_exists($this->getUploadDirImages().$images_chunks[$i])) {
$file = $this->getUploadDirImages().$images_chunks[$i];
$image_name = $images_chunks[$i];
if (!file_exists($this->getProductImagesDir().$product_slug)) { mkdir("C:/xampp/htdocs/sklep/web/images/products/".$product_slug, 0777
, true); continue;
}
$newfile = $this->getProductImagesDir().$product_slug.'/'.$images_chunks[$i];
$filetmp = "C:/xampp/htdocs/sklep/web/uploads/csv/images/".$images_chunks[$i];
$newfiletmp = "C:/xampp/htdocs/sklep/web/images/products/".$product_slug.'/'.$images_chunks[$i];
if (!copy($filetmp, $newfiletmp)) { echo "Problem z kopiowaniem pliku: $images_chunks[$i]"."<br />"; continue;
}
$images->setProducts($product);
$images->setFilename($image_name);
$images->setIsMain(0);
$images->setIsActive(1);
$manager->persist($images);
} else {
echo "Brak załączonego zdjęcia: $images_chunks[$i]"."<br />"; continue;
}
}
}
$manager->persist($product);
$manager->flush();
}
}
}
W tej chwili to dziala (bez dodawania atrybutow dla produktow), ale strasznie wolno. Kopiowanie zdjec pomiedzy katalogami trwa kupe czasu. Nie sa one jakies duze, maksymalnie 400 KB przypada na jedno.
Wg Was mozna jakos zoptymalizowac skrypt, aby dzialal szybciej?
I kolejna sprawa: mam produkty i kategorie paroduktow w relacji n:m oraz dodalem kolumne z nr katalogowym produktu. Kodowanie pliku z szablonem CSV to latin2/win-1250, a baze danych mam w utf-8, dlatego ten iconv.
Plik CSV wyglada teraz tak:
"nr_katalogowy::sku";nazwa_produktu::name";"zdjecia::product_img";"kategorie:product_cats";"styl";"rozmiar"
"a1";"produkt nr 1";"zdjecie_nr_1.jpg|zdjecie_nr_2.jpg|zdjecie_nr_3.jpg|zdjecie_nr_4.jpg";"kategorie|kabiny|prysznicowe";;
"a2";"produkt nr 2";"zdjecie_nr_1.jpg|zdjecie_nr_2.jpg|zdjecie_nr_3.jpg";"kategorie|kabiny|prysznicowe";"nowoczesny|retro";"20cm"
"a3";"produkt nr 3";"zdjecie_nr_1.jpg|zdjecie_nr_2.jpg";"kategorie|kabiny|prysznicowe";"nowoczesny|retro|klasyczny";"30cm|10cm"
Fragment z kontrolera:
$row = 1;
if (($handle = fopen($path."szablon-last.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { $row++;
for ($c=0; $c < $num; $c++) {
$product = new Products();
$product_sku = iconv('windows-1250', 'utf-8', $data[0]);
$product->setSku($product_sku);
$product_name = iconv('windows-1250', 'utf-8', $data[1]);
$product->setName($product_name);
$product_slug = $this->slugify($product_name);
$product->setSlug($product_slug); // Przyjazny link
$product_categories = iconv('windows-1250', 'utf-8', $data[3]);
if ($product_categories != NULL) {
$categories_chunks = explode("|", $product_categories); $cat = (count($categories_chunks) - 1
); // wartosc zmiennej to "prysznicowe" }
// Zdjęcia produktów
$product_images = iconv('windows-1250', 'utf-8', $data[2]);
if ($product_images != NULL) {
$images = new Images();
$images_chunks = explode("|", $product_images);
for ($i = 0; $i < count($images_chunks); $i++) { if (file_exists($this->getUploadDirImages().$images_chunks[$i])) {
$file = $this->getUploadDirImages().$images_chunks[$i];
$image_name = $images_chunks[$i];
if (!file_exists($this->getProductImagesDir().$product_slug)) { mkdir("C:/xampp/htdocs/sklep/web/images/products/".$product_slug, 0777
, true); continue;
}
$newfile = $this->getProductImagesDir().$product_slug.'/'.$images_chunks[$i];
$filetmp = "C:/xampp/htdocs/sklep/web/uploads/csv/images/".$images_chunks[$i];
$newfiletmp = "C:/xampp/htdocs/sklep/web/images/products/".$product_slug.'/'.$images_chunks[$i];
if (!copy($filetmp, $newfiletmp)) { echo "Problem z kopiowaniem pliku: $images_chunks[$i]"."<br />"; continue;
}
$images->setProducts($product);
$images->setFilename($image_name);
$images->setIsMain(0);
$images->setIsActive(1);
$manager->persist($images);
} else {
echo "Brak załączonego zdjęcia: $images_chunks[$i]"."<br />"; continue;
}
}
}
$manager->persist($product);
$manager->flush();
}
}
}
Encja produktow:
/**
* Products
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Ml\FrontendBundle\Entity\ProductsRepository")
*/
class Products
{
...
/**
* @ORM\OneToMany(targetEntity="Images", mappedBy="products", cascade={"persist"})
*/
protected $images;
/**
* @ORM\OneToMany(targetEntity="Attributes", mappedBy="products", cascade={"persist"})
*/
protected $attributes;
/**
* @ORM\ManyToMany(targetEntity="Categories", inversedBy="products")
*/
protected $categories;
public function __toString() {
return $this->name;
}
...
/**
* Constructor
*/
public function __construct()
{
$this->images = new \Doctrine\Common\Collections\ArrayCollection();
$this->attributes = new \Doctrine\Common\Collections\ArrayCollection();
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
...
/**
* Add category
*
* @param \Ml\FrontendBundle\Entity\Categories $category
*
* @return Products
*/
public function addCategory(\Ml\FrontendBundle\Entity\Categories $category)
{
$this->categories[] = $category;
return $this;
}
/**
* Remove category
*
* @param \Ml\FrontendBundle\Entity\Categories $category
*/
public function removeCategory(\Ml\FrontendBundle\Entity\Categories $category)
{
$this->categories->removeElement($category);
}
/**
* Get categories
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
...
Encja kategorii:
/**
* Categories
*
* @Gedmo\Tree(type="nested")
* @ORM\Table(name="categories")
* @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
*/
class Categories
{
/**
* @var integer
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @Assert\NotBlank()
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string $slug
*
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(length=255, unique=true)
*/
private $slug;
// gedmo tree
/**
* @ORM\ManyToMany(targetEntity="Products", mappedBy="categories")
*/
protected $products;
/**
* @ORM\ManyToMany(targetEntity="Attributes", mappedBy="categories")
*/
protected $attributes;
...
public function __toString() {
return $this->name;
}
/**
* Constructor
*/
public function __construct()
{
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
$this->products = new \Doctrine\Common\Collections\ArrayCollection();
$this->attributes = new \Doctrine\Common\Collections\ArrayCollection();
}
...
/**
* Add product
*
* @param \Ml\FrontendBundle\Entity\Products $product
*
* @return Categories
*/
public function addProduct(\Ml\FrontendBundle\Entity\Products $product)
{
$this->products[] = $product;
return $this;
}
/**
* Remove product
*
* @param \Ml\FrontendBundle\Entity\Products $product
*/
public function removeProduct(\Ml\FrontendBundle\Entity\Products $product)
{
$this->products->removeElement($product);
}
/**
* Get products
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
...
Mam problem z zapisem id produktu i id kategorii do tabeli laczacej "products_categories";
Czy ktos mi pomoze?