I use the widget to perform any kind of text operation. And this operation must be performed in beforeSave (). Well, or come up with something else. This is a widget.

<?php require_once('/../EMT.php'); class lnEMTypograph extends CWidget { public $text; public function init() { parent::init(); } public function run() { $typograph = new EMTypograph(); $typograph->setup([ 'Text.paragraphs' => 'off', 'OptAlign.oa_quote' => 'off', ]); $typograph->set_text($text); $text = $typograph->apply(); echo $text; $this->render($text); } } 

    1 answer 1

    IMHO is not a good decision to use widget in a model as they are intended to be used in view. In addition, the widget should generate some markup, and in your case, the widget will format the text. I would make a certain assistant in the form of a class with a static method.

    Sample helper

     class TypographHelper { public static function apply($text) { $typograph = new EMTypograph(); $typograph->setup([ 'Text.paragraphs' => 'off', 'OptAlign.oa_quote' => 'off', ]); $typograph->set_text($text); return $typograph->apply(); } } 

    Then in modelka use it:

     class ... extends \yii\db\ActiveRecord { public function beforeSave($insert){ if (parent::beforeSave($insert)) { $this->text = TypographHelper::apply($this->text); return true; } else { return false; } } } 
    • Thank you very much. Here I have a stupid question. In the helper, you can use require_once ('/../ EMT.php'); or is there any more correct way to add the desired file? - user220125
    • Use Namespaces - Kison