2017-05-03 79 views
0

我們有一個Symfony2應用程序,允許管理員使用Ajax POST請求通過付款來貸記用戶的帳戶。 POST請求發送由Symfony控制器處理的JSON。控制者應該創建一個「支付」實體,驗證並堅持它。我們希望利用Symfony表單系統的內置驗證。Symfony2:使用Ajax和Forms創建相關實體而不加載所有實體

舉個例子,信貸ID爲20 100P用戶,張貼JSON是應該像...

{"user":"20","amount":"100","description":"test payment","paymentType":"credit"} 

在Symfony的結束,我們有一個PaymentType形式;

class PaymentType extends AbstractType 

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('user', 'entity', [ 
     'class' => 'OurAppBundle\Entity\User', 
     'choice_label' => 'email', 
     'multiple'  => false, 
     'expanded'  => true, 
    ]); 
    $builder->add('paymentType', 'choice', [ 
     'choices' => [ 1 => 'credit', 2 => 'debit'], 
     'choices_as_values' => true, 

    ]); 
    $builder->add('amount', 'number'); 
    $builder->add('description', 'textarea'); 
} 

支付實體與用戶實體相關幷包含驗證;

class Payment 
{ 
const TYPE_CREDIT = 'credit'; 
const TYPE_DEBIT = 'debit'; 

use TimestampableTrait; 

/** 
* The ID of the payment 
* 
* @var integer 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 


/** 
* the User object 
* 
* @var User 
* 
* @ORM\ManyToOne(targetEntity="User", inversedBy="payments") 
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE", nullable=false) 
*/ 
protected $user; 

/** 
* payment type 
* 
* either 'credit' - a credit to the user's account 
* or 'debit' - money taken out their account 
* 
* @var string 
* 
* @ORM\Column(name="payment_type", type="string", length=7, nullable=false) 
* 
* @Assert\Choice(choices= {"credit", "debit"}, message="Choose a valid payment type") 
*/ 
protected $paymentType; 

/** 
* amount 
* 
* amount of payment, in pence 
* 
* @var int 
* 
* @ORM\Column(name="amount", type="integer", nullable=false) 
* 
* @Assert\Range(
*  min = 1, 
*  max = 2000, 
*  minMessage = "Payments must be positive", 
*  maxMessage = "You cannot credit/debit more than £20" 
*) 
*/ 
protected $amount; 

/** 
* a string describing the transaction 
* 
* @var string 
* 
* @ORM\Column(name="description", type="string", length=255, nullable=false) 
* 
* @Assert\NotBlank(message="Please enter a description") 
*/ 
protected $description; 
.... 

我們在控制器中沿着代碼行;

public function creditUserAction(Request $request) 
{ 
     $body = $request->getContent(); 
     $formData = json_decode($body, true); 

     $payment = new Payment(); 
     $form = $this->createForm(new PaymentType(), $payment); 
     $form->submit($data); 
     if ($form->isValid()) { 
      // persist and flush ..... 

     } .... 

的問題是,當PaymentType形式是由裝的CreateForm,學說試圖從數據庫加載所有用戶和有數以萬計的用戶的。

所以,我的問題是 - 是否有更好的方法來創建一個實體,使用Ajax POST請求,這與另一個使用Symfony Forms的實體相關,其中相關實體有數千個實例?

回答

1

您可以驗證你的實體,而無需創建一個表單

$validator = $this->get('validator'); 
$errors = $validator->validate($payment); 
if(count($errors)) { 
    //... 
} 
+0

同意。我希望使用Symfony表單來保存手動設置實體的每個屬性 - 但是,手動設置每個屬性並不是一項繁重的任務。感謝您的幫助,我將使用您建議的方法。 –

相關問題