<p>Use <code>AttributeType::Name</code> instead, this will undergo validation</p>

CC BY-SA 3.0
2017-09-28T18:43:02.320Z
What.  This seems like a workaround for something broken in Craft.
Tyguy7
CC BY-SA 3.0
2015-06-21T14:36:20.097Z
2015-06-21T14:36:20.097Z
Bob Olde Hampsink
<p>I have create a plugin to save information from my form in the front-end.</p>

<p>I can create a row with some data. </p>

<p>Email and Number type work well but String is NULL</p>

<p>Have you any idea ?</p>

<h2>my controller</h2>

<pre><code>public function actionSaveInscription()
{
    $this-&gt;requirePostRequest();

    if ($id = craft()-&gt;request-&gt;getPost('inscriptionId')) {
       $model = craft()-&gt;inscriptionCommunautes-&gt;getInscriptionById($id);
    } else {
        $model = craft()-&gt;inscriptionCommunautes-&gt;newInscription($id);
    }

    $attributes = craft()-&gt;request-&gt;getPost('inscription');

    $model-&gt;setAttributes($attributes);

    if (craft()-&gt;inscriptionCommunautes-&gt;saveInscription($model)) {
        craft()-&gt;userSession-&gt;setNotice(Craft::t('Inscription saved.'));
        $model-&gt;getAttribute('id')));
    }
}
</code></pre>

<h2>My Service (saveAction)</h2>

<pre><code>public function saveInscription(InscriptionCommunautes_InscriptionModel &amp;$model)
{
    if ($id = $model-&gt;getAttribute('id')) {
        if (null === ($record = $this-&gt;inscriptionRecord-&gt;findByPk($id))) {
            throw new Exception(Craft::t('Can\'t find inscription with ID "{id}"', array('id' =&gt; $id)));
        }
    } else {
        $record = $this-&gt;inscriptionRecord-&gt;create();
    }
        $record-&gt;setAttributes($model-&gt;getAttributes());
    if ($record-&gt;save()) {
        $model-&gt;setAttribute('id', $record-&gt;getAttribute('id'));
        return true;
    } else {
        $model-&gt;addErrors($record-&gt;getErrors());
        return false;
    }
}
</code></pre>

<h2>My Model</h2>

<pre><code>protected function defineAttributes()
{
    return array(
        'id' =&gt; AttributeType::Number,
        'communaute' =&gt; AttributeType::String,
        'nom' =&gt; AttributeType::String,
        'courriel' =&gt; AttributeType::Email,
        'telephone' =&gt; AttributeType::String,
        'entraineurs' =&gt; AttributeType::Number,
        'accompagnateurs' =&gt; AttributeType::Number,
        'athletes' =&gt; AttributeType::Number,
    );
}
</code></pre>

<h2>My Record</h2>

<pre><code>public function getTableName()
{
    return 'CustomPlugin_Inscriptions';
}

protected function defineAttributes()
{
    return array(
        'communaute' =&gt; AttributeType::String,
        'nom' =&gt; AttributeType::String,
        'courriel' =&gt; AttributeType::Email,
        'telephone' =&gt; AttributeType::String,
        'entraineurs' =&gt; AttributeType::Number,
        'accompagnateurs' =&gt; AttributeType::Number,
        'athletes' =&gt; AttributeType::Number,
    );
}

public function defineIndexes()
{
    return array(

    );
}

public function create()
{
    $class = get_class($this);
    $record = new $class();

    return $record;
}
</code></pre>

<h2>Craft::dd($model);</h2>

