Is it possible to just assign the ID to the owning side of a relationship rather than querying the DB, loading the record and assigning it?

Something similar to Doctrines _getReference()

1 Answer 1


I think that's actually the way to do it, assign the id to the owning record:

$record = new Cocktails_CocktailRecord;
$record->authorId = 10;
$record->title = "Gin Tonic";

When getting the record from the db, the record is turned into a model, at this point, records and models are only dealing with authorId, not the whole record. Your model will then have to define a getAuthor() method that will get the author user for you:

public function getAuthor()
    if ($this->authorId) {
        return craft()->users->getUserById($this->authorId);

This way your model only loads the relationship when you actually use it:

  • Yeah setting the id directly works. I thought it didn't when I first tried it but it does. Commented Nov 6, 2014 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.