<p>I just had a bit more of a look into this, I got the same error as you but by changing</p>

<pre><code>'id' =&gt; array(AttributeType::Number, 'required' =&gt; true, 'unique' =&gt; true),
</code></pre>

<p>to just</p>

<pre><code>id =&gt; AttributeType::Number
</code></pre>

<p>It fixed the issue and I was able to validate on it no problem, I don't think you need to worry about the ID being unique or being set on the model as this will be handled automatically by your record when you use it to save to the database, so you can be certain that every models id will always be unique.</p>

CC BY-SA 3.0
2015-05-13T15:49:09.593Z
cheers Alec this does work @bradbell would you agree on this solution?
Lettie
CC BY-SA 3.0
2015-05-13T14:41:53.233Z
2015-05-13T14:41:53.233Z
Alec Ritson
<p>Hi I'm getting this error when I try to validate a model.</p>

<p>Craft\mymodelModel and its behaviors do not have a method or closure named "getTableSchema".</p>

<p>I'm using the following code in the controller function that calls the save. It saves the data fine but I want to validate the model first before it does so but this is throwing the above error. What am I missing?</p>

<pre><code>$model-&gt;setAttributes($attribute);

if($model-&gt;validate()) {
    // code that sends model off to be saved
} else {
    $model-&gt;getErrors();    
}
</code></pre>

<p>Cheers</p>

<p>Model class code:</p>

<pre><code>namespace Craft;

    /**
    * myModel Model
    *
    * Provides a read-only object representing a myModel, which is   returned
    * by our service class and can be used in our templates and controllers.
    */
    class myModels_myModelModel extends BaseModel
   {
    /**
     * Defines what is returned when someone puts {{ myModel }} directly
     * in their template.
     *
     * @return string
     */
     public function __toString()
   {
       return $this-&gt;name;
   }

   /**
     * Define the attributes this model will have.
     *
     * @return array
     */
     public function defineAttributes()
     {
        return array(
            'id'    =&gt; array(AttributeType::Number, 'required' =&gt; true, 'unique' =&gt; true),
        'field1'    =&gt; array(AttributeType::Number, 'required' =&gt; true),
        'field2' =&gt; array(AttributeType::Enum, 'values' =&gt; "value1,value2"),
        'field3'  =&gt; array(AttributeType::Enum, 'values' =&gt; "value1,value2,value3", 'required' =&gt; true, 'default' =&gt; 'value1'),
        'field4' =&gt;  array(AttributeType::Name, 'maxLength' =&gt; 255, 'required' =&gt; false),
        'field5' =&gt;  array(AttributeType::Name, 'maxLength' =&gt; 255, 'required' =&gt; false),
        'field6' =&gt;  array(AttributeType::Name, 'maxLength' =&gt; 255, 'required' =&gt; false),
        'field7' =&gt;  array(AttributeType::Name, 'maxLength' =&gt; 255, 'required' =&gt; false),
        'field8' =&gt;  array(AttributeType::Name, 'maxLength' =&gt; 255, 'required' =&gt; false),
        'field9' =&gt;  array(AttributeType::Name, 'maxLength' =&gt; 255, 'required' =&gt; false),
        'field10' =&gt;  array(AttributeType::Name, 'maxLength' =&gt; 255, 'required' =&gt; false),
        'field11' =&gt;  array(AttributeType::Name, 'maxLength' =&gt; 255, 'required' =&gt; false),
        'field12' =&gt;  array(AttributeType::Name, 'maxLength' =&gt; 255, 'required' =&gt; false),
        'field13' =&gt;  array(AttributeType::Name, 'maxLength' =&gt; 255, 'required' =&gt; false)
        );
    }

    /**
     * Get User
     */
    public function getmyModel()
    {
        if ($this-&gt;userId)
        {
            return craft()-&gt;users-&gt;getUserById($this-&gt;userId);
        }
    }

}
</code></pre>

CC BY-SA 3.0
2015-05-13T10:15:22.207Z
@carlcs the documentation refers to it under the models section though? http://buildwithcraft.com/docs/plugins/models
Lettie
CC BY-SA 3.0
2015-05-13T10:21:48.033Z
Yep you're right, ignore my comment.
carlcs
CC BY-SA 3.0
2015-05-13T11:40:22.983Z
Are you able to post the code in your model class?
Alec Ritson
CC BY-SA 3.0
2015-05-13T13:55:47.387Z
@AlecRitson no proplem. I've changed the name and field names for security.
Lettie
CC BY-SA 3.0
2015-05-13T14:17:12.413Z
Okay, just to clarify, in your actual code. Your model class name follows `PluginHandle_ModelNameModel` convention and you are calling a new instance of the model like `new PluginHandle_ModelNameModel()` ? Just because in my experience with looking at errors with models, the exception should start `Craft\PluginHandle_ModelNameModel` whereas the error you pasted doesn't , I understand you have changed the names of things, but I just wanted to make sure. :)
Alec Ritson
CC BY-SA 3.0
2015-05-13T14:39:53.707Z
@AlecRitson yes sorry I should have changed it to the naming convention and yes to calling a new model
Lettie
CC BY-SA 3.0
2015-05-13T14:41:11.373Z
Can you enable devMode and share the full stack trace of the error?
Brad Bell
<plugin-development><models><validation>
Error with model validation
# Error with model validation

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

