3

I'm building a plugin which creates a few tables, and need to store some fairly robust data. I've done this before and previously gotten by with mostly AttributeType::String and AttributeType::Mixed, but this time I'd like to ensure I'm doing things by the book. Problem is: so far as I can tell, nobody's written the book.

I started out by Model and Record documentation, but they don't reference the possible AttributeType options. Next I read the class reference, but this isn't helpful either. Then I looked at the ModelHelper and DbHelper code. These were a bit helpful, but they don't explain how exactly an AttributeType affects throughout Craft. Eventually I found Experience's Definitive Guide to Validation in Craft series (specifically the linked AttributeType entry). This is quite helpful, but because it's focused mostly on validation, it doesn't answer many of my questions.

It's possible that the book by Experience answers gives more detail, but I'm surprised I need a book to learn about this. It also seems like a slight gamble, as I'm only potentially interested in one chapter right now.

Here's the sort of thing I'm hoping to learn:

  • What is the Name type used for?
  • What's the difference between Handle and Slug? Why choose one over the other?
  • What exactly does Template do, and why does it need its own type?
  • What's the difference between Url and Uri? Is it just the validation?

Maybe it's not important to know these things, but I think it would be helpful to understand what I'm doing and how these types are processed and affect the system. It's also not completely clear what types are available, as the ModelHelper source doesn't list types like String. I assume that's getting inherited from Yii, but I'm not certain.

Question: Is there some documentation, or blog post, out there that explains the types available through AttributeType?

2

Problem is: so far as I can tell, nobody's written the book.

That's partly our fault... we've never really documented the Attribute parts of Models and Records. And it probably won't happen now since the majority of that has been stripped out of the upcoming Craft 3 making it much easier to follow Yii 2 conventions (which happens to be way more documented).

What is the Name type used for?

In the context of a Record, it's a shortcut for saying you're going to get a varchar datatype set to 255 chars. In the context of a Model, it'll just enforce the 255 character limit.

What's the difference between Handle and Slug? Why choose one over the other?

A handle is a varchar(255) that has a few pre-defined reserve words. id, dateCreated, dateUpdated, uid, title. Currently used in Craft for defining the handles that things like Asset Sources, Global Sets, Asset Transforms, etc. uses.

There isn't a AttributeType::Slug.

What exactly does Template do, and why does it need its own type?

It's just a shortcut for a varchar(500), currently used in Craft when defining a template path for Category and Section settings.

What's the difference between Url and Uri? Is it just the validation?

Semantically, a URL is a subset of a URI: https://danielmiessler.com/study/url-uri/

In the context of Craft, yeah - the only practical difference is the validation.

  • 1
    Good to hear that Yii 2/Craft 3 have this issue licked! Thanks for the response. I've ended up getting most of my questions answered by trolling through the source, but this was a bit of a pain. Honestly, I just ended up using Enums aside from Strings and Mixed types this time around, but it was puzzling. Thanks! – Gavin Anderegg Jul 6 '16 at 0:35

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.