<pre><code> Craft\InscriptionCommunautes_InscriptionModel#1
(
    [*:classSuffix] =&gt; 'Model'
    [*:strictAttributes] =&gt; true
    [Craft\BaseModel:_classHandle] =&gt; null
    [Craft\BaseModel:_attributeConfigs] =&gt; array
    (
        'id' =&gt; array
        (
            'maxLength' =&gt; 11
            'min' =&gt; -2147483648
            'max' =&gt; 2147483647
            'decimals' =&gt; 0
            'type' =&gt; 'number'
            'unsigned' =&gt; false
            'length' =&gt; 10
            'column' =&gt; 'integer'
        )
        'communaute' =&gt; array
        (
            'type' =&gt; 'string'
        )
        'nom' =&gt; array
        (
            'type' =&gt; 'string'
        )
        'courriel' =&gt; array
        (
            'maxLength' =&gt; 255
            'minLength' =&gt; 5
            'column' =&gt; 'varchar'
            'type' =&gt; 'email'
        )
        'telephone' =&gt; array
        (
            'type' =&gt; 'string'
        )
        'entraineurs' =&gt; array
        (
            'maxLength' =&gt; 11
            'min' =&gt; -2147483648
            'max' =&gt; 2147483647
            'decimals' =&gt; 0
            'type' =&gt; 'number'
            'unsigned' =&gt; false
            'length' =&gt; 10
            'column' =&gt; 'integer'
        )
        'accompagnateurs' =&gt; array
        (
            'maxLength' =&gt; 11
            'min' =&gt; -2147483648
            'max' =&gt; 2147483647
            'decimals' =&gt; 0
            'type' =&gt; 'number'
            'unsigned' =&gt; false
            'length' =&gt; 10
            'column' =&gt; 'integer'
        )
        'athletes' =&gt; array
        (
            'maxLength' =&gt; 11
            'min' =&gt; -2147483648
            'max' =&gt; 2147483647
            'decimals' =&gt; 0
            'type' =&gt; 'number'
            'unsigned' =&gt; false
            'length' =&gt; 10
            'column' =&gt; 'integer'
        )
    )
    [Craft\BaseModel:_attributes] =&gt; array
    (
        'communaute' =&gt; 'communaute'
        'nom' =&gt; 'nom'
        'courriel' =&gt; 'courriel@domaine.com'
        'telephone' =&gt; '1231231212'
        'entraineurs' =&gt; '1'
        'accompagnateurs' =&gt; '2'
        'athletes' =&gt; '3'
    )
    [Craft\BaseModel:_extraAttributeNames] =&gt; null
    [CModel:_errors] =&gt; array()
    [CModel:_validators] =&gt; null
    [CModel:_scenario] =&gt; ''
    [CComponent:_e] =&gt; null
    [CComponent:_m] =&gt; null
) 
</code></pre>

<h2>Craft::dd($model->getAttributes());</h2>

<pre><code>array
(
    'id' =&gt; null
    'communaute' =&gt; 'comm'
    'nom' =&gt; 'name'
    'courriel' =&gt; 'email@domaine.com'
    'telephone' =&gt; '1231231231'
    'entraineurs' =&gt; '1'
    'accompagnateurs' =&gt; '2'
    'athletes' =&gt; '3'
)
</code></pre>

<h2>Database Structure Generated</h2>