Hi I'm getting this error when I try to validate a model.
Craft\mymodelModel and its behaviors do not have a method or closure named "getTableSchema".
I'm using the following code in the controller function that calls the save. It saves the data fine but I want to validate the model first before it does so but this is throwing the above error. What am I missing?
$model->setAttributes($attribute);

if($model->validate()) {
    // code that sends model off to be saved
} else {
    $model->getErrors();    
}

Cheers
Model class code:
namespace Craft;

    /**
    * myModel Model
    *
    * Provides a read-only object representing a myModel, which is   returned
    * by our service class and can be used in our templates and controllers.
    */
    class myModels_myModelModel extends BaseModel
   {
    /**
     * Defines what is returned when someone puts {{ myModel }} directly
     * in their template.
     *
     * @return string
     */
     public function __toString()
   {
       return $this->name;
   }

   /**
     * Define the attributes this model will have.
     *
     * @return array
     */
     public function defineAttributes()
     {
        return array(
            'id'    => array(AttributeType::Number, 'required' => true, 'unique' => true),
        'field1'    => array(AttributeType::Number, 'required' => true),
        'field2' => array(AttributeType::Enum, 'values' => "value1,value2"),
        'field3'  => array(AttributeType::Enum, 'values' => "value1,value2,value3", 'required' => true, 'default' => 'value1'),
        'field4' =>  array(AttributeType::Name, 'maxLength' => 255, 'required' => false),
        'field5' =>  array(AttributeType::Name, 'maxLength' => 255, 'required' => false),
        'field6' =>  array(AttributeType::Name, 'maxLength' => 255, 'required' => false),
        'field7' =>  array(AttributeType::Name, 'maxLength' => 255, 'required' => false),
        'field8' =>  array(AttributeType::Name, 'maxLength' => 255, 'required' => false),
        'field9' =>  array(AttributeType::Name, 'maxLength' => 255, 'required' => false),
        'field10' =>  array(AttributeType::Name, 'maxLength' => 255, 'required' => false),
        'field11' =>  array(AttributeType::Name, 'maxLength' => 255, 'required' => false),
        'field12' =>  array(AttributeType::Name, 'maxLength' => 255, 'required' => false),
        'field13' =>  array(AttributeType::Name, 'maxLength' => 255, 'required' => false)
        );
    }

    /**
     * Get User
     */
    public function getmyModel()
    {
        if ($this->userId)
        {
            return craft()->users->getUserById($this->userId);
        }
    }

}

**Tags:** plugin-development, models, validation
> **Comments:**
> **username_0**: @carlcs the documentation refers to it under the models section though? http://buildwithcraft.com/docs/plugins/models  
> **username_1**: Yep you're right, ignore my comment.  
> **username_2**: Are you able to post the code in your model class?  
> **username_0**: @AlecRitson no proplem. I've changed the name and field names for security.  
> **username_2**: Okay, just to clarify, in your actual code. Your model class name follows `PluginHandle_ModelNameModel` convention and you are calling a new instance of the model like `new PluginHandle_ModelNameModel()` ? Just because in my experience with looking at errors with models, the exception should start `Craft\PluginHandle_ModelNameModel` whereas the error you pasted doesn't , I understand you have changed the names of things, but I just wanted to make sure. :)  
> **username_0**: @AlecRitson yes sorry I should have changed it to the naming convention and yes to calling a new model  
> **username_3**: Can you enable devMode and share the full stack trace of the error?  

---

## 1 answers

### Answer by username_2 on 2015-05-13. Score: 1

I just had a bit more of a look into this, I got the same error as you but by changing
'id' => array(AttributeType::Number, 'required' => true, 'unique' => true),

to just
id => AttributeType::Number

It fixed the issue and I was able to validate on it no problem, I don't think you need to worry about the ID being unique or being set on the model as this will be handled automatically by your record when you use it to save to the database, so you can be certain that every models id will always be unique.

> **Comments:**
> **username_0**: cheers Alec this does work @bradbell would you agree on this solution?  

---

