Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

CsvImportBehavior support hasMany associations #52

Merged
merged 2 commits into from Oct 26, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions Model/Behavior/CsvImportBehavior.php
Expand Up @@ -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;
}

/**
Expand All @@ -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] : '';
}
Expand Down
27 changes: 26 additions & 1 deletion Test/Case/Model/Behavior/CsvImportTest.php 100644 → 100755
Expand Up @@ -37,7 +37,7 @@ class CsvImportTest extends CakeTestCase {
* @var array
* @access public
*/
public $fixtures = array('plugin.utils.content');
public $fixtures = array('plugin.utils.content', 'plugin.utils.comment');

/**
* Creates the model instance
Expand Down Expand Up @@ -96,6 +96,31 @@ public function testImportCSV() {
$this->assertEqual($permalinks, array(13444555, 'A permalink'));
}

/**
* testImportCSVWithHasMany
*
* @access public
* @return void
*/
public function testImportCSVWithHasMany() {
$this->Content->bindModel(array(
'hasMany' => array(
'Comment'
)
), false);
$this->Content->Behaviors->load('Utils.CsvImport');
$path = App::pluginPath('Utils');
$result = $this->Content->importCSV($path . 'Test' . DS . 'tmp' . DS . 'test2.csv');
$this->assertTrue($result);

$records = $this->Content->find('all', array('order' => 'created DESC', 'limit' => 2));
$titles = Set::extract('/Comment/body', $records);
$this->assertEqual($titles, array('really? how strange?', 'very good read'));

$permalinks = Set::extract('/Content/permalink', $records);
$this->assertEqual($permalinks, array(13444555, 'A permalink'));
}

/**
* testImportCSVWithCallback
*
Expand Down
18 changes: 18 additions & 0 deletions Test/Fixture/CommentFixture.php
@@ -0,0 +1,18 @@
<?php
// For the inheritable behavior
class CommentFixture extends CakeTestFixture {
var $name = 'Comment';

var $fields = array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'content_id' => array('type' => 'integer', 'null' => false),
'body' => 'text',
'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
'permalink' => array('type' => 'string'),
'parent_id' => array('type' => 'integer'),
'created' => 'datetime',
'updated' => 'datetime'
);

}
?>
3 changes: 3 additions & 0 deletions Test/Tmp/test2.csv
@@ -0,0 +1,3 @@
user_id;title;body;type;permalink;Comment.0.body
1;"Unearthed rare monster in london";"very strange discovery...";Article;13444555;"really? how strange?"
1;"Another Title";"This is a body";Book;"A permalink";"very good read"