Answers.Body: <p>If your custom field needs more than the default column to store it's data you can create your own custom table (or tables) and store your data however you need to.</p>
  
  <p>This involves a few things:</p>
  
  <ul>
  <li>Create a Record that defines your database table</li>
  <li>Save the data to the custom table</li>
  <li>Retrieve the data from the custom table</li>
  </ul>
  
  <p><strong>Create a Record that defines your database table</strong></p>
  
  <p>To create a Record, take a look at the Craft <a href="http://buildwithcraft.com/docs/plugins/records" rel="nofollow">Records documentation</a>.  The Record defines the columns in your database table and that custom table will get created when you install your plugin, and removed when you uninstall your plugin. So, after you create your Record, go ahead and re-install your plugin to get the database table to appear in the database and ensure it's the schema you need.</p>
  
  <p><strong>Save the data to the custom table</strong></p>
  
  <p>Now that you have your custom table, you can save data to it.  In your Fieldtype Class <code>CustomPlugin_CustomFieldType.php</code> you can do so using the <code>onAfterElementSave()</code> method.</p>
  
  <pre><code>public function onAfterElementSave()
  {
    // $this-&gt;element will give you access access to the Element Model  
    // your field is associated with (For example, EntryModel, CategoryModel, etc).
  
    // $this-&gt;model will give you access access to the FieldModel 
    // your field is associated with.
  
    // craft()-&gt;request-&gt;post will give you access to the post data 
    // submitted on the request.
  
    // Once you have retrieved and modified the data you want to save,
    // you can go ahead and save it to your custom table 
  }
  </code></pre>
  
  <p><strong>Retrieve the data from the custom table</strong></p>
  
  <p>To populate your field, you'll retrieve your data from your custom tables in the <code>getInputHtml()</code> method.  In the example below, I'm using calling the <code>getCustomFieldValues()</code> method from the service layer which would query the database and give me back the data I need to hand off to my template to display:</p>
  
  <pre><code>public function getInputHtml($name, $value)
  {
      $values = craft()-&gt;customPlugin-&gt;getCustomFieldValues();
  
      return craft()-&gt;templates-&gt;render('customplugin/fields/input', array(
          'name'       =&gt; $name,
          'values'     =&gt; $values
      ));
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-14T23:40:26.600Z
Answers.LastActivityDate: 2015-08-14T23:40:26.600Z
Answers.UserDisplayName: Ben Parizek
Body: <p>I have a custom field where I'm defining <strong><em>AttributeType::Mixed</em></strong> in order to achieve the affect of saving multiple pieces of data for a single field. The data gets stored as JSON as expected. Quick example shown below:</p>
  
  <pre><code>public function defineContentAttribute()
  {
  
      return AttributeType::Mixed;
  
  }
  </code></pre>
  
  <p>I was wondering though, is it possible to define multiple columns for a single field? If so will the field then require its own table? Not sure where to begin with this one.</p>
  
  <p>Thanks for any assistance.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-07T13:07:57.630Z
Comments.Text: Out of interest, what's the benefit to using multiple columns? You've essentially got all the space you want using a JSON object. For instance, the Table field type stores all its column and default row data in that settings object. Happy to assist - but just need a bit more background on what your use-case is.
Comments.UserDisplayName: crawf
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-07T13:37:45.237Z
Comments.Text: I'm concerned about querying a JSON string. JSON is ok, and I've used it myself in my own applications outside of Craft, but it comes with overhead that you would not have using separate columns. Regardless, I solved this problem by making a custom field, setting the table attribute to false, saving it using a record after element save, then relating the data to the element to which it belongs.
Comments.UserDisplayName: Matt V
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-07T13:40:47.270Z
Comments.Text: Yeah, fair enough, and can totally understand. Depending on what you're doing, querying those settings can be a pain through JSON.
Comments.UserDisplayName: crawf
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-20T12:40:16.870Z
Comments.Text: Hey Matt - Wondering if you have any update on your question. I'm interested in the answer.
Comments.UserDisplayName: Jesse Schutt
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-21T14:20:57.770Z
Comments.Text: I don't have a tremendous amount of time right now to go into detail but I'll give you the gist of it. Every field has a "onAfterElementSave" method. You can utilize this method to save a field however you'd like. Essentially What I did was; create a record, in the function "onAfterElementSave" I set up logic on how I wanted the field to save, after the record is saved I relate it to the element to which it belongs (You can relate the data however you'd like but if you want to use Craft relations your field will need to save it's own elements). I suggest you look at the category field code.
Comments.UserDisplayName: Matt V
Tags: <plugin-development><custom-fieldtype>
Title: Can You Make a Custom Field with multiple columns?
ThreadText: # Can You Make a Custom Field with multiple columns?
  
  (Asked by: username_0 on 2015-04-30)
  
  I have a custom field where I'm defining ***AttributeType::Mixed*** in order to achieve the affect of saving multiple pieces of data for a single field. The data gets stored as JSON as expected. Quick example shown below:
  
  ```
  public function defineContentAttribute()
  {
  
      return AttributeType::Mixed;
  
  }
  
  ```
  
  I was wondering though, is it possible to define multiple columns for a single field? If so will the field then require its own table? Not sure where to begin with this one.
  
  Thanks for any assistance.
  
  **Tags:** plugin-development, custom-fieldtype
  
  > **Comments:**
  > **username_1**: Out of interest, what's the benefit to using multiple columns? You've essentially got all the space you want using a JSON object. For instance, the Table field type stores all its column and default row data in that settings object. Happy to assist - but just need a bit more background on what your use-case is.  
  > **username_0**: I'm concerned about querying a JSON string. JSON is ok, and I've used it myself in my own applications outside of Craft, but it comes with overhead that you would not have using separate columns. Regardless, I solved this problem by making a custom field, setting the table attribute to false, saving it using a record after element save, then relating the data to the element to which it belongs.  
  > **username_1**: Yeah, fair enough, and can totally understand. Depending on what you're doing, querying those settings can be a pain through JSON.  
  > **username_2**: Hey username_0 - Wondering if you have any update on your question. I'm interested in the answer.  
  > **username_0**: I don't have a tremendous amount of time right now to go into detail but I'll give you the gist of it. Every field has a "onAfterElementSave" method. You can utilize this method to save a field however you'd like. Essentially What I did was; create a record, in the function "onAfterElementSave" I set up logic on how I wanted the field to save, after the record is saved I relate it to the element to which it belongs (You can relate the data however you'd like but if you want to use Craft relations your field will need to save it's own elements). I suggest you look at the category field code.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2015-08-14. Score: 3
  
  If your custom field needs more than the default column to store it's data you can create your own custom table (or tables) and store your data however you need to.
  
  This involves a few things:
  
  * Create a Record that defines your database table
  * Save the data to the custom table
  * Retrieve the data from the custom table
  
  **Create a Record that defines your database table**
  
  To create a Record, take a look at the Craft [Records documentation](http://buildwithcraft.com/docs/plugins/records). The Record defines the columns in your database table and that custom table will get created when you install your plugin, and removed when you uninstall your plugin. So, after you create your Record, go ahead and re-install your plugin to get the database table to appear in the database and ensure it's the schema you need.
  
  **Save the data to the custom table**
  
  Now that you have your custom table, you can save data to it. In your Fieldtype Class `CustomPlugin_CustomFieldType.php` you can do so using the `onAfterElementSave()` method.
  
  ```
  public function onAfterElementSave()
  {
    // $this->element will give you access access to the Element Model  
    // your field is associated with (For example, EntryModel, CategoryModel, etc).
  
    // $this->model will give you access access to the FieldModel 
    // your field is associated with.
  
    // craft()->request->post will give you access to the post data 
    // submitted on the request.
  
    // Once you have retrieved and modified the data you want to save,
    // you can go ahead and save it to your custom table 
  }
  
  ```
  
  **Retrieve the data from the custom table**
  
  To populate your field, you'll retrieve your data from your custom tables in the `getInputHtml()` method. In the example below, I'm using calling the `getCustomFieldValues()` method from the service layer which would query the database and give me back the data I need to hand off to my template to display:
  
  ```
  public function getInputHtml($name, $value)
  {
      $values = craft()->customPlugin->getCustomFieldValues();
  
      return craft()->templates->render('customplugin/fields/input', array(
          'name'       => $name,
          'values'     => $values
      ));
  }
  
  ```
  
  
  ---
  
  