A
A
Alexander Borisovich2013-07-15 12:01:10
MySQL
Alexander Borisovich, 2013-07-15 12:01:10

UPPERCASE save to database?

Is it possible in some way to specify that everything is saved as UPPERCASE?
We missed the moment, and different registers are pouring into the database. It is possible to drive the database to the uppercase, but are there any simple means, can it be done directly in Yii or how? or only specify in the SQL query UPPERCASE when saving?
How are you doing with this in general?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
rPman, 2013-07-15
@rPman

If you have varchar or text, then the case will be ignored during comparisons, so it’s smart enough to add uppercase everywhere when outputting.
ps and a trigger for change, ugly but effective.

C
cat_crash, 2013-07-15
@cat_crash

Of the obvious solutions
1. Write a Trigger for SQL. I assume that you are using MySQL. Example
2. Write a behavior for Yii that will convert everything to UPCASE before saving.

protected beforeSave()
{
$result=parent::beforeSave();

$result=strtoupper($result); //Ну или mb_strtoupper

return $result;
}

T
Timur, 2013-07-15
@XAKEPEHOK

And there is another option: save in any register, and when fetching, override afterFind

protected function afterFind() {
    parent::afterFind();
    $this->field = strtoupper($this->field);
}

S
softarts, 2018-05-02
@softarts

I built a structure, just an example of a table with currencies (it is necessary to store the currency code according to ISO, it is also short_name):
1. DB

CREATE TRIGGER currency_ucase_insert BEFORE INSERT ON currency FOR EACH ROW
SET NEW.short_name = UPPER(NEW.short_name);

CREATE TRIGGER currency_ucase_update BEFORE UPDATE ON currency FOR EACH ROW
SET NEW.short_name = UPPER(NEW.short_name);

2. Model yii.
I have added a method:
/*
     * Капсим сокращение валют
     */
    public function afterFind()
    {
        parent::afterFind();
        $this->short_name = strtoupper($this->short_name);
    }

3. Input field
echo $form->field(
    $model,
    'short_name'
)->textInput([
    'maxlength' => true,
    'style'     => 'text-transform: uppercase',
]);

Everything seems to be class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question