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 ...
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 ...
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,...
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 ...
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 ...
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 ...
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.
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 ...
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 ...
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 ...
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 ...
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://...
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 ...
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.
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 ...
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.
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
{
/**
* @...
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 "...
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-...
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 ...
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 ...
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 ...
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.
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 ...
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 ...
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.
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();
//...
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 ...
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()->...
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',...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
records × 48plugin-development × 30
models × 17
database × 12
plugins × 5
relations × 5
attribute × 4
validation × 3
craft3 × 2
templating × 2
custom-field × 2
queries × 2
categories × 1
users × 1
config × 1
elementcriteriamodel × 1
events × 1
craft2 × 1
migration × 1
javascript × 1
mysql × 1
installation × 1
json × 1
updates × 1
sorting × 1