Skip to content

Commit

Permalink
Fixing #42 and #39 - made the schema getting the right schema data no…
Browse files Browse the repository at this point in the history
…w from the sectionSchemas.
  • Loading branch information
Florian Krämer committed Mar 22, 2012
1 parent d1373b0 commit 4366a89
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
21 changes: 17 additions & 4 deletions Model/UserDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ public function getSection($userId = null, $section = null) {
return $results;
}

/**
* Overriding this method to inject the active section schema
*
* @param mixed $field Set to true to reload schema, or a string to return a specific field
* @return array Array of table metadata
*/
public function schema($field = false) {
if (isset($this->activeSectionSchema) && !empty($this->sectionSchema[$this->activeSectionSchema])) {
return $this->sectionSchema[$this->activeSectionSchema];
}
return parent::schema($field);
}

/**
* Save details for named section
*
Expand All @@ -175,8 +188,7 @@ public function getSection($userId = null, $section = null) {
*/
public function saveSection($userId = null, $data = null, $section = null) {
if (!empty($this->sectionSchema[$section])) {
$tmpSchema = $this->_schema;
$this->_schema = $this->sectionSchema[$section];
$this->activeSectionSchema = $section;

foreach($data as $model => $userDetails) {
if ($model == $this->alias) {
Expand All @@ -197,8 +209,8 @@ public function saveSection($userId = null, $data = null, $section = null) {
$this->validate = $tmpValidate;
}

if (isset($tmpSchema)) {
$this->_schema = $tmpSchema;
if (isset($this->activeSectionSchema)) {
unset($this->activeSectionSchema);
}

if (!empty($data) && is_array($data)) {
Expand Down Expand Up @@ -245,3 +257,4 @@ public function saveSection($userId = null, $data = null, $section = null) {
return true;
}
}
;
22 changes: 20 additions & 2 deletions Test/Case/Model/UserDetailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,35 @@ public function testSaveSection() {
*/
public function testDateSaving() {
$this->UserDetail->sectionSchema['User'] = array(
'birthday' => array('type' => 'date'));
'birthday' => array('type' => 'date'),
'start_time' => array('type' => 'time'),
'date_time' => array('type' => 'datetime'));

$data = array(
'UserDetail' => array(
'date_time' => array(
'day' => '01',
'month' => '04',
'year' => '1983',
'hour' => '12',
'min' => '35',
'meridian' => 'pm'),
'start_time' => array(
'hour' => '12',
'min' => '35',
'meridian' => 'pm'),
'birthday' => array(
'day' => '01',
'month' => '04',
'year' => '1983')));

$this->UserDetail->saveSection('47ea303a-3cyc-k251-b313-4811c0a800bf', $data, 'User');
$result = $this->UserDetail->getSection('47ea303a-3cyc-k251-b313-4811c0a800bf', 'User');
$this->assertEqual($result['UserDetail']['birthday'], '1983-04-01');

$this->assertEqual($result['User'], array(
'birthday' => '1983-04-01',
'date_time' => '1983-04-01 12:35:00',
'start_time' => '12:35:00'));
}

}

0 comments on commit 4366a89

Please sign in to comment.