public function load(ObjectManager $manager) { // Obtener todas las tiendas y ciudades de la base de datos $cities = $manager->getRepository('TestBundle:City')->findAll(); $shops = $manager->getRepository('TestBundle:Shop')->findAll(); foreach ($cities as $city) { $shops = $manager->getRepository('TestBundle:Shop')->findByCity($city->getId()); for ($j = 1; $j <= 15; $j++) { $offer = new Offer(); $offer->setName($this->getName()); $offer->setSlug(Util::getSlug($this->getName())); $offer->setDescription($this->getDescription()); $offer->setConditions($this->getConditions()); $offer->setPicture('picture' . rand(1, 20) . '.jpg'); $offer->setPrice(number_format(rand(100, 10000) / 100, 2)); $offer->setDiscount($offer->getPrice() * (rand(10, 70) / 100)); // Una oferta se publica hoy, el resto se reparte entre el pasado y el futuro if (1 == $j) { $date = 'today'; $offer->setChecked(true); } elseif ($j < 10) { $date = 'now - ' . ($j - 1) . ' days'; // el 80% de las ofertas pasadas se marcan como revisadas $offer->setChecked(rand(1, 1000) % 10 < 8); } else { $date = 'now + ' . ($j - 10 + 1) . ' days'; $offer->setChecked(true); } $publicationDate = new \DateTime($date); $publicationDate->setTime(0, 0, 0); // this is a really mess // 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() $expirationDate = clone $publicationDate; $expirationDate->add(\DateInterval::createFromDateString('24 hours')); $offer->setPublicationDate($publicationDate); $offer->setExpirationDate($expirationDate); $offer->setShopping(0); $offer->setThreshold(rand(25, 100)); $offer->setCity($city); // Seleccionar aleatoriamente una tienda que pertenezca a la ciudad anterior $shop = $shops[array_rand($shops)]; $offer->setShop($shop); $manager->persist($offer); $manager->flush(); } } }
/** * Set name * * @param string $name * @return City */ public function setName($name) { $this->name = $name; $this->slug = Util::getSlug($name); }