Created
December 8, 2013 11:13
-
-
Save kszucs/7856016 to your computer and use it in GitHub Desktop.
Lithium autoload model relationships to entity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\extensions\data\entity; | |
class Document extends \lithium\data\entity\Document { | |
public function &__get($name) { | |
if ($result = parent::__get($name)) { | |
return $result; | |
} | |
if (($model = $this->_model) && ($rel = $model::relations($name))) { | |
$this->_relationships[$name] = $rel->get($this); | |
return $this->_relationships[$name]; | |
} | |
return $result; | |
} | |
} | |
//connections.php | |
Connections::add('default', [ | |
'type' => 'MongoDb', | |
'host' => 'localhost', | |
'database' => 'database', | |
'classes' => [ | |
'entity' => 'app\extensions\data\entity\Document' | |
] | |
]); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, for simple lazy-loading that's pretty clean. I dunno if I'd recommend it for anything serious, since this style makes it very easy to run lots of queries in a loop.