Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CsvImportBehavior Support hasMany associations

From: https://github.com/ProLoser/CakePHP-CSV

Support 3rd key in header field names so hasMany associations work.
Example: User.name, Profile.0.phone
  • Loading branch information...
commit 1f1545cfd59105a9edb11d3f5338fb6abb0123bd 1 parent 5d7049d
@houseoftech houseoftech authored
Showing with 11 additions and 7 deletions.
  1. +11 −7 Model/Behavior/CsvImportBehavior.php
View
18 Model/Behavior/CsvImportBehavior.php
@@ -84,11 +84,11 @@ protected function _getCSVLine(Model &$Model, SplFileObject $handle) {
*/
protected function _getHeader(Model &$Model, SplFileObject $handle) {
if ($this->settings[$Model->alias]['hasHeader'] === true) {
- $header = $this->_getCSVLine($Model, $handle);
- } else {
- $header = array_keys($Model->schema());
- }
- return $header;
+ $header = $this->_getCSVLine($Model, $handle);
+ } else {
+ $header = array_keys($Model->schema());
+ }
+ return $header;
}
/**
@@ -113,8 +113,12 @@ public function importCSV(Model &$Model, $file, $fixed = array(), $returnSaved =
foreach ($header as $k => $col) {
// get the data field from Model.field
if (strpos($col, '.') !== false) {
- list($model,$field) = explode('.',$col);
- $data[$model][$field]= (isset($row[$k])) ? $row[$k] : '';
+ $keys = explode('.', $col);
+ if (isset($keys[2])) {
+ $data[$keys[0]][$keys[1]][$keys[2]]= (isset($row[$k])) ? $row[$k] : '';
+ } else {
+ $data[$keys[0]][$keys[1]]= (isset($row[$k])) ? $row[$k] : '';
+ }
} else {
$data[$Model->alias][$col]= (isset($row[$k])) ? $row[$k] : '';
}
Please sign in to comment.
Something went wrong with that request. Please try again.