function copyProduct() { $sourceProduct = new Product($this->input->post('productId')); if (!$sourceProduct->exists()) { show_404(); } else { //copy product first $product = $sourceProduct->get_copy(); $i = 0; do { $i++; $name = "COPY " . $i . " OF - " . $sourceProduct->name; $checkName = new Product(); $checkName->where('name', $name); $checkName->get(); } while ($checkName->result_count() > 0); $product->name = $name; $product->productCode = ""; $product->save(); //copy category $sourceCategory = $sourceProduct->productcat; $product->save($sourceCategory->all); //copy spec $sourceSpec = $sourceProduct->productspec; foreach ($sourceSpec as $row) { $specItem = $row->get_copy(); $specItem->product_id = $product->id; $specItem->save(); $specItem->clear(); } //photo copy $photo = new Productphoto(); $photo->where('product_id', $sourceProduct->id); $photo->get(); foreach ($photo as $row) { $photoitem = $row->get_copy(); $photoitem->product_id = $product->id; $photoitem->save(); $photoitem->clear(); } //store copy $store = $sourceProduct->store; $product->save($store->all); //accessory $acces = $sourceProduct->accessory; foreach ($acces as $row) { $accessory = new Product($row->id); $product->save(array('accessories' => $accessory)); $accessory->clear(); } redirect("admin/products/edit/" . $product->id); } }