<p>Okie doke. After bashing my head against the wall <strong><em>THIS</em></strong> is how you create your very own super deluxe content table. Allow me to explain...</p>

<p>In your <strong><em>element class</em></strong> you will need to specify the following:</p>

<pre><code>  public function hasContent()
  {
    return true; // You definitely will need this
  }

  public function hasTitles()
  {
    return true; // This is optional
  }

  public function isLocalized()
  {

    return false; // This is optional

  }

  public function getContentTableForElementsQuery(ElementCriteriaModel $criteria){

    return "custom_content_table_name"; // You definitely will need this

  }

  public function getFieldsForElementsQuery(ElementCriteriaModel $criteria)
  {

      // NOTE: I do not have any custom fields set to this table. If you have custom fields you should return them here so that they are included in the criteria query. If you also do not have custom fields just do what I'm doing here and return an empty array. 

       $fields = array();

       return $fields;

  }
</code></pre>

<p>In your <strong><em>element's model class</em></strong> you'll need to do the following:</p>

<pre><code>  public function getContentTable(){

    return "custom_content_table_name"; // you will need this

  }

  public function getFieldContext()
  {

    return 'custom_content_table_name'; // I have NO idea what this is and the Craft documentation is not clear about it.

  }
</code></pre>

<p>Finally, your record for the content table will have to make the following columns:</p>

<ol>
<li>elementId</li>
<li>title</li>
<li>locale</li>
</ol>

<p>That's basically it. If you run into any errors make sure you're not including extra non-existent fields in the criteria queries when querying content. Check the Craft logs to see exactly what's happening if you encounter any problems.</p>

CC BY-SA 3.0
2015-05-08T14:45:08.877Z
2015-05-08T14:45:08.877Z
Matt V
<p>I've created a custom Element Type, and used the <code>getContentTableForElementsQuery()</code> to set where the element's content is stored.</p>

<p>For instance, the default BaseElement class defines:</p>

<pre><code>public function getContentTableForElementsQuery(ElementCriteriaModel $criteria)
{
    return 'content'; // table craft_content
}
</code></pre>

<p>Here, you would simply provide the table you want to store your data in, rather than <code>content</code>.</p>

<p>Then, when going to save the field(s), you need to set the contentTable like so:</p>

<pre><code>$originalContentTable = craft()-&gt;content-&gt;contentTable;

craft()-&gt;content-&gt;contentTable = 'mycontenttable';

// Save your fields

craft()-&gt;content-&gt;contentTable = $originalContentTable;
</code></pre>

<p>FYI - I learnt this from looking at how SproutForms works, which uses its own content table for fields.</p>

CC BY-SA 3.0
2015-05-07T13:44:29.993Z
Ehh. I've done this so, considering your answer, I'm either missing something or not explaining myself well enough. What I'm after is the ability to define where Craft fields go. Right now If I create a new plain text field a column gets created in craft_content. I would prefer to be able to choose where that column gets created. Is that possible?
Matt V
CC BY-SA 3.0
2015-05-07T13:50:30.583Z
Sure, I've added some extra code to the above answer
crawf
CC BY-SA 3.0
2015-05-07T13:58:35.730Z
As per your updated answer, I assume I'll need to make custom fields to get them to save where I want?
Matt V
CC BY-SA 3.0
2015-05-07T14:18:16.837Z
You don't have to - you just put whatever your saving (your element) between those `contentTable` assignments. Haven't tried, but `craft()->elements->saveElement($element)` should do
crawf
CC BY-SA 3.0
2015-05-07T17:07:48.887Z
Ok. So let's take some steps back because I'm clearly doing something wrong. I have **getContentTableForElementsQuery** returning the name of my custom table and I have **hasContent** set to true. In my model I have **getContentTable** returning the same table name as **getContentTableForElementsQuery**. Now in my CP when I go to my page for inputting data for my element I immediately see "An unknown error has occurred"
Matt V
CC BY-SA 3.0
2015-05-07T12:33:45.687Z
2015-05-07T13:54:34.510Z
2015-05-07T13:54:34.510Z
crawf
<p>If I want to create fields that get set to a custom content table rather than craft_content how would I do that?</p>

<plugin-development><field><content>
Creating and using custom content tables
# Creating and using custom content tables

(Asked by: username_0 on 2015-05-06)

If I want to create fields that get set to a custom content table rather than craft\_content how would I do that?

**Tags:** plugin-development, field, content

---

## 2 answers

### Answer by username_0 on 2015-05-08. Score: 3

Okie doke. After bashing my head against the wall THIS is how you create your very own super deluxe content table. Allow me to explain...
In your element class you will need to specify the following:
  public function hasContent()
  {
    return true; // You definitely will need this
  }

  public function hasTitles()
  {
    return true; // This is optional
  }

  public function isLocalized()
  {

    return false; // This is optional

  }

  public function getContentTableForElementsQuery(ElementCriteriaModel $criteria){

    return "custom_content_table_name"; // You definitely will need this

  }

  public function getFieldsForElementsQuery(ElementCriteriaModel $criteria)
  {

      // NOTE: I do not have any custom fields set to this table. If you have custom fields you should return them here so that they are included in the criteria query. If you also do not have custom fields just do what I'm doing here and return an empty array. 

       $fields = array();

       return $fields;

  }

In your element's model class you'll need to do the following:
  public function getContentTable(){

    return "custom_content_table_name"; // you will need this

  }

  public function getFieldContext()
  {

    return 'custom_content_table_name'; // I have NO idea what this is and the Craft documentation is not clear about it.

  }

Finally, your record for the content table will have to make the following columns:

elementId
title
locale

That's basically it. If you run into any errors make sure you're not including extra non-existent fields in the criteria queries when querying content. Check the Craft logs to see exactly what's happening if you encounter any problems.


---

### Answer by username_1 on 2015-05-07. Score: 1

I've created a custom Element Type, and used the getContentTableForElementsQuery() to set where the element's content is stored.
For instance, the default BaseElement class defines:
public function getContentTableForElementsQuery(ElementCriteriaModel $criteria)
{
    return 'content'; // table craft_content
}

Here, you would simply provide the table you want to store your data in, rather than content.
Then, when going to save the field(s), you need to set the contentTable like so:
$originalContentTable = craft()->content->contentTable;

craft()->content->contentTable = 'mycontenttable';

// Save your fields

craft()->content->contentTable = $originalContentTable;

FYI - I learnt this from looking at how SproutForms works, which uses its own content table for fields.

> **Comments:**
> **username_0**: Ehh. I've done this so, considering your answer, I'm either missing something or not explaining myself well enough. What I'm after is the ability to define where Craft fields go. Right now If I create a new plain text field a column gets created in craft_content. I would prefer to be able to choose where that column gets created. Is that possible?  
> **username_1**: Sure, I've added some extra code to the above answer  
> **username_0**: As per your updated answer, I assume I'll need to make custom fields to get them to save where I want?  
> **username_1**: You don't have to - you just put whatever your saving (your element) between those `contentTable` assignments. Haven't tried, but `craft()->elements->saveElement($element)` should do  
> **username_0**: Ok. So let's take some steps back because I'm clearly doing something wrong. I have **getContentTableForElementsQuery** returning the name of my custom table and I have **hasContent** set to true. In my model I have **getContentTable** returning the same table name as **getContentTableForElementsQuery**. Now in my CP when I go to my page for inputting data for my element I immediately see "An unknown error has occurred"  

---

