简体   繁体   English

机器人 yii2。 会话的属性丢失

[英]Botman yii2. Properties of the conversation lost

BotMan Version: 2.1 PHP Version:7.3 Messaging Service(s): Cache Driver: SymfonyCache Description: I trying to have conversation. BotMan 版本:2.1 PHP 版本:7.3 消息服务:缓存驱动程序:SymfonyCache 描述:我试图进行对话。 In every next method I lost data from conversation properties, that was saved in properties before!在每个 next 方法中,我都从对话属性中丢失了数据,这些数据之前保存在属性中!

class GetAlertDataConversation extends AppConversation
{
   public $bot;

   public $alertTitle;

   public $alertDescription;

   public $alertLatitude;

   public $alertLongitude;

   public $alertAuthorId;

   public $alertAuthorName;

   public function __construct($bot)
   {
      $this->bot = $bot;
   }


   private function askTitle()
   {

      $this->ask('Что случилось? (кратко)', function (Answer $answer) {
         $this->alertTitle = $this->askSomethingWithLettersCounting($answer->getText(), 'Слишком коротко!', 'askDescription');
         \Yii::warning($this->alertTitle);
      });
   }

   public function askDescription()
   {

      $this->ask('Расскажи подробней!', function (Answer $answer) {
         $this->alertDescription = $this->askSomethingWithLettersCounting($answer->getText(), 'Слишком коротко!', 'askLocation');
         \Yii::warning($this->alertTitle);
      });
   }

   private function askLocation()
   {
      \Yii::warning($this->alertTitle);
      $this->askForLocation('Локация?', function (Location $location) {
         // $location is a Location object with the latitude / longitude.
         $this->alertLatitude = $location->getLatitude();
         $this->alertLongitude = $location->getLongitude();
         $this->endConversation();
         return true;
      });
   }

   private function endConversation()
   {
      \Yii::warning($this->alertTitle);
      $alertId = $this->saveAlertData();
      if ($alertId)
         $this->say("Событие номер {$alertId} зарегистрировано!");
      else
         $this->say("Ошибка при сохранении события, обратитесь к администратору!");
   }

   private function saveAlertData()
   {

      $user = $this->bot->getUser();
      $this->alertAuthorId = $user->getId();
      $this->alertAuthorName = $user->getFirstName() . ' ' . $user->getLastName();


      $alert = new Alert();
      \Yii::warning($this->alertTitle);
      $alert->name = $this->alertTitle;
      $alert->description = $this->alertDescription;
      $alert->latitude = $this->alertLatitude;
      $alert->longitude = $this->alertLongitude;
      $alert->author_id = $this->alertAuthorId;
      $alert->author_name = $this->alertAuthorName;
      $alert->chat_link = '';
      $alert->additional = '';
      if ($alert->validate()) {
         $alert->save();
         return $alert->id;
      } else {
         \Yii::warning($alert->errors);
         \Yii::warning($alert);
         return false;
      }
   }
}

There is user's text answer in the first \Yii::warning($this->alertTitle);第一个\Yii::warning($this->alertTitle);中有用户的文本答案in the askTitle() function.在 askTitle() function 中。 But all other \Yii::warning($this->alertTitle);但所有其他 \Yii::warning($this->alertTitle); returns NULL,,.?返回 NULL,,.? As the result, saving of Alert object not working.结果,警报 object 的保存不起作用。 Please.请。 help me.帮我。 Some ideas?一些想法? I think, that it can be by some caching + serialise problem.我认为,这可能是一些缓存+序列化问题。 I was trying to change cache method to Redis.我试图将缓存方法更改为 Redis。 Same result.结果相同。

Problem was in this function call and caching:问题出在这个 function 调用和缓存中:

$this->askSomethingWithLettersCounting($answer->getText(), 'Слишком коротко!', 'askDescription');

If you will read other conversation botman issues in github, you wil see, that most of issues in conversation cache and serialise.如果您将阅读 github 中的其他对话机器人问题,您将看到对话缓存和序列化中的大多数问题。 Not any PHP code of conversation can be cached right.不能正确缓存对话的任何 PHP 代码。 In this case, not direct call of functions askDescription() and askLocation() broken conversation caching.在这种情况下,不直接调用函数 askDescription() 和 askLocation() 会破坏对话缓存。 I fixed that by removing askSomethingWithLettersCounting() function.我通过删除 askSomethingWithLettersCounting() function 解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM