0

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);
        }
    }

}
7
  • @carlcs the documentation refers to it under the models section though? buildwithcraft.com/docs/plugins/models
    – Lettie
    Commented May 13, 2015 at 10:15
  • Yep you're right, ignore my comment.
    – carlcs
    Commented May 13, 2015 at 10:21
  • Are you able to post the code in your model class? Commented May 13, 2015 at 11:40
  • @AlecRitson no proplem. I've changed the name and field names for security.
    – Lettie
    Commented May 13, 2015 at 13:55
  • 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. :) Commented May 13, 2015 at 14:17

1 Answer 1

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.

1
  • cheers Alec this does work @bradbell would you agree on this solution?
    – Lettie
    Commented May 13, 2015 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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