{"id":18389749,"url":"https://github.com/proloser/cakephp-csv","last_synced_at":"2025-05-07T15:09:22.679Z","repository":{"id":780672,"uuid":"473006","full_name":"ProLoser/CakePHP-CSV","owner":"ProLoser","description":"A component that will import/export data from a csv file into a save-friendly nested model data array format.","archived":false,"fork":false,"pushed_at":"2019-01-08T07:11:26.000Z","size":39,"stargazers_count":52,"open_issues_count":4,"forks_count":41,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-05-07T15:09:14.865Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"dongri/convert-to-utf8","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ProLoser.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-01-15T03:23:33.000Z","updated_at":"2024-03-05T23:57:34.000Z","dependencies_parsed_at":"2022-07-05T14:30:54.905Z","dependency_job_id":null,"html_url":"https://github.com/ProLoser/CakePHP-CSV","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProLoser%2FCakePHP-CSV","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProLoser%2FCakePHP-CSV/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProLoser%2FCakePHP-CSV/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProLoser%2FCakePHP-CSV/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ProLoser","download_url":"https://codeload.github.com/ProLoser/CakePHP-CSV/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252902614,"owners_count":21822261,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T01:44:21.093Z","updated_at":"2025-05-07T15:09:22.660Z","avatar_url":"https://github.com/ProLoser.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CSV Plugin\n\nAllows the importing and exporting of a standard $this-\u003edata formatted array to and from csv files.\nDoesn't currently support HABTM.\n\n## Options\n\nImporting, exporting and setup come with the same options and default values\n\n```php\n$options = array(\n\t// Refer to php.net fgetcsv for more information\n\t'length' =\u003e 0,\n\t'delimiter' =\u003e ',',\n\t'enclosure' =\u003e '\"',\n\t'escape' =\u003e '\\\\',\n\t// Generates a Model.field headings row from the csv file\n\t'headers' =\u003e true,\n\t// If true, String $content is the data, not a path to the file\n\t'text' =\u003e false,\n)\n```\n\n## Instructions\n\n* Add Behavior to the table\n\n```php\n\u003c?php\nnamespace App\\Model\\Table;\n\nuse Cake\\ORM\\Query;\nuse Cake\\ORM\\Table;\n\n/**\n * Posts Model\n */\nclass PostsTable extends Table\n{\n\n    /**\n     * Initialize method\n     *\n     * @param array $config The configuration for the Table.\n     * @return void\n     */\n    public function initialize(array $config)\n    {\n        //$options = ...\n        $this-\u003eaddBehavior('CakePHPCSV.Csv', $options);\n    }\n}\n?\u003e\n```\n\n### Importing\n\n* Upload a csv file to the server\n\n* Import the csv file into your data variable:\n\n**Approach 1:** Use a CSV file with the first row being Model.field headers\n\n```php\nPosts.csv\nPost.title, Post.created, Post.modified, body, user_id, Section.name, Category.0.name, Category.0.description, Category.1.name, Category.1.description\n..., ..., ...\n```\n\n```php\n$this-\u003edata = $this-\u003ePosts-\u003eimport($content, $options);\n```\n\n**Approach 2:** Pass an array of fields (in order) to the method\n\n```php\n$data = $this-\u003ePosts-\u003eimport($content, array('Post.title', 'Post.created', 'Post.modified', 'body', 'user_id', 'Category.0.name', 'Category.0.description', 'Category.1.name', 'Category.1.description'));\n```\n\n* Process/save/whatever with the data\n\n```php\n$entities = $this-\u003ePosts-\u003enewEntities($data);\n$Table = $this-\u003ePosts;\n$Table-\u003econnection()-\u003etransactional(function () use ($Table, $entities) {\n    foreach ($entities as $entity) {\n        $Table-\u003esave($entity, ['atomic' =\u003e false]);\n    }\n});\n```\n\n### Exporting\n\n* Populate an $this-\u003edata type array\n\n```php\n$data = $this-\u003ePost-\u003efind()-\u003eall();\n```\n\n* Export to a file in a writeable directory\n\n```php\n$this-\u003ePosts-\u003eexportCsv($filepath, $data, $options);\n```\n\n### Additional optional callbacks:\n\n* `beforeImportCsv($filename, $fields, $options)` returns boolean $continue\n* `afterImportCsv($data)`\n* `beforeExportCsv($filename, $data, $options)` returns boolean $continue\n* `afterExportCsv()`\n\n### FAQ\n\n#### Incorrect Line Endings (OSX)\nSome people [have mentioned](https://github.com/ProLoser/CakePHP-CSV/issues/6) having incorrect line endings. This can be fixed by having this in your php codebase:\n```\nini_set(\"auto_detect_line_endings\", true);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fproloser%2Fcakephp-csv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fproloser%2Fcakephp-csv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fproloser%2Fcakephp-csv/lists"}