Skip to main content

Questions tagged [models]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
159 views

How can I best add additional fields to the Address form during the checkout

I want my customers to enter their date of birth and place of birth during the checkout. Do I have to overwrite the Address commerce model somehow? (\craft\commerce\models\Address). Something else I ...
Rayco's user avatar
  • 11
2 votes
1 answer
151 views

::model(), ::populateModel() and ::populateModels() in Craft 3

I'm attempting to port a custom plugin that was originally written for Craft 2. It has a fair bit of logic, controllers, services, records, models etc. The records and models which I admittedly don't ...
James White's user avatar
  • 1,171
3 votes
1 answer
194 views

Override entry.url / entry.getUrl() Method

For certain entries I would like to be able to override how Craft generates the URLs to them. So I create an Entry Model in my module extending from \craft\elements\Entry and override the getUrl() ...
Matthias Redl-Mann's user avatar
1 vote
1 answer
40 views

How to get instance of DiscountModel in Commerce 2?

In Commerce i got an instance of DiscountModel Class as follows: $discount = new Commerce_DiscountModel(); Is this the right way in Commerce 2? use craft\commerce\models\Discount as DiscountModel; ....
phaetons's user avatar
  • 705
1 vote
1 answer
484 views

What's the difference between a Component and a Model in Craft 3?

I'm trying to familiarize myself with the Craft architecture and I can't understand whether I should be extending craft\base\Component or craft\base\Model for my business logic models. Component seems ...
Lazlo's user avatar
  • 185
2 votes
1 answer
214 views

Model values assignment issue

I am setting attributes on my record using a models' attributes (that has validated successfully). I cannot figure out why the values aren't assigned. I can manually assign each one but that's ...
Shift2Design's user avatar
1 vote
1 answer
213 views

How to detect the model type of an item in an array of query results

When querying multiple model types (entries, Commerce products and Commerce variants) I want to be able to detect the model type of each item as I iterate over the results array. Ideally to avoid ...
amogower's user avatar
  • 113
1 vote
1 answer
349 views

Saving Form Data to Plugin Record via Javascript

I am new to Craft plugin development, so I apologize ahead of time for my lack of understanding. I'm working on a plugin that allows the user to "upload" Excel/csv files via the control panel that ...
user3285578's user avatar
1 vote
1 answer
230 views

AttributeType::Number giving validation error "Must be an integer"

So I have a model attribute attributeName with an AttributeType::Number I haven't implemented any personal validation rules yet, however when I run: model->validate(), its giving me a "...
Paul Kirby's user avatar
1 vote
1 answer
142 views

no element type exists with getCriteria and custom element type

I am working on a plugin using the getCriteria function, but for every Element Type I put, I always get an error: No element type exists by the type “Comerce_Group”. $criteria = craft()->...
Abraham's user avatar
  • 11
2 votes
0 answers
785 views

Custom plugin - cannot save record

I have a service which am passing a model and an identifier (optional) to. The model contains new data and the aim is that if an existing record exists then we merge the two responses into a ...
Cole Henley's user avatar
  • 1,741
2 votes
2 answers
166 views

Plugin development - Querying related data

I am working on a Craft plugin. I have a main entities are 'Courses' and 'Entries'. If I click on an Entries in the CMS there is an option to select and associated 'Course'. In my controller I can ...
Alan A's user avatar
  • 131
1 vote
1 answer
412 views

Is it possible to return data/models from controller to template via setRouteVariables?

I'm trying to write a plugin that lives in the admin interface, that reads external data (a cinema program), parses it, and creates entries based on this (movie info and screening times). While doing ...
KSP's user avatar
  • 849
0 votes
2 answers
21 views

Model public method throws CMapIterator warning

I have a strange problem with the following public method on a model. public function getAttributes() { return $this->getId() . $this->getStyle() . $this->getDataAttributes(); } ...
Fyrebase's user avatar
  • 735
1 vote
0 answers
58 views

Filter conditions on related UserModel in custom Record

