示例#1
0
 /**
  * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
  */
 public function configureFormFields(FormMapper $formMapper)
 {
     $formMapper->with($this->trans('address.form.group_advanced_label', array(), 'SonataCustomerBundle'))->add('type', 'choice', array('choices' => Address::getTypesList(), 'translation_domain' => 'SonataCustomerBundle'))->add('current', null, array('required' => false))->add('name')->end();
     $formMapper->with($this->trans('address.form.group_contact_label', array(), 'SonataCustomerBundle'))->add('firstname')->add('lastname')->add('phone')->end();
     if (!$this->isChild()) {
         $formMapper->with($this->trans('address.form.group_contact_label', array(), 'SonataCustomerBundle'))->add('customer', 'sonata_type_model_list')->end();
     }
     $formMapper->with($this->trans('address.form.group_address_label', array(), 'SonataCustomerBundle'))->add('address1')->add('address2')->add('address3')->add('postcode')->add('city')->add('countryCode', 'country')->end();
 }
 /**
  * Generates a Customer with his addresses.
  *
  * @param ObjectManager $manager
  * @param int $i Random number to avoid username collision
  *
  * @return Customer
  */
 protected function generateCustomer(ObjectManager $manager, $i)
 {
     $faker = $this->getFaker();
     $firstName = $faker->firstName();
     $lastName = $faker->lastName();
     $email = $faker->email();
     $username = $faker->userName();
     if (0 === $i % 50 && $this->hasReference('customer-johndoe')) {
         return $this->getReference('customer-johndoe');
     }
     // Customer
     $customer = new Customer();
     $customer->setTitle(array_rand(array(BaseCustomer::TITLE_MLLE, BaseCustomer::TITLE_MME, BaseCustomer::TITLE_MR)));
     $customer->setFirstname($firstName);
     $customer->setLastname($lastName);
     $customer->setEmail($email);
     $customer->setBirthDate($faker->datetime());
     $customer->getBirthPlace($faker->city());
     $customer->setPhoneNumber($faker->phoneNumber());
     $customer->setMobileNumber($faker->phoneNumber());
     $customer->setFaxNumber($faker->phoneNumber());
     $customer->setLocale('fr');
     $customer->setIsFake(true);
     // Customer billing address
     $customerBillingAddress = new Address();
     $customerBillingAddress->setType(BaseAddress::TYPE_BILLING);
     $customerBillingAddress->setCustomer($customer);
     $customerBillingAddress->setCurrent(true);
     $customerBillingAddress->setName('My billing address');
     $customerBillingAddress->setFirstname($customer->getFirstname());
     $customerBillingAddress->setLastname($customer->getLastname());
     $customerBillingAddress->setAddress1($faker->address());
     $customerBillingAddress->setPostcode($faker->postcode());
     $customerBillingAddress->setCity($faker->city());
     $customerBillingAddress->setCountryCode(0 === $i % 50 ? 'FR' : $faker->countryCode());
     $customerBillingAddress->setPhone($faker->phoneNumber());
     // Customer contact address
     $customerContactAddress = new Address();
     $customerContactAddress->setType(BaseAddress::TYPE_CONTACT);
     $customerContactAddress->setCustomer($customer);
     $customerContactAddress->setCurrent(true);
     $customerContactAddress->setName('My contact address');
     $customerContactAddress->setFirstname($customer->getFirstname());
     $customerContactAddress->setLastname($customer->getLastname());
     $customerContactAddress->setAddress1($faker->address());
     $customerContactAddress->setPostcode($faker->postcode());
     $customerContactAddress->setCity($faker->city());
     $customerContactAddress->setCountryCode(0 === $i % 50 ? 'FR' : $faker->countryCode());
     $customerContactAddress->setPhone($customer->getPhoneNumber());
     // Customer delivery address
     $customerDeliveryAddress = new Address();
     $customerDeliveryAddress->setType(BaseAddress::TYPE_DELIVERY);
     $customerDeliveryAddress->setCustomer($customer);
     $customerDeliveryAddress->setCurrent(true);
     $customerDeliveryAddress->setName('My delivery address');
     $customerDeliveryAddress->setFirstname($customer->getFirstname());
     $customerDeliveryAddress->setLastname($customer->getLastname());
     $customerDeliveryAddress->setAddress1($faker->address());
     $customerDeliveryAddress->setPostcode($faker->postcode());
     $customerDeliveryAddress->setCity($faker->city());
     $customerDeliveryAddress->setCountryCode(0 === $i % 50 ? 'FR' : $faker->countryCode());
     $customerDeliveryAddress->setPhone($faker->phoneNumber());
     $customer->addAddress($customerBillingAddress);
     $customer->addAddress($customerContactAddress);
     $customer->addAddress($customerDeliveryAddress);
     // User
     if (0 === $i % 10) {
         $user = $this->getReference('user-johndoe');
         $this->setReference('customer-johndoe', $customer);
     } else {
         /** @var \Sonata\UserBundle\Model\User $user */
         $user = new User();
         $user->setUsername($i . '-' . $username);
         $user->setUsernameCanonical($i . '-' . $username);
         $user->setEmail($i . '_' . $email);
         $user->setEmailCanonical($email);
         $user->setPlainPassword('customer');
     }
     $customer->setUser($user);
     $manager->persist($customerBillingAddress);
     $manager->persist($customerContactAddress);
     $manager->persist($customerDeliveryAddress);
     $manager->persist($user);
     $manager->persist($customer);
     return $customer;
 }