Skip to content

Instantly share code, notes, and snippets.

@greut
Created March 27, 2011 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greut/889432 to your computer and use it in GitHub Desktop.
Save greut/889432 to your computer and use it in GitHub Desktop.
Lazy namespace loading for PHP-AR
diff --git a/lib/Relationship.php b/lib/Relationship.php
index 56e4584..58540d4 100644
--- a/lib/Relationship.php
+++ b/lib/Relationship.php
@@ -64,7 +64,7 @@ abstract class AbstractRelationship implements InterfaceRelationship
*
* @var array
*/
- static protected $valid_association_options = array('class_name', 'class', 'foreign_key', 'conditions', 'select', 'readonly');
+ static protected $valid_association_options = array('class_name', 'class', 'foreign_key', 'conditions', 'select', 'readonly', 'namespace');
/**
* Constructs a relationship.
@@ -157,6 +157,9 @@ abstract class AbstractRelationship implements InterfaceRelationship
if (!isset($options['class_name'])) {
$class = classify($options['through'], true);
+ if (isset($this->options['namespace']) && !class_exists($class))
+ $class = $this->options['namespace'].'\\'.$class;
+
$through_table = $class::table();
} else {
$class = $options['class_name'];
@@ -283,7 +286,16 @@ abstract class AbstractRelationship implements InterfaceRelationship
protected function set_class_name($class_name)
{
- $reflection = Reflections::instance()->add($class_name)->get($class_name);
+ try {
+ $reflection = Reflections::instance()->add($class_name)->get($class_name);
+ } catch (\ReflectionException $e) {
+ if (isset($this->options['namespace'])) {
+ $class_name = $this->options['namespace'].'\\'.$class_name;
+ $reflection = Reflections::instance()->add($class_name)->get($class_name);
+ } else {
+ throw $e;
+ }
+ }
if (!$reflection->isSubClassOf('ActiveRecord\\Model'))
throw new RelationshipException("'$class_name' must extend from ActiveRecord\\Model");
diff --git a/lib/Table.php b/lib/Table.php
index c6ce57a..cd3477c 100644
--- a/lib/Table.php
+++ b/lib/Table.php
@@ -457,6 +457,7 @@ class Table
private function set_associations()
{
require_once 'Relationship.php';
+ $namespace = $this->class->getNamespaceName();
foreach ($this->class->getStaticProperties() as $name => $definitions)
{
@@ -466,6 +467,7 @@ class Table
foreach (wrap_strings_in_arrays($definitions) as $definition)
{
$relationship = null;
+ $definition += compact('namespace');
switch ($name)
{
diff --git a/test/helpers/foo.php b/test/helpers/foo.php
index 4c14897..1ed77ca 100644
--- a/test/helpers/foo.php
+++ b/test/helpers/foo.php
@@ -4,27 +4,25 @@ namespace foo\bar\biz;
class User extends \ActiveRecord\Model {
static $has_many = array(
- array('user_newsletters', 'class_name' => '\foo\bar\biz\UserNewsletter'),
- array('newsletters', 'class_name' => '\foo\bar\biz\Newsletter',
- 'through' => 'user_newsletters')
+ array('user_newsletters'),
+ array('newsletters', 'through' => 'user_newsletters')
);
}
class Newsletter extends \ActiveRecord\Model {
static $has_many = array(
- array('user_newsletters', 'class_name' => '\foo\bar\biz\UserNewsletter'),
- array('users', 'class_name' => '\foo\bar\biz\User',
- 'through' => 'user_newsletters')
+ array('user_newsletters'),
+ array('users', 'through' => 'user_newsletters'),
);
}
class UserNewsletter extends \ActiveRecord\Model {
static $belong_to = array(
- array('user', 'class_name' => '\foo\bar\biz\User'),
- array('newsletter', 'class_name' => '\foo\bar\biz\Newsletter'),
+ array('user'),
+ array('newsletter'),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment