如何在PHP 7.0.2中使用Symfnony2将Doctrine2注入Thread?

时间:2017-07-26 15:04:36

标签: php multithreading symfony php-pthread

以前它使用的是PHP 5.6.3版本,pthreads,Symfony2,Doctrine2,MongoDB,一切都运行良好。我决定迁移到PHP 7.0.2,我安装了pthreads,我还在使用Symfony2,Doctrine2和MongoDB,但是多线程处理停止了工作。

我定义了以下类:

   class Formula extends \Worker
   {
        static $document_manager;
        static $elements;

        public function start( int $options = NULL  )
        {
            parent::start(PTHREADS_INHERIT_NONE);
        }

        public function run()
        {
            //Set require Autoload and AppKernel
            require_once __DIR__.'/../../../../app/autoload.php';
            require_once __DIR__.'/../../../../app/AppKernel.php';

            //Creating a new AppKernel with the given environment
            $kernel = new \AppKernel( 'dev', 1 );
            //Loading the Cache and Classes
            $kernel->loadClassCache();
            $kernel->boot();

            //Set document manager
            static::$document_manager = $kernel->getContainer()->get('doctrine_mongodb')->getManager(); 

            static::$elements = static::$document_manager->getRepository('MyBundle:Elements')->findAllActive();

            $elements = array();

            //Creating array of objects type Element
            if ( static::$elements )
            {
                foreach ( static::$elements as $element )
                {
                    $elements[] = new Element( $element );
                }
            }

            if (!empty($elements))
            {
                foreach ( $elements as $element )
                {
                    //For each element execute the run method using start
                    $element->start();
                    $element->join();               
                }
            }       

            $processed_elements = array();

            while ( count($elements) > 0 )
            {
                foreach ( $elements as $id => $element )
                {
                    //If finished the run method
                    if ( !$element->is_running )
                    {
                        $processed_elements[] = $element;
                        //Cleaning up once this thread is done
                        unset($elements[$id]);
                    }
                }
            }

            /**
             * Performing Logic with Processed Items    
             */
        }
    }


    class Element extends \Thread
    {
        public $is_running; 
        static $document_manager;
        static $medicine;

        public function __construct ( $Element )
        {
            error_log( 'Constructing Element Thread' );
            /**
             * Collect item data
             */

            $this->is_running = true;
        }

        public function start(  int $options = Null )
        {
            parent::start(PTHREADS_INHERIT_NONE);
        }   


        public function run()
        {
            //Set require Autoload and AppKernel
            require_once __DIR__.'/../../../../app/autoload.php';
            require_once __DIR__.'/../../../../app/AppKernel.php';

            //Creating a new AppKernel with the given environment
            $kernel = new \AppKernel( 'dev', 1 );
            //Loading the Cache and Classes
            $kernel->loadClassCache();
            $kernel->boot();

            //Set document manager
            static::$document_manager = $kernel->getContainer()->get('doctrine_mongodb')->getManager(); 

            /**
             * Logic for obtaining the medicine ....
             */

            $id_medicamento = 'xxxxxx';
            static::$medicine = static::$document_manager->getRepository('MyBundle:Medicine')->find($id_medicamento);


            $return = false;

            //Save in the database
            if ( $this->save() )
            {
                $return = true;
            }   

            $this->is_running = false;

            return $return;
        }


        protected function save()
        {
            error_log( 'Saving' );
            //Save the data
            if ( !empty(static::$medicine) )
            {
                //Create new instance of Indication
                $indication = new Indication();
                $indication->setName( 'indication name' );
                $indication->setValue( 'indication value' );
                $indication->setDoctor( "doctor's identification" );
                //Persist Indication
                static::$document_manager->persist( $indication );
                //Add new Indication in Medicine
                static::$medicine->addIndicacion( $indication );

                //Create instance of Event
                $event = new Event();
                $event->setAction( 'Setting indication' );
                $event->setDatetime( new \MongoDate() );
                $event->setComment( 'Event comment' );
                //Persist Event
                static::$document_manager->persist( $event );
                //Add new Event in Medicine
                static::$medicine->addEvento( $event );

                // Write in DB
                static::$document_manager->persist( static::$medicine );

                /**
                 * Here the bug is generated and it neither writes the Indication nor the Event 
                 * in the Medication collection, to know which error was generated I put the 
                 * following line between try-catch and the exception is:
                 * "Catchable Fatal Error: Object of class Volatile could not be converted to string"
                 */         
                static::$document_manager->flush();


                return true;
            }

            return false;
        }

    }

错误在sav​​e方法中。任何帮助解决这个错误我都非常感激。即使在公式和元素线程中使用Doctrine的任何优化都会非常有用。谢谢。

1 个答案:

答案 0 :(得分:0)

在线:

$indication-> setValue($ value);

收到的值是一个数组,该字段在文档中定义为一个字符串。 在这种情况下,该数组被视为一个易失性对象并生成错误" Catchable Fatal Error:类Volatile的对象无法转换为字符串"

解决方案是:

$indication-> setValue(json_encode ($ value));