4

I'm building a plugin where each Element created has a unique content table associated with it. Each individual Element behaves as needed, however, when I try to create an index page that returns all of the records of this Element Type, Craft seems to assume that I'm using the default content table unless I tell it differently in the getContentTableForElementsQuery() method.

However, the results that are being returned have Elements with multiple content tables.

Am I overlooking anything here or is there a different way I should be thinking about this?

3
+50

You can only return a single content table per element query.

If you need to join multiple content tables, you have two options:

  1. Use multiple element queries, one for each group of elements that share a content table. Disadvantage of this approach is, you won't be able to order elements in SQL.

  2. Just don’t join in any content tables in the initial elements query, and let your elements lazy-load their content rows if any custom fields are actually accessed on the elements. You can do this by having your element type’s getContentTableForElementsQuery() function return null. The only disadvantage here is that you won't be able to order/filter by custom field values, since those tables won’t have been joined in.

    If you go this route, you will need to make sure that your model that extends BaseElementModel is overriding the getContentTable() function (which it should be anyway, if you’re using custom content tables). That way it will know which table to pull data from if any custom field values are requested from it.

In your case, if each element is going to have its own content table, then option #2 is probably going to be your best bet.

1
  • Thanks for the overview of these options. I've settled for a simpler solution for the time being and added a 'Title' field to all the content tables that the user can set like Title Format using the renderObjectModel method. Aug 10 '14 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.