Yii Framework Forum: How to implement thread with database query in Yii 1.x - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to implement thread with database query in Yii 1.x Discus about the multi-threading and implementing in yii 1.x Rate Topic: ***** 1 Votes

#1 User is offline   Chinmay Sahu 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 30-March 17

Posted 28 November 2017 - 05:09 AM

I have an existing project. Now I want to use multi-threading in my project. But I searched in Google Here is not getting any sufficient information about Yii 1.x thread. Could you please help me. I am unable to get model inside of the `ContentThread` extension. See my code:


protected/extensions/threads/ContentThread.php


class ContentThread extends Thread {
    public $content;

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


    public function run() {
       foreach ($this->content as $value) {
            $his = new LogHistory();
            $his->user_id = 1000;
            $his->login_date = gmdate('Y-m-d H:i:s');
            $his->logout_date = gmdate('Y-m-d H:i:s');
            $his->ip = '0.0.0.0';
            $his->save();
        }
    }
}

TestController.php -> actionThread()

public function actionThread(){
        Yii::import('ext.threads.ContentThread');            
        $content = array(1,2,3,4,5); /* just for static */
        //echo "<pre>"; print_r($content); echo "</pre>";
        //echo "<pre>"; print_r(array_slice($content, 2)); exit;
        $thread_1 = new ContentThread(array_slice($content, 0, 2));
        $thread_2 = new ContentThread(array_slice($content, 3));

        $thread_1->start();
        $thread_2->start();


    }

Error Output:

Fatal error: Class 'LoginHistory' not found in C:\xampp\htdocs\xxxxx\protected\extensions\threads\ContentThread.php on line 19

Fatal error: Class 'LoginHistory' not found in C:\xampp\htdocs\xxxxx\protected\extensions\threads\ContentThread.php on line 19


See I have posted my coding. Here is Yii model LoginHistory() not working. Because of my class extend from Thread instead of CApplicationComponent. How it will work with Yii context?
Chinmay Sahu
Senior Software Engineer
Muvi - Launch your video platform in 1-Click
www.muvi.com
Recognized as “Enterprise OTT” platform of the year by FierceOnlineVideo
0

#2 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,497
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 28 November 2017 - 06:19 AM

did you try and import your model in your extension


public function run() {
Yii::import('path.to.models.LoginHistory');
       foreach ($this->content as $value) {
            $his = new LoginHistory();
            $his->studio_id = 54;
            $his->user_id = 1000;
            $his->login_at = gmdate('Y-m-d H:i:s');
            $his->logout_at = gmdate('Y-m-d H:i:s');
            $his->ip = '0.0.0.0';
            $his->save();
        }
    }

1

#3 User is offline   Chinmay Sahu 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 30-March 17

Posted 30 November 2017 - 06:21 AM

View Postalrazi, on 28 November 2017 - 06:19 AM, said:

did you try and import your model in your extension


public function run() {
Yii::import('path.to.models.LoginHistory');
       foreach ($this->content as $value) {
            $his = new LoginHistory();
            $his->studio_id = 54;
            $his->user_id = 1000;
            $his->login_at = gmdate('Y-m-d H:i:s');
            $his->logout_at = gmdate('Y-m-d H:i:s');
            $his->ip = '0.0.0.0';
            $his->save();
        }
    }



@alrazi This is not only the model related but also other yii context I need All yii context like Yii::app(), CDbExpression, LoginHistory::model(), model, controller, component, extensions , vendor, Yii mail etc. I searched Google since last 8 days. There are no any yii related thread.

Your Yii::import also not working. because ContentThread extends from Thread not CApplicationComponent or CWidget Thats why yii inbuild code not working. how will work in my ContentThread.php file?

Please help...
Chinmay Sahu
Senior Software Engineer
Muvi - Launch your video platform in 1-Click
www.muvi.com
Recognized as “Enterprise OTT” platform of the year by FierceOnlineVideo
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users