My plugin has a custom ResponseRecord with relations to a UserRecord and ElementRecord as follows: public function defineRelations() { return array( 'user' => array(static::BELONGS_TO,...
Frank's user avatar
  • 153
3 votes
2 answers
552 views

How to duplicate an element type model with content?

Duplicating models is easy if you do it from the edit view, where you can just omit the ID and pass it along to save(). But how would I go about doing that programmatically without having a POST ...
Gustavs's user avatar
  • 406
2 votes
1 answer
508 views

Getting related models in templates?

I recently started learning plugin development for Craft CMS. I'm creating a plugin that deals with video content. For this example I'll use two models: MyPlugin_ChannelModel and MyPlugin_VideoModel. ...
Nelis86's user avatar
  • 23
3 votes
1 answer
953 views

Model/Record AttributeType documentation?

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, ...
Gavin Anderegg's user avatar
3 votes
1 answer
491 views

How do I write custom model validators for my plugin that refer to other fields?

I'm looking at this guide to writing custom validators: https://experiencehq.net/blog/the-definitive-guide-to-validation-in-craft-part-5-custom-validators However, this only allows code that refers ...
danieltalsky's user avatar
1 vote
2 answers
956 views

Validating Date of Birth format from a form field value

I'm working a on website project which includes a web form which asks for various personal details, one of the fields on the form is Date of Birth. The form is being validated by a custom plugin ...
James White's user avatar
  • 1,171
2 votes
1 answer
597 views

Override a model method via plugin

I'm new to Craft and PHP, so thanks ahead of time! Is it possible to override a model method with a plugin? For example, I want to add some additional functionality to the getUrl method of the ...
kroppian's user avatar
3 votes
1 answer
495 views

Craft Commerce: Custom address model validations

How/Where/Can I add more address model validations than just "firstName" and "lastName"...?
R0wan's user avatar
  • 335
0 votes
1 answer
600 views

Plugin Use Default Value When Empty

I've got a default value defined in my model for a field 'limit' => array(AttributeType::Number, 'default' => 0) When I create a new record, if the limit field is empty, NULL gets stored in ...
Trevor Davis's user avatar
1 vote
1 answer
288 views

Getting UTC DateTime from Model attributes

The AttributeType::DateTime in model attributes saves as UTC which is great, but upon getting that attribute I receive a Craft\DateTime object with my server timezone set for it, and thus the time ...
Gustavs's user avatar
  • 406
1 vote
1 answer
257 views

Using $model->validate() with AttributeType::Url

I have a basic model defined: protected function defineAttributes() { return array( 'id' => array(AttributeType::String, 'required' => true), 'url' => ...
taylor's user avatar
  • 1,128
2 votes
1 answer
235 views

More extensive documentation for defining models for plugins

I'm trying to get a better understanding of how to define new models in plugins, but the documentation at http://buildwithcraft.com/docs/plugins/models doesn't get me very far. For example, there is: ...
John O's user avatar
  • 391
2 votes
1 answer
707 views

How to add to arrays in models?

I have a model that contains a bunch of storage arrays where I collect results from various services I have written. class Shipper_ResultsModel extends BaseModel { shipping categories for debugging ...
Jeremy Daalder's user avatar
0 votes
1 answer
102 views

When to set Model properties

I was just wondering when it's recommended to set the attributes on a custom model? Would it be ok to set them within the model's constructor or not? I guess it depends on when defineAttributes() is ...
user avatar
5 votes
1 answer
821 views

What attribute type should I set in the record or model to create a TEXT type in the database?

I am having a problem with record and model attribute types. I have a frontend user form with some textfields which require a TEXT type field in the database to hold the data. So far I have tried ...
Lettie's user avatar
  • 2,033
0 votes
1 answer
494 views

Error with model validation

Hi I'm getting this error when I try to validate a model. Craft\mymodelModel and its behaviors do not have a method or closure named "getTableSchema". I'm using the following code in the controller ...
Lettie's user avatar
  • 2,033
0 votes
1 answer
855 views

setAttribute Issue

I have a controller function creating attributes for a model from posted data and sending them to a service to save the data. Everything seems to be working correctly if I do a print_r in the service ...
Lettie's user avatar
  • 2,033
1 vote
1 answer
343 views

Getting an entry's tags

I'm trying to find a way to get tag data from a Model. Currently, I have a template that makes use of tags in an entry like this: Codes: {% for tag in entry.codes %} {{ tag.title }}; {% endfor %} ...
charmeleon's user avatar
1 vote
1 answer
451 views

Is there a good way of setting a model attribute to be an instance of another model?

I have an API call to make that needs a particular format of data shown below. The person and addresses attributes are currently set as Mixed AttributeTypes and I'll need to create a manual validation ...
Russ Back's user avatar
  • 1,503
1 vote
1 answer
442 views

Is there a way to define an attribute setter method on a model?

I want to ensure that any attempt to set a date attribute on a model by way of populateModel() or myModel->foo = 'bar' (basically any method of setting a value) is intercepted so I can set a default ...
Russ Back's user avatar
  • 1,503
8 votes
1 answer
1k views

Query table in a different database

I'm writing another plugin for my craft app, this time I'm attempting to reuse a MySQL table that's used by other applications as well. I've a class extending the BaseRecord, and getTableName() will ...
charmeleon's user avatar
2 votes
1 answer
440 views

How do I validate an array model attribute against a set of possible values?

I have a need to set a property on a plugin Model that is an array of one or more valid values. I can see that I can use AttributeType::Enum and record and define a set of values but if I'm not ...
Russ Back's user avatar
  • 1,503
1 vote
1 answer
436 views

When using Active Record with a View (instead of Table) the primary key is not set in the record after save

I ran into a problem when saving a new record. I have created a test below. When saving a record using a db table, the record is saved to the db and the primary key id is updated in the record ...
user3372's user avatar
0 votes
1 answer
494 views

AttributeType::String is NULL in my database

I have create a plugin to save information from my form in the front-end. I can create a row with some data. Email and Number type work well but String is NULL Have you any idea ? my controller ...
theatus's user avatar
  • 37
1 vote
3 answers
746 views

Customise errors when validating model data?

I’m trying to set a custom message for a specific model attribute. I have something like this: protected function defineAttributes() { return array( 'attributeOne' => array( ...
James's user avatar
  • 1,138
3 votes
1 answer
293 views

Can a model or record have conditional requirements?

I have a form that spans two pages, with a required field on both pages and an optional field on both pages: Form A - Page 1 - Question 1 (required) - Question 2 (optional) [User submits partial ...
Ben Parizek's user avatar
  • 13.5k
4 votes
1 answer
1k views

Models v Records - confused

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'...
Russ Back's user avatar
  • 1,503
2 votes
1 answer
2k views

How can I search for entries that are enabled OR disabled?

Within a plugin I am fetching existing entries using the CriteriaModel class as documented here http://buildwithcraft.com/docs/plugins/working-with-elements#fetching-elements, specifically like this: ...
Russ Back's user avatar
  • 1,503
5 votes
2 answers
2k views

Populate a record with a model?

In a follow up to my earlier question, I'm focusing on cleaning up my code to keep it incredibly simple. I've moved on from the Controller and Model to the Model and Service. Typically you would have ...
Jason McCallister's user avatar
1 vote
1 answer
335 views

MVC - Should these be seperate controllers/models etc

I hope this is the right place to post this, it's more a question on MVC for plugin development. I'm developing a plugin to extend SproutForms to add users to Campaign Monitor lists. So far I've got ...
KeironLowe's user avatar
5 votes
1 answer
2k views

How to return errors from a model back to the template?

I'm working on validation with Craft. I have a custom plugin that allows public submission. However, I want to make sure I can pass errors back to the template. The key is trying to make my code as "...
Jason McCallister's user avatar
3 votes
1 answer
1k views

How can I have a dynamic/virtual attribute AND have as an attribute of the Model?

Take the example of a model with a firstName and lastName. I know I can add a getFullName method to the model, and $model->fullName will run through the getter. However, I'd like the attribute to ...
Tim Kelty's user avatar
  • 3,081
8 votes
2 answers
2k views

How do I get model/record attributes in their proper types, and not just strings?

Here's my record (model is nearly identical): namespace Craft; class Recurly_PlanRecord extends BaseRecord { public function getTableName() { return 'recurly_plans'; } protected ...
Tim Kelty's user avatar
  • 3,081
5 votes
1 answer
371 views

Content Modeling for Site Architecture Recommendations

I am doing some content modeling for a site with over 100 pages with a pretty deep tree. The site has a lot of what I refer to as "General Pages" as well as a collection of channel type content in the ...
SethG's user avatar
  • 101
3 votes
2 answers
962 views

Plugin autoload classes

im developing a forms plugin and i'm having some trouble getting my plugin model class autoloaded. To make development easier i'm implementing the Yii way of rendering forms(http://www.yiiframework....
Sjoerd Adema's user avatar
1 vote
1 answer
198 views

From AttributeType::Enum in Model to a matching selectField?

So I have a model with an Enum and I want a selectField for that Enum. Is there a DRY way to do that ? And if yes does it work with the translation-filter ?
maxx's user avatar
  • 443