Skip to content

Commit

Permalink
Fix issue with HABTM fields
Browse files Browse the repository at this point in the history
habtm fields were not being correctly being detected.
  • Loading branch information
markstory committed Sep 9, 2011
1 parent d6b978c commit 1743eaa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
26 changes: 24 additions & 2 deletions lib/Cake/Test/Case/View/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ public function testSetEntityAssociated() {
$this->assertEquals($expected, $this->Helper->entity());

$this->assertEquals('HelperTestComment', $this->Helper->model());

}

/**
Expand All @@ -314,14 +313,37 @@ public function testSetEntityAssociated() {
* @return void
*/
public function testSetEntityAssociatedCamelCaseField() {
$this->Helper->fieldset = array('HelperTestComment' => array('fields' => array('BigField' => 'something')));
$this->Helper->fieldset = array(
'HelperTestComment' => array(
'fields' => array('BigField' => array('type' => 'integer'))
)
);
$this->Helper->setEntity('HelperTestComment', true);
$this->Helper->setEntity('HelperTestComment.BigField');

$this->assertEquals('HelperTestComment', $this->Helper->model());
$this->assertEquals('BigField', $this->Helper->field());
}

/**
* Test that multiple fields work when they are camelcase and in fieldset
*
* @return void
*/
public function testSetEntityAssociatedCamelCaseFieldHabtmMultiple() {
$this->Helper->fieldset = array(
'HelperTestComment' => array(
'fields' => array('Tag' => array('type' => 'multiple'))
)
);
$this->Helper->setEntity('HelperTestComment', true);
$this->Helper->setEntity('Tag');

$this->assertEquals('Tag', $this->Helper->model());
$this->assertEquals('Tag', $this->Helper->field());
$this->assertEquals(array('Tag', 'Tag'), $this->Helper->entity());
}

/**
* test that 'view' doesn't break things.
*
Expand Down
23 changes: 13 additions & 10 deletions lib/Cake/View/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,23 +454,26 @@ public function setEntity($entity, $setScope = false) {

$this->_association = null;

// check for associated model.
$reversed = array_reverse($parts);
foreach ($reversed as $part) {
if (empty($this->fieldset[$this->_modelScope]['fields'][$part]) && preg_match('/^[A-Z]/', $part)) {
$this->_association = $part;
break;
}
}

// habtm models are special
if (
isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
$this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple'
) {
$this->_association = $parts[0];
$entity = $parts[0] . '.' . $parts[0];
} else {
// check for associated model.
$reversed = array_reverse($parts);
foreach ($reversed as $part) {
if (
!isset($this->fieldset[$this->_modelScope]['fields'][$part]) &&
preg_match('/^[A-Z]/', $part)
) {
$this->_association = $part;
break;
}
}
}

$this->_entityPath = $entity;
return;
}
Expand Down

0 comments on commit 1743eaa

Please sign in to comment.