Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [models]

The tag has no usage guidance.

1
vote
1answer
13 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
1answer
102 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 ...
3
votes
1answer
39 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
1answer
39 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
1answer
152 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
1answer
158 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 "...
1
vote
1answer
94 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()->...
2
votes
0answers
418 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
2answers
107 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
1answer
123 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
2answers
20 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
0answers
42 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
1answer
165 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
1answer
342 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. ...
4
votes
1answer
687 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
1answer
421 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
2answers
125 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
1answer
242 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 ...
4
votes
1answer
377 views

Craft Commerce: Custom address model validations

How/Where/Can I add more address model validations than just "firstName" and "lastName"...?
0
votes
1answer
329 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
1answer
213 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 ...
1
vote
1answer
177 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
1answer
135 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: ...
2
votes
1answer
512 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
1answer
69 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 ...
5
votes
1answer
736 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
1answer
293 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 ...
0
votes
1answer
601 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
1answer
314 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 %} ...
1
vote
1answer
394 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 ...
1
vote
1answer
321 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 ...
7
votes
1answer
964 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
1answer
310 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 ...
1
vote
1answer
251 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 ...
0
votes
1answer
425 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 ...
1
vote
3answers
543 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( ...
3
votes
1answer
252 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 ...
3
votes
1answer
872 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
1answer
1k 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
2answers
1k 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
1answer
205 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
1answer
1k 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 "...
2
votes
1answer
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 ...
8
votes
2answers
1k 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 ...
5
votes
1answer
232 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 ...
3
votes
2answers
655 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
1answer
182 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 ?
1
vote
1answer
140 views

Do I need a model when I have a record?

I have a record so I can save my data to the DB but do I need a model for services and stuff ? Or can the record do that too ?
1
vote
1answer
142 views

When should I build upon existing models in my own plugins?

In the Craft Models documentation example the model extends the BaseModel class. However in the example Events plugin source code the Events_EventModel extends the BaseElementModel. While poking ...
6
votes
1answer
580 views

Allow anonymous submission on a custom element type?

I have a custom element type that I want to allow anonymous submission from the front-end. I have a solid concept on how the form data is passed but I seem to be missing something. I have the ...