Skip to main content

Questions tagged [models]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
167 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 ...
2 votes
1 answer
152 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 ...
3 votes
2 answers
570 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 ...
3 votes
1 answer
196 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() ...
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: ...
1 vote
1 answer
41 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; ....
1 vote
1 answer
487 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 ...
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()->...
1 vote
2 answers
979 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 ...
2 votes
1 answer
600 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 ...
2 votes
1 answer
216 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 ...
1 vote
1 answer
221 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 ...
1 vote
1 answer
353 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 ...
1 vote
1 answer
238 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 "...
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 ...
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 ...
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 ...
1 vote
1 answer
415 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 ...
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(); } ...
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,...
2 votes
1 answer
515 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. ...
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"...?
3 votes
1 answer
955 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, ...
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 ...
1 vote
1 answer
289 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 ...
0 votes
1 answer
601 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 ...
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( ...
1 vote
1 answer
258 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' => ...
2 votes
1 answer
442 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 ...
2 votes
1 answer
710 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 ...
0 votes
1 answer
103 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 ...
1 vote
1 answer
452 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 ...
0 votes
1 answer
495 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 ...
0 votes
1 answer
496 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 ...
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 ...
0 votes
1 answer
860 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 ...
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 %} ...
3 votes
2 answers
965 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....
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 ...
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 ...
3 votes
1 answer
295 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 ...
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'...
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: ...
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 ...
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 ...
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 "...
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 ...
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 ...
5 votes
1 answer
373 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 ...
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 ?