Skip to content

Commit

Permalink
fixed keyify to properly convert something like BuildingType to build…
Browse files Browse the repository at this point in the history
…ing_type_id instead of buildingtype_id
  • Loading branch information
kla committed Jun 28, 2010
1 parent f90c6a2 commit 5e93d26
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
7 changes: 6 additions & 1 deletion lib/Inflector.php
Expand Up @@ -101,6 +101,11 @@ public function underscorify($s)
return preg_replace(array('/[_\- ]+/','/([a-z])([A-Z])/'),array('_','\\1_\\2'),trim($s));
}

public function keyify($class_name)
{
return strtolower($this->underscorify(denamespace($class_name))) . '_id';
}

abstract function variablize($s);
}

Expand All @@ -112,4 +117,4 @@ class StandardInflector extends Inflector
public function tableize($s) { return Utils::pluralize(strtolower($this->underscorify($s))); }
public function variablize($s) { return str_replace(array('-',' '),array('_','_'),strtolower(trim($s))); }
}
?>
?>
9 changes: 2 additions & 7 deletions lib/Relationship.php
Expand Up @@ -236,11 +236,6 @@ protected function unset_non_finder_options($options)
return $options;
}

protected function keyify($class_name)
{
return strtolower(classify(denamespace($class_name))). '_id';
}

/**
* Infers the $this->class_name based on $this->attribute_name.
*
Expand Down Expand Up @@ -441,7 +436,7 @@ protected function set_keys($model_class_name, $override=false)
{
//infer from class_name
if (!$this->foreign_key || $override)
$this->foreign_key = array($this->keyify($model_class_name));
$this->foreign_key = array(Inflector::instance()->keyify($model_class_name));

if (!$this->primary_key || $override)
$this->primary_key = Table::load($model_class_name)->pk;
Expand Down Expand Up @@ -607,7 +602,7 @@ public function __construct($options=array())

//infer from class_name
if (!$this->foreign_key)
$this->foreign_key = array($this->keyify($this->class_name));
$this->foreign_key = array(Inflector::instance()->keyify($this->class_name));

$this->primary_key = array(Table::load($this->class_name)->pk[0]);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Utils.php
Expand Up @@ -348,4 +348,4 @@ public static function squeeze($char, $string)
return preg_replace("/$char+/",$char,$string);
}
};
?>
?>
7 changes: 6 additions & 1 deletion test/InflectorTest.php
Expand Up @@ -20,5 +20,10 @@ public function test_tableize()
$this->assert_equals('angry_people',$this->inflector->tableize('AngryPerson'));
$this->assert_equals('my_sqls',$this->inflector->tableize('MySQL'));
}

public function test_keyify()
{
$this->assert_equals('building_type_id', $this->inflector->keyify('BuildingType'));
}
};
?>
?>

0 comments on commit 5e93d26

Please sign in to comment.