<pre><code>CREATE TABLE IF NOT EXISTS `craft_InscriptionCommunautes_Inscriptions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `communaute` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `nom` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `courriel` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `telephone` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `entraineurs` int(10) DEFAULT NULL,
  `accompagnateurs` int(10) DEFAULT NULL,
  `athletes` int(10) DEFAULT NULL,
  `dateCreated` datetime NOT NULL,
  `dateUpdated` datetime NOT NULL,
  `uid` char(36) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;
</code></pre>

CC BY-SA 3.0
2015-03-23T23:58:05.067Z
Can you post some relevant code in the original question? Hard to tell what's going on without some context.
Brad Bell
CC BY-SA 3.0
2015-03-24T03:40:06.433Z
Does your record have similar attributes? If you `Craft::dd($record)` right before you save it in your server are all of the attribute values setup properly?
Brad Bell
CC BY-SA 3.0
2015-03-24T04:36:23.980Z
what is the dd function ?
theatus
CC BY-SA 3.0
2015-03-24T04:50:05.937Z
Dump and die (http://buildwithcraft.com/classreference//Craft#dd-detail). Will dump a variable's contents to the screen and end the request.
Brad Bell
CC BY-SA 3.0
2015-03-25T21:23:31.190Z
no idea anyone ?


the string array is tiny in comparison with email :
'nom' => array
(
    'type' => 'string'
)
'courriel' => array
(
    'maxLength' => 255
    'minLength' => 5
    'column' => 'varchar'
    'type' => 'email'
)
theatus
CC BY-SA 3.0
2015-03-25T21:29:58.113Z
Sorry... `Craft::dd($model->getContent());` instead of just `$model`.
Brad Bell
CC BY-SA 3.0
2015-03-25T21:49:45.587Z
it give me an error 
Craft\InscriptionCommunautes_InscriptionModel and its behaviors do not have a method or closure named "getContent". 

with a stack trace
theatus
CC BY-SA 3.0
2015-03-25T21:53:45.153Z
I'll get it right eventually... `Craft::dd($model->getAttributes());`
Brad Bell
CC BY-SA 3.0
2015-03-25T22:17:10.887Z
What about `Craft::dd($record->getAttributes());` right before you call `$record->save()` in your service?
Brad Bell
CC BY-SA 3.0
2015-03-25T22:17:14.157Z
array  
(  
    'id' => null  
    'communaute' => 'comm'  
    'nom' => 'name'  
    'courriel' => 'email@domaine.com'  
    'telephone' => '1231231231'  
    'entraineurs' => '1'  
    'accompagnateurs' => '2'  
    'athletes' => '3'  
)
theatus
CC BY-SA 3.0
2015-03-25T22:26:43.310Z
I'm stumped... everything looks like it should work. If you look at the `courriel` column in the database, what data type is it set to?  `varchar(255)`?
Brad Bell
CC BY-SA 3.0
2015-03-26T01:16:57.523Z
it's really weird. i have added the database export structure
theatus
CC BY-SA 3.0
2015-03-26T15:20:58.330Z
i have try Mixed and Name type. with mixed, its the same thing but with Name it finally work
theatus
CC BY-SA 3.0
2015-03-30T03:06:49.013Z
I'm new to Craft development but I found that `AttributeType::String` does not perform any validation and therefore is considered unsafe to store in your db. Try using `AttributeType::Name` to validate against a string. [Here is my thread](http://craftcms.stackexchange.com/questions/8884/save-contact-form-data-to-database/8887?noredirect=1#comment10466_8887) so you can see what I was finding.
Damon
<plugin-development><database><models><records><attribute>
AttributeType::String is NULL in my database
# AttributeType::String is NULL in my database

(Asked by: username_0 on 2015-03-23)

I have create a plugin to save information from my form in the front-end.
I can create a row with some data. 
Email and Number type work well but String is NULL
Have you any idea ?
my controller
public function actionSaveInscription()
{
    $this->requirePostRequest();

    if ($id = craft()->request->getPost('inscriptionId')) {
       $model = craft()->inscriptionCommunautes->getInscriptionById($id);
    } else {
        $model = craft()->inscriptionCommunautes->newInscription($id);
    }

    $attributes = craft()->request->getPost('inscription');

    $model->setAttributes($attributes);

    if (craft()->inscriptionCommunautes->saveInscription($model)) {
        craft()->userSession->setNotice(Craft::t('Inscription saved.'));
        $model->getAttribute('id')));
    }
}

My Service (saveAction)
public function saveInscription(InscriptionCommunautes_InscriptionModel &$model)
{
    if ($id = $model->getAttribute('id')) {
        if (null === ($record = $this->inscriptionRecord->findByPk($id))) {
            throw new Exception(Craft::t('Can\'t find inscription with ID "{id}"', array('id' => $id)));
        }
    } else {
        $record = $this->inscriptionRecord->create();
    }
        $record->setAttributes($model->getAttributes());
    if ($record->save()) {
        $model->setAttribute('id', $record->getAttribute('id'));
        return true;
    } else {
        $model->addErrors($record->getErrors());
        return false;
    }
}

My Model
protected function defineAttributes()
{
    return array(
        'id' => AttributeType::Number,
        'communaute' => AttributeType::String,
        'nom' => AttributeType::String,
        'courriel' => AttributeType::Email,
        'telephone' => AttributeType::String,
        'entraineurs' => AttributeType::Number,
        'accompagnateurs' => AttributeType::Number,
        'athletes' => AttributeType::Number,
    );
}

My Record
public function getTableName()
{
    return 'CustomPlugin_Inscriptions';
}

protected function defineAttributes()
{
    return array(
        'communaute' => AttributeType::String,
        'nom' => AttributeType::String,
        'courriel' => AttributeType::Email,
        'telephone' => AttributeType::String,
        'entraineurs' => AttributeType::Number,
        'accompagnateurs' => AttributeType::Number,
        'athletes' => AttributeType::Number,
    );
}

public function defineIndexes()
{
    return array(

    );
}

public function create()
{
    $class = get_class($this);
    $record = new $class();

    return $record;
}

Craft::dd($model);
 Craft\InscriptionCommunautes_InscriptionModel#1
(
    [*:classSuffix] => 'Model'
    [*:strictAttributes] => true
    [Craft\BaseModel:_classHandle] => null
    [Craft\BaseModel:_attributeConfigs] => array
    (
        'id' => array
        (
            'maxLength' => 11
            'min' => -2147483648
            'max' => 2147483647
            'decimals' => 0
            'type' => 'number'
            'unsigned' => false
            'length' => 10
            'column' => 'integer'
        )
        'communaute' => array
        (
            'type' => 'string'
        )
        'nom' => array
        (
            'type' => 'string'
        )
        'courriel' => array
        (
            'maxLength' => 255
            'minLength' => 5
            'column' => 'varchar'
            'type' => 'email'
        )
        'telephone' => array
        (
            'type' => 'string'
        )
        'entraineurs' => array
        (
            'maxLength' => 11
            'min' => -2147483648
            'max' => 2147483647
            'decimals' => 0
            'type' => 'number'
            'unsigned' => false
            'length' => 10
            'column' => 'integer'
        )
        'accompagnateurs' => array
        (
            'maxLength' => 11
            'min' => -2147483648
            'max' => 2147483647
            'decimals' => 0
            'type' => 'number'
            'unsigned' => false
            'length' => 10
            'column' => 'integer'
        )
        'athletes' => array
        (
            'maxLength' => 11
            'min' => -2147483648
            'max' => 2147483647
            'decimals' => 0
            'type' => 'number'
            'unsigned' => false
            'length' => 10
            'column' => 'integer'
        )
    )
    [Craft\BaseModel:_attributes] => array
    (
        'communaute' => 'communaute'
        'nom' => 'nom'
        'courriel' => 'courriel@domaine.com'
        'telephone' => '1231231212'
        'entraineurs' => '1'
        'accompagnateurs' => '2'
        'athletes' => '3'
    )
    [Craft\BaseModel:_extraAttributeNames] => null
    [CModel:_errors] => array()
    [CModel:_validators] => null
    [CModel:_scenario] => ''
    [CComponent:_e] => null
    [CComponent:_m] => null
) 

Craft::dd($model->getAttributes());
array
(
    'id' => null
    'communaute' => 'comm'
    'nom' => 'name'
    'courriel' => 'email@domaine.com'
    'telephone' => '1231231231'
    'entraineurs' => '1'
    'accompagnateurs' => '2'
    'athletes' => '3'
)

Database Structure Generated
CREATE TABLE IF NOT EXISTS `craft_InscriptionCommunautes_Inscriptions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `communaute` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `nom` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `courriel` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `telephone` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `entraineurs` int(10) DEFAULT NULL,
  `accompagnateurs` int(10) DEFAULT NULL,
  `athletes` int(10) DEFAULT NULL,
  `dateCreated` datetime NOT NULL,
  `dateUpdated` datetime NOT NULL,
  `uid` char(36) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6 ;

