Import dat do DB pomoci forech

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
parti
Člen | 117
+
0
-

Zdravim nevite nahodou proc mi cyklus forech neprojede vsechny radky a nezapise je do DB.?
Zapise jenom prvni radak.
Dekuji za kazdu radu.

private function saveData() {
        $file = new \SplFileObject($this->csv);
        $file->setFlags(\SplFileObject::READ_CSV);

        $i = 1;
        $file->setCsvControl(';');
        foreach ($file as $i => $cols) {

            if ($i === 0) {
                continue;
            }
            $data = [
                'Location' => self::clearCell($cols[0]),
                'Address' => self::clearCell($cols[1]),
                'Location_notes' => self::clearCell($cols[2]),
                'Room' => self::clearCell($cols[3]),
                'Room_location' => self::clearCell($cols[4]),
                'Room_notes' => self::clearCell($cols[5]),
                'Rack_coordinates' => self::clearCell($cols[6]),
                'Rack_notes' => self::clearCell($cols[7]),
                'Rack_asset_tag' => self::clearCell($cols[8]),
                'Rack_serial_no' => self::clearCell($cols[9]),
                'Rack_created' => self::clearCell($cols[10]),
                'Rack_modified' => self::clearCell($cols[11]),
                'Rack_audit' => self::clearCell($cols[12]),
                'Rack_audit_asset' => self::clearCell($cols[13]),
                'Rack_audit_serial' => self::clearCell($cols[14]),
                'Item_asset_tag' => self::clearCell($cols[15]),
                'Item_hostname' => self::clearCell($cols[16]),
                'Item_serial_no' => self::clearCell($cols[17]),
                'Item_notes' => self::clearCell($cols[18]),
                'Item_created' => self::clearCell($cols[19]),
                'Item_modified' => self::clearCell($cols[20]),
                'Item_audit' => self::clearCell($cols[21]),
                'Item_audit_hostname' => self::clearCell($cols[22]),
                'Item_audit_asset' => self::clearCell($cols[23]),
                'Item_audit_serial' => self::clearCell($cols[24])
            ];
           return $this->cd->saveCsvData($data);
        }
    }
CZechBoY
Člen | 3608
+
0
-

Pošli tu metodu na odeslání $this->cd->saveCsvData($data) – asi tam bude zádrhel.

edit: moje blbost…, je tam return který to ukončuje, takže metoda už na to nemá vliv

Editoval CZechBoY (15. 2. 2017 9:37)

parti
Člen | 117
+
0
-
public function saveCsvData($data){
       $this->database->table('physical_audit')->insert($data);
   }

Pokud odstranim to return tak se posunu dal,ale vypise mi to chybu

$this->cd->saveCsvData($data);

Notice

Undefined offset: 1
a to skoncim tady

foreach ($file as $i => $cols) {
            if ($i === 0) {
                continue;
            }
            $data = [
                'Location' => self::clearCell($cols[0]),
                'Address' => self::clearCell($cols[1]),

Editoval parti (14. 2. 2017 22:34)

parti
Člen | 117
+
-3
-

tak jsem to predelal do while asi tako a funguje to. Nevim klidne mi k tomu dejte komentar jestli je to ok neb ne.

public function saveData() {
        $fileOpen = fopen($this->csv, 'r');

        $i = 1;
        while ($row = fgets($fileOpen, 1024)) {

            if ($i > 1) {
                $cols = preg_split("/;|,|:/", $row);
                $data = [
                    'Location' => self::clearCell($cols[0]),
                    'Address' => self::clearCell($cols[1]),
                    'Location_notes' => self::clearCell($cols[2]),
                    'Room' => self::clearCell($cols[3]),
                    'Room_location' => self::clearCell($cols[4]),
                    'Room_notes' => self::clearCell($cols[5]),
                    'Rack_coordinates' => self::clearCell($cols[6]),
                    'Rack_notes' => self::clearCell($cols[7]),
                    'Rack_asset_tag' => self::clearCell($cols[8]),
                    'Rack_serial_no' => self::clearCell($cols[9]),
                    'Rack_created' => self::clearCell($cols[10]),
                    'Rack_modified' => self::clearCell($cols[11]),
                    'Rack_audit' => self::clearCell($cols[12]),
                    'Rack_audit_asset' => self::clearCell($cols[13]),
                    'Rack_audit_serial' => self::clearCell($cols[14]),
                    'Item_asset_tag' => self::clearCell($cols[15]),
                    'Item_hostname' => self::clearCell($cols[16]),
                    'Item_serial_no' => self::clearCell($cols[17]),
                    'Item_notes' => self::clearCell($cols[18]),
                    'Item_created' => self::clearCell($cols[19]),
                    'Item_modified' => self::clearCell($cols[20]),
                    'Item_audit' => self::clearCell($cols[21]),
                    'Item_audit_hostname' => self::clearCell($cols[22]),
                    'Item_audit_asset' => self::clearCell($cols[23]),
                    'Item_audit_serial' => self::clearCell($cols[24])
                ];
                $this->cd->saveCsvData($data);
            }$i++;
        }
h4kuna
Backer | 740
+
+2
-

Viz první příspěvek, to že ti zapíše jen první řádek je proto, že tam máš return v cyklu.

<?php
return $this->cd->saveCsvData($data);
?>

Smázni return.