14
votes
What attribute type should I set in the record or model to create a TEXT type in the database?
Ok worked out a solution to the attributes. You can set them like this:
array(AttributeType::String, 'column' => ColumnType::Text, 'required' => false)
By defining the column, so hopefully ...
8
votes
Accepted
Query table in a different database
Have you tried it? I'm pretty sure that's not going to work as Active Record is tied to the db connection in craft/config/db.php hanging off of craft()->db.
You should be able to pull it off with ...
6
votes
Accepted
Models v Records - confused
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,...
5
votes
Accepted
How to return errors from a model back to the template?
If you use $model->getErrors() instead, it should return a multidimensional array of errors indexed by attribute name like so:
array(
'email' => array(
'First email error.',
...
5
votes
Accepted
What's the difference between a Component and a Model in Craft 3?
Models are classes that contains abstractions of the reality, for example you could have the model car that contains the technical information about it, how many wheels it has and it's brand. Models ...
4
votes
Do I need a model when I have a record?
Records can do that too (where that = hold data), but, according to Craft docs, Records should never ever leave Service layer. So, yes, you do need a model too.
4
votes
How do I validate an array model attribute against a set of possible values?
Found an answer in the form of a customer validator class on the model:
https://experiencehq.net/blog/the-definitive-guide-to-validation-in-craft-part-5-custom-validators#newsletter
So something ...
4
votes
Accepted
Craft Commerce: Custom address model validations
You can do either or both of:
Use javascript validation on the front end (with e.g. parsley)
Create a plugin with a method that is registered as a listener on this event:
https://craftcommerce.com/...
3
votes
Can a model or record have conditional requirements?
I can think of a couple of options and I'll just throw them all out there.
Have a separate model per partial form and each model performs its own validation.
Have a single model, but have it override ...
3
votes
MVC - Should these be seperate controllers/models etc
Well, it looks like Sprout Forms itself prefers separate services for everything (they'd be some big services if they weren't,) but Craft does bundle closely-related things together sometimes - ...
3
votes
Accepted
Allow anonymous submission on a custom element type?
Ok, so I finally figured it out. The trick was using the following two methods:
assembleLayoutFromPost()
setContentFromPost('fields')
The controller now looks like this:
public function ...
3
votes
Accepted
How can I search for entries that are enabled OR disabled?
I haven't tried this in php, but on the 'template' side there is an attribute called 'status' which you might try. I assume it would work in php as well.
$existing = craft()->elements->...
3
votes
How do I get model/record attributes in their proper types, and not just strings?
I ended up overriding BaseModel->setAttribute() and BaseRecord->prepAttributesForUse() in my plugin.
Works well! Right now I'm just handling Bool and Number types.
Posted this solution here: http://...
3
votes
Accepted
How do I get model/record attributes in their proper types, and not just strings?
Currently the only thing Craft does in BaseModel->setAttribute() and BaseRecord->prepAttributesForUse() is some normalization when the attribute in either case happens to be AttributeType::DateTime or ...
3
votes
Accepted
Content Modeling for Site Architecture Recommendations
It sounds like you have 3 different general buckets of entries to consider:
Streams of similar entries (News section, blogs, etc.)
General content pages (100+ of them)
Unique/one-off pages (Contact ...
3
votes
Accepted
Override a model method via plugin
Kind of, but probably not what you're looking for.
Your plugin can certainly do a MyAssetFileModel extends AssetFileModel and (in the scope of your plugin) take advantage of existing AssetFileModel ...
3
votes
Accepted
How to duplicate an element type model with content?
I'm going to look pretty dumb if somebody drops a one-line answer down the line, but here goes: AFAIK, unfortunately, "cloning" an element (e.g. an EntryModel) isn't that simple.
For one, $model->...
3
votes
Accepted
Model values assignment issue
There is a second parameter in your setAttributes() function. This parameter is true per default and checks if the attribute is safe so you either have to declare all safe attributes in your records ...
2
votes
When should I build upon existing models in my own plugins?
BaseComponentModel, BaseElementModel and BaseEntryRevisionModel all ultimately end up extending BaseModel, which itself extends Yii's CModel.
I like to think of them, roughly, as beefed up Data ...
2
votes
Plugin autoload classes
Got it working now, namespace issue :s. The autoloaded classes are in the Craft namespace and i didnt prepend this to the dynamicly generated class names. Time to take a break...
2
votes
Populate a record with a model?
Yii's setAttributes method is probably a better way to do this than the populateRecords method, as by default assignments will only be made to safe attributes.
See this wiki article, Understanding "...
2
votes
Accepted
Populate a record with a model?
Yii does have the equivalent populateRecord and populateRecords on CActiveRecord.
However, on both the populateModel(s) and populateRecord(s) methods, you should be very cautious of blindly mass-...
2
votes
Customise errors when validating model data?
This might be similar to Brad Bell's recommendation, but if you are using Yii's built-in validation rules, you should just override the rules() method in your model or record, specify the rule there, ...
2
votes
Customise errors when validating model data?
In addition to Brad’s answer (which is what I was looking for), you could also use template-specific messages using conditionals if for some reason you only needed to override the global error ...
2
votes
Accepted
Getting an entry's tags
If you have $entry, you should be able to get the tags by the field name, (codes, in your case).
$tags = $entry->codes;
foreach($tags as $tag)
{
echo "<li>".$tag->title."</li>";...
2
votes
Accepted
Is there a good way of setting a model attribute to be an instance of another model?
In your Micros_GuestRegistrationRequestModel's attribute definition, if you define person like so:
'person' => array(AttributeType::Mixed, 'model' => 'Micros_GuestPersonModel');
Craft should ...
2
votes
Accepted
Is there a way to define an attribute setter method on a model?
In your model you can override setAttribute(), which covers calling setAttribute('foo', 'bar') directly as well as $model->foo = 'bar'.
public function setAttribute($name, $value)
{
if ($name =...
2
votes
Accepted
How to add to arrays in models?
I'm pretty sure 'default'=>[] isn't doing anything so it's probably not necessary.
If you wanted to force a single attribute to be of a specific model, you could use:
'courierShippingItems' =>...
2
votes
Accepted
How do I write custom model validators for my plugin that refer to other fields?
Looks like I can just refer to the other field name:
public function validateUsername($attribute)
{
$value = $this->$attribute;
$otherFieldsValue = $this->otherField;
if ($value &...
2
votes
Accepted
Model/Record AttributeType documentation?
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
models × 53plugin-development × 34
records × 17
attribute × 10
validation × 8
database × 5
plugin-craftcommerce × 3
relations × 3
controller × 3
services × 3
craft3 × 2
templating × 2
plugins × 2
datetime × 2
elementcriteriamodel × 2
custom-field × 2
elementtype × 2
control-panel × 1
search × 1
entry × 1
form × 1
php × 1
config × 1
tags × 1
craft2 × 1