2

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()

2

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";
$record->save();

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:

$model->getAuthor();
| improve this answer | |
  • Yeah setting the id directly works. I thought it didn't when I first tried it but it does. – Leevi Graham Nov 6 '14 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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