**Tags:** plugin-development, database, models, records, attribute

> **Comments:**
> **username_1**: Can you post some relevant code in the original question? Hard to tell what's going on without some context.  
> **username_1**: Does your record have similar attributes? If you `Craft::dd($record)` right before you save it in your server are all of the attribute values setup properly?  
> **username_0**: what is the dd function ?  
> **username_1**: Dump and die (http://buildwithcraft.com/classreference//Craft#dd-detail). Will dump a variable's contents to the screen and end the request.  
> **username_0**: no idea anyone ?   the string array is tiny in comparison with email : 'nom' => array (     'type' => 'string' ) 'courriel' => array (     'maxLength' => 255     'minLength' => 5     'column' => 'varchar'     'type' => 'email' )  
> **username_1**: Sorry... `Craft::dd($model->getContent());` instead of just `$model`.  
> **username_0**: it give me an error  Craft\InscriptionCommunautes_InscriptionModel and its behaviors do not have a method or closure named "getContent".   with a stack trace  
> **username_1**: I'll get it right eventually... `Craft::dd($model->getAttributes());`  
> **username_1**: What about `Craft::dd($record->getAttributes());` right before you call `$record->save()` in your service?  
> **username_0**: array   (       'id' => null       'communaute' => 'comm'       'nom' => 'name'       'courriel' => 'email@domaine.com'       'telephone' => '1231231231'       'entraineurs' => '1'       'accompagnateurs' => '2'       'athletes' => '3'   )  
> **username_1**: I'm stumped... everything looks like it should work. If you look at the `courriel` column in the database, what data type is it set to?  `varchar(255)`?  
> **username_0**: it's really weird. i have added the database export structure  
> **username_0**: i have try Mixed and Name type. with mixed, its the same thing but with Name it finally work  
> **username_2**: I'm new to Craft development but I found that `AttributeType::String` does not perform any validation and therefore is considered unsafe to store in your db. Try using `AttributeType::Name` to validate against a string. [Here is my thread](http://craftcms.stackexchange.com/questions/8884/save-contact-form-data-to-database/8887?noredirect=1#comment10466_8887) so you can see what I was finding.  

---

## 1 answers

### Answer by username_3 on 2015-06-21. Score: 1 | Accepted answer

Use `AttributeType::Name` instead, this will undergo validation

> **Comments:**
> **username_4**: What.  This seems like a workaround for something broken in Craft.  

---

