4

If a Model can be used to save entries, what is a Record for? Is it literally just to define the schema? I've seen a few examples where the defineAttributes methods have duplication of code too, so I'm a little confused...

  • There is a good explanation of models vs. records in Craft here: craftcms.stackexchange.com/a/2973/57 – Brad Bell Dec 18 '14 at 2:53
  • Thanks, Brad. I'd like to echo one of the comments on that article in that it would be great if this detail was in the docs. – Russ Back Dec 18 '14 at 9:53
6

I might be wrong here, but I don't think a model can be used to save entries. You populate a model, then populate a record with that model and save it. Once saved, you get data from the db as a record, then populate a model with it to pass that to a view, for instance.

If everything works as it should, a record is never used outside the service.

  • Nailed it. In rare instances, you might modify a record's properties just before saving (or just after retrieving). But if that data ever leaves your service layer, it should do so as a model. – Lindsey D Dec 18 '14 at 6:45
  • How does this work when updating an existing record? gist.github.com/russback/f6d96809a459e3872363 shows how I'm currently doing it but it feels a little odd to have to declare a model just to access the allErrors property (it doesn't exist on the BaseRecord class. – Russ Back Dec 18 '14 at 14:56
  • The underlying CActiveRecord class contains hasErrors and getErrors methods. – Jay May 1 '15 at 14:15

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.