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...
Asked
Active
Viewed 1,424 times
4
-
There is a good explanation of models vs. records in Craft here: http://craftcms.stackexchange.com/a/2973/57 – Brad Bell Dec 18 '14 at 02: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 09:53
1 Answers
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.
Paul
- 6,338
- 12
- 26
-
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 06:45
-
How does this work when updating an existing record? https://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 01 '15 at 14:15