symfony - Symfony2 get parameters.yml in entity, special case -


i know against framework make entity container aware, special case, have credit card entity, , want this:

/**  * @return mixed  */ public function getnumber() {     $number = $this->number;     $crypt = base64_decode($number);     $number = mcrypt_decrypt(mcrypt_rijndael_256, $key, $crypt, mcrypt_mode_ecb);     return trim($number); }  /**  * @param $number  * @return $this  */ public function setnumber($number) {     $crypt = mcrypt_encrypt(mcrypt_rijndael_256, $key, $number, mcrypt_mode_ecb);     $number = trim(base64_encode($crypt));     $this->number = $number;     return $this; } 

and want $key secret parameters.yml, since dont want save in code.

i can't pass parameter, when use formtype, cause form type not pass when binds request.

    $credit_card = new creditcard();     $credit_card->setcustomer($customer);     $payment_form = $this->createpaymentform($credit_card);      $payment_form->handlerequest($request); 

remove data modifications entity, , add formtype.

public function getnumber() {     return $this->number; }  public function setnumber($number) {     $this->number = $number;      return $this; } 

make formtype take parameter argument.

class creditcardtype extends abstracttype {      private $key;      public function __construct($key) {         $this->key = $key;     }      // ...      // listen on pre_bind event update value before binding     $builder->addeventlistener(formevents::pre_bind, function (formevent $event)      {         $data = $event->getdata();         $number = $data['number'];         $crypt = $this->encryptnumber($number);         $data['number'] = $crypt;          $event->setdata($data);     });      public function encryptnumber($key, $number)     {         $crypt = mcrypt_encrypt(mcrypt_rijndael_256, $key, $number, mcrypt_mode_ecb);         $number = trim(base64_encode($crypt));          return $number;         } } 

now, in createpaymentform, add parameter formtype instance, :

$param = $this->container->getparameter('yourkeyparam');  $form = $this->createform(new creditcardtype($param), $credit_card, array()); 

hope need.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -