| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Core\Entity\ArticleType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use MysqlUuid\Formats\Binary; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use MysqlUuid\Uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Phinx\Migration\AbstractMigration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class ArticleDiscussions extends AbstractMigration | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     public function up() | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |         $this->table('article_discussions', ['id' => false]) | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |             ->addColumn('article_uuid', 'binary', ['limit' => 16]) | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |             ->addColumn('title', 'text') | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |             ->addColumn('body', 'text') | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |             ->addForeignKey('article_uuid', 'articles', 'article_uuid', ['delete' => 'NO_ACTION', 'update' => 'NO_ACTION']) | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |             ->create(); | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | //        $this->insertDummyData(); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function down() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->dropTable('article_discussions'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private function insertDummyData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $ids = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $rows = $this->fetchAll('select admin_user_uuid from admin_users;'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         foreach ($rows as $r) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $ids[] = $r['admin_user_uuid']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $faker = Faker\Factory::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $count = rand(250, 300); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         for ($i = 0; $i < $count; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $id = $faker->uuid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $mysqluuid = (new Uuid($id))->toFormat(new Binary()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $title = $faker->sentence(5, 15); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $article = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 'article_uuid'    => $mysqluuid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 'article_id'      => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 'slug'            => strtolower(trim(preg_replace('~[^\pL\d]+~u', '-', $title), '-')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 'status'          => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 'admin_user_uuid' => $ids[array_rand($ids)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 'type'            => ArticleType::DISCUSSION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $post = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 'article_uuid' => $mysqluuid, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 'title'        => 'Discussion: '.$title, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 'body'         => $faker->paragraph(15), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $this->insert('articles', $article); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $this->insert('article_discussions', $post); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  |