Hot answers tagged

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 ...
user avatar
  • 2,023
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 ...
user avatar
  • 66.4k
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,...
user avatar
  • 6,288
5 votes

How can I populate some records after my plugin is installed?

On the main MyPluginPlugin class, there is a method you can override called onAfterInstall(). Which will run after your plugin is installed. To create those records, you can place the following code ...
user avatar
5 votes
Accepted

How can I expand on a normal Craft query to sort by a column in a custom table?

You can't do this using the craft.entries tag in twig. You'll need to create a new function in your plugin that returns the entries in your custom order. Assuming you can create an array of element ...
user avatar
  • 6,226
5 votes
Accepted

Foreign Key column pointing to `craft_relations` table

I might be wrong, but I don't believe you'll be able to do that within the defineRelations() method – Craft needs a record class for the table you want to create a relation with. A workaround is to ...
user avatar
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.
user avatar
4 votes
Accepted

How to check if a DateTime attribute is set?

You shouldn't be returning a record instance to your templates directly. That can lead to all sorts of badness like templates being able to update the database themselves. Have your plugin's service ...
user avatar
  • 66.4k
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 ...
user avatar
  • 66.4k
3 votes
Accepted

Plugin: Making a record's dateUpdated column update to CURRENT_TIMESTAMP

How are you inserting the data? If you're inserting data through any of Craft's Active Record or DbCommand classes (craft()->db->createCommand()), then Craft will take of updating dateUpdated ...
user avatar
  • 66.4k
3 votes
Accepted

How can I join plugin data with entry data based on uri?

You could of course manually create a select query with a join that fetches entries ordered by hits descending, however you will basically be recreating what $criteria->find() does above. I imagine ...
user avatar
  • 6,226
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://...
user avatar
  • 3,061
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 ...
user avatar
  • 66.4k
3 votes
Accepted

How do I include record relationships in validation?

Yii/Active Record won't do this by default. You'd need to manually add an exists validation rule to the typeId attribute to get it to behave like you're looking for.
user avatar
  • 66.4k
3 votes
Accepted

How many users can Craft handle?

It depends more on how you are going to create and display them. I had to import 20k elements this weekend and I had to be careful to store them step by step. Depending on your server/hardware - you ...
user avatar
3 votes
Accepted

What do multiple columns of a record being set to unique mean?

It means that no combination of those two columns can be the same in the table stated in the getTableName() of your record.
user avatar
  • 3,833
3 votes

Equivalent of Records in Craft 3?

/** * Class SearchRelationship record * * @property int $id * @property int $aaa * @property int $bbb * @property string $ccc */ class SearchRelationship extends ActiveRecord { /** * @...
user avatar
  • 66.4k
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 "...
user avatar
  • 6,226
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-...
user avatar
  • 66.4k
2 votes
Accepted

Dealing with existing data in a separate database

I haven't tested, but I'm about 90% confident that Yii's CActiveRecord is directly coupled to the underlying craft()->db connection and there isn't a way to change that at runtime. You're going to ...
user avatar
  • 66.4k
2 votes
Accepted

Craft CMS Plugin Save Record to DB from Variable

I’m seeing a few issues with this: Service classes aren’t created with any arguments*, so the $traceRecord argument in your __construct() method is pointless. Setting $this->traceRecord to ...
user avatar
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 ...
user avatar
  • 66.4k
2 votes
Accepted

Is it possible to create composite Primary Keys in Records?

This should be fixed in the next (post 2.6.2791) release so that plugins can define composite primary keys in their records.
user avatar
  • 66.4k
2 votes

Is it possible to create composite Primary Keys in Records?

It would appear not unfortunately. Craft's BaseRecord (which a Record should inherit from) tries to uses the primaryKey() method call to help create the table, but assumes a single column name is ...
user avatar
2 votes

Add Record without deleting data

The Craft website has a great article on this, you can simply use migrations following this document: https://craftcms.com/docs/plugins/migrations If your schema changes over the life of your ...
user avatar
2 votes

Save multiple records at once

You could use a task within your plugin for better performance when looping through your records. Check the TasksService docs and also PowerNap, an example plugin that uses a task.
user avatar
  • 3,833
2 votes
Accepted

Define record start ID

Not the most direct way, but you could do something like this from your plugin's record: public function createTable() { // Let the base class do the actual work parent::createTable(); //...
user avatar
  • 66.4k
2 votes
Accepted

Plugin uninstall only removes one table

Thanks for that tip, Lindsey, but here's what happened: I changed calendar37 to calendar37_Occurrences and then all three tables wouldn't uninstall. It turns out that the issue is having a capital ...
user avatar
  • 1,263
2 votes
Accepted

Getting related models in templates?

I would move that logic out of your model class and into a service method. Your model could still have a thin wrapper for it: public function getVideos() { return craft()->...
user avatar
  • 66.4k
2 votes

How do I include record relationships in validation?

Thanks to Brad for pointing me in the right direction. I added this rules method to my record. public function rules() { $rules = parent::rules(); $rules[] = [ 'typeId', 'exist',...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible