Browse Source

Fix tests.

Mark Scherer 10 years ago
parent
commit
8a7166d17a
2 changed files with 26 additions and 0 deletions
  1. 21 0
      src/Database/Type/ArrayType.php
  2. 5 0
      src/Model/Behavior/JsonableBehavior.php

+ 21 - 0
src/Database/Type/ArrayType.php

@@ -0,0 +1,21 @@
+<?php
+namespace Tools\Database\Type;
+
+use Cake\Database\Driver;
+use Cake\Database\Type;
+use PDO;
+
+/**
+ * Do not convert input on marshal().
+ */
+class ArrayType extends Type {
+
+	/**
+	 * @param mixed $value
+	 * @return mixed
+	 */
+	public function marshal($value) {
+		return $value;
+	}
+
+}

+ 5 - 0
src/Model/Behavior/JsonableBehavior.php

@@ -7,6 +7,7 @@ use Cake\ORM\Entity;
 use Cake\ORM\Query;
 use Cake\ORM\Query;
 use Tools\Utility\Text;
 use Tools\Utility\Text;
 use Cake\Datasource\ResultSetInterface;
 use Cake\Datasource\ResultSetInterface;
+use Cake\Database\Type;
 
 
 /**
 /**
  * A behavior that will json_encode (and json_decode) fields if they contain an array or specific pattern.
  * A behavior that will json_encode (and json_decode) fields if they contain an array or specific pattern.
@@ -70,6 +71,7 @@ class JsonableBehavior extends Behavior {
 	 * @return void
 	 * @return void
 	 */
 	 */
 	public function initialize(array $config = []) {
 	public function initialize(array $config = []) {
+		Type::map('array', 'Tools\Database\Type\ArrayType');
 		if (empty($this->_config['fields'])) {
 		if (empty($this->_config['fields'])) {
 			throw new \Exception('Fields are required');
 			throw new \Exception('Fields are required');
 		}
 		}
@@ -82,6 +84,9 @@ class JsonableBehavior extends Behavior {
 		if (!empty($this->_config['map']) && count($this->_config['fields']) !== count($this->_config['map'])) {
 		if (!empty($this->_config['map']) && count($this->_config['fields']) !== count($this->_config['map'])) {
 			throw new \Exception('Fields and Map need to be of the same length if map is specified.');
 			throw new \Exception('Fields and Map need to be of the same length if map is specified.');
 		}
 		}
+		foreach ($this->_config['fields'] as $field) {
+			$this->_table->schema()->columnType($field, 'array');
+		}
 	}
 	}
 
 
 	/**
 	/**