Answer the question
In order to leave comments, you need to log in
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
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.
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;
}
And there is another option: save in any register, and when fetching, override afterFind
protected function afterFind() {
parent::afterFind();
$this->field = strtoupper($this->field);
}
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);
/*
* Капсим сокращение валют
*/
public function afterFind()
{
parent::afterFind();
$this->short_name = strtoupper($this->short_name);
}
echo $form->field(
$model,
'short_name'
)->textInput([
'maxlength' => true,
'style' => 'text-transform: uppercase',
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question