Exemple #1
0
 public function load(ObjectManager $manager)
 {
     // Obtener todas las tiendas y ciudades de la base de datos
     $ciudades = $manager->getRepository('CiudadBundle:Ciudad')->findAll();
     $tiendas = $manager->getRepository('TiendaBundle:Tienda')->findAll();
     foreach ($ciudades as $ciudad) {
         $tiendas = $manager->getRepository('TiendaBundle:Tienda')->findByCiudad($ciudad->getId());
         for ($j = 1; $j <= 15; $j++) {
             $oferta = new Oferta();
             $oferta->setNombre($this->getNombre());
             $oferta->setDescription($this->getDescripcion());
             $oferta->setCondiciones($this->getCondiciones());
             $oferta->setRutaFoto('foto' . rand(1, 20) . '.jpg');
             $oferta->setPrecio(number_format(rand(100, 10000) / 100, 2));
             $oferta->setDescuento($oferta->getPrecio() * (rand(10, 70) / 100));
             // Una oferta se publica hoy, el resto se reparte entre el pasado y el futuro
             if (1 == $j) {
                 $fecha = 'today';
                 $oferta->setRevisada(true);
             } elseif ($j < 10) {
                 $fecha = 'now - ' . ($j - 1) . ' days';
                 // el 80% de las ofertas pasadas se marcan como revisadas
                 $oferta->setRevisada(rand(1, 1000) % 10 < 8);
             } else {
                 $fecha = 'now + ' . ($j - 10 + 1) . ' days';
                 $oferta->setRevisada(true);
             }
             $fechaPublicacion = new \DateTime($fecha);
             $fechaPublicacion->setTime(23, 59, 59);
             // Se debe clonar el valor de la fechaPublicacion porque si se usa directamente
             // el método ->add(), se modificaría el valor original, que no se guarda en la BD
             // hasta que se hace el ->flush()
             $fechaExpiracion = clone $fechaPublicacion;
             $fechaExpiracion->add(\DateInterval::createFromDateString('24 hours'));
             $oferta->setFechaPublicacion($fechaPublicacion);
             $oferta->setFechaExpiracion($fechaExpiracion);
             $oferta->setCompras(0);
             $oferta->setUmbral(rand(25, 100));
             $oferta->setCiudad($ciudad);
             // Seleccionar aleatoriamente una tienda que pertenezca a la ciudad anterior
             $tienda = $tiendas[array_rand($tiendas)];
             $oferta->setTienda($tienda);
             $manager->persist($oferta);
             $manager->flush();
         }
     }
 }