1. <?php
  2. class ProjectImportController extends BuildTask {
  3. public function index() {
  4. $csv = \League\Csv\Reader::createFromPath(BASE_PATH . '/mysite/import.csv');
  5. $data = $csv->fetchAssoc();
  6. foreach ($data as $row) {
  7. // $row is 1 row of the csv file containing all the columns of the file
  8. // the first line of the csv is used for the keys of the $row array
  9. $obj = new MyDataObject();
  10. // example normal field:
  11. $obj->Title = $row['Title'];
  12. // example image:
  13. // lets assume there is a filename in the csv file, and the file is in mysite/import/
  14. $file = trim($row['Logo']);
  15. if (file_exists(BASE_PATH . "/mysite/import/$file")) {
  16. $parentFolder = Folder::find_or_make('logos');
  17. $fileObj = new Image();
  18. $fileObj->Filename = $filename;
  19. $fileObj->ParentID = $parentFolder->ID;
  20. $fileObj->setName($file);
  21. $fileObj->write();
  22. copy(ASE_PATH . "/mysite/import/$file", BASE_PATH . "/{$obj->Filename}");
  23. // lets assume MyDataObject has a has_one relation to Image called Logo
  24. $obj->LogoID = $fileObj->ID;
  25. }
  26. $obj->write();
  27. // has_many and many_many relations should be done after the first $obj->write(), because then $obj has an ID which you need for relation saving
  28. }