Skip to content
Browse files

Merge pull request #52 from houseoftech/master

CsvImportBehavior support hasMany associations
  • Loading branch information...
2 parents 5d7049d + df3d40b commit 267ffb9be6fbbdd53b1f054bd2f8bb5c4e06edf2 @burzum burzum committed
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] : '';
}
View
27 Test/Case/Model/Behavior/CsvImportTest.php 100644 → 100755
@@ -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
@@ -97,6 +97,31 @@ public function testImportCSV() {
}
/**
+ * 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
*
* @access public
View
18 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'
+ );
+
+}
+?>
View
3 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"

0 comments on commit 267ffb9

Please sign in to comment.
Something went wrong with that request. Please try again.