-1

以前它与 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;
        }

    }

错误在保存方法中。我非常感谢解决此错误的任何帮助。即使在公式和元素线程中对 Doctrine 的使用进行任何优化也会非常有帮助。谢谢你。

4

1 回答 1

0

在线上:

$indication-> setValue($ value);

接收到的值是一个数组,该字段在文档中定义为字符串。在这种情况下,该数组被认为是一个 Volatile 对象并生成错误“可捕获的致命错误:Volatile 类的对象无法转换为字符串”

解决方案是:

$indication-> setValue(json_encode ($ value));
于 2017-07-27T19:31:31.880 回答