Skip to content

Instantly share code, notes, and snippets.

@kszucs
Created December 8, 2013 11:13
Show Gist options
  • Save kszucs/7856016 to your computer and use it in GitHub Desktop.
Save kszucs/7856016 to your computer and use it in GitHub Desktop.
Lithium autoload model relationships to entity
<?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'
]
]);
@nateabele
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment