0

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 ;
  • Can you post some relevant code in the original question? Hard to tell what's going on without some context. – Brad Bell Mar 23 '15 at 23:58
  • 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 Mar 24 '15 at 3:40
  • what is the dd function ? – theatus Mar 24 '15 at 4:36
  • 1
    Dump and die (buildwithcraft.com/classreference//Craft#dd-detail). Will dump a variable's contents to the screen and end the request. – Brad Bell Mar 24 '15 at 4:50
  • 1
    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 so you can see what I was finding. – Damon Mar 30 '15 at 3:06
1

Use AttributeType::Name instead, this will undergo validation

  • What. This seems like a workaround for something broken in Craft. – Tyguy7 Sep 28 '17 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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