Hot answers tagged

14

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 this will help someone else.


8

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 a plugin that instantiates its own DbConnection, though and uses Query Builder instead of Active Record. Completely untested, but something like this: $...


6

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, then populate a model with it to pass that to a view, for instance. If everything works as it should, a record is never used outside the service.


5

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 into the method: public function onAfterInstall() { $record = new MyPlugin_ExampleRecord; $record->setAttribute('attributeName', 'value'); $...


5

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 ids in your custom order then the following code should allow you to then fetch entries in that order: $criteria = craft()->elements->getCriteria(...


5

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 override the createTable() method in your record class and add the foreign key constraint manually using DbCommand: public function createTable() { parent::...


4

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

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 populate a Mailer_LogModel with just the attributes you want to expose to your template and return that. Pretty sure after you do that, a simple: {% if log....


3

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 the rules() method and you can conditionally set what validations methods you want depending on the step you're on. Presumably, you'd want to pass in a step ...


3

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 this will introduce an almost negligible performance improvement, but it really depends on how many entries you are returning. The following code should give ...


3

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://craftcookbook.net/recipes/324


3

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 AttributeType::Mixed. All other attribute types will be returned directly as the database returned them (as a string). However, I agree with you that we ...


3

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.


3

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 can't just insert 10k elements in one request. There is a function App::maxPowerCaptain() to disable the timeout but I still faced overflow errors. And of ...


3

It means that no combination of those two columns can be the same in the table stated in the getTableName() of your record.


3

/** * Class SearchRelationship record * * @property int $id * @property int $aaa * @property int $bbb * @property string $ccc */ class SearchRelationship extends ActiveRecord { /** * @inheritdoc * @return string */ public static function tableName(): string { return '{{%somename}}'; } } All you really need to ...


2

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 "Safe" Validation Rules , for details on how you can specify what attributes are safe to be massively assigned. This will allow you to protect against unwanted ...


2

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-assigning untrusted inputs to models/records that participate in the Active Record pattern (like Craft/Yii does). They can be susceptible to what's known as the ...


2

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 for you. If you're using raw SQL, then you'll have to keep track of it yourself.


2

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 be better off going with the method in the answer you linked to and use QueryBuilder.


2

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 Trackr_TraceRecord::model() is really weird. When you call ::model() you’re getting a “meta” version of your record class, which should only be used to call things ...


2

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 since the majority of that has been stripped out of the upcoming Craft 3 making it much easier to follow Yii 2 conventions (which happens to be way more documented)....


2

This should be fixed in the next (post 2.6.2791) release so that plugins can define composite primary keys in their records.


2

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 returned, not an Array and so falls over on the isset call. $pk = $this->primaryKey(); if (isset($columns[$pk])) { $columns[$pk]['primaryKey'] = true; $...


2

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 plugin, you can write a migration to keep existing installations updated with the latest schema. Craft automatically checks for new migrations whenever a plugin’s ...


2

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.


2

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(); // Change the PK auto-increment starting point. craft()->db->createCommand()->setText('ALTER TABLE myTable AUTO_INCREMENT=9999')->execute(); }


2

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 letter after the underscore. So it seems like an "undocumented feature" that you can make plugin tables persist after uninstall by including a capital letter ...


2

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()->myPluginVideoService->getVideosByChannelId($this->id); } Then your plugin's video service would have: public function getVideosByChannelId($channelId) { // Can memoize ...


2

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', 'allowEmpty' => false, 'attributeName' => 'id', 'className' => 'Craft\Events_EventTypeRecord', 'message' => 'You must ...


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