2011-10-18 46 views
24

nếu tôi hiển thị trường "loại thực thể" trong biểu mẫu của tôi và tôi muốn lọc loại đối tượng này dựa trên đối số tôi chuyển từ bộ điều khiển, làm cách nào để thực hiện điều đó ..?chuyển dữ liệu từ bộ điều khiển đến Loại symfony2

//PlumeOptionsType.php 
public function buildForm(FormBuilder $builder, array $options) 
{ 
    $builder->add('framePlume', 'entity', array(
     'class' => 'DessinPlumeBundle:PhysicalPlume', 
     'query_builder' => function(EntityRepository $er) { 
           return $er->createQueryBuilder('pp') 
            ->where("pp.profile = :profile") 
            ->orderBy('pp.index', 'ASC') 
            ->setParameter('profile', ????) 
           ; 
          }, 

    )); 
} 

public function getName() 
{ 
    return 'plumeOptions'; 
} 

public function getDefaultOptions(array $options) 
{ 
    return array(
      'data_class'  => 'Dessin\PlumeBundle\Entity\PlumeOptions', 
      'csrf_protection' => true, 
      'csrf_field_name' => '_token', 
      // a unique key to help generate the secret token 
      'intention'  => 'plumeOptions_item', 
    ); 
} 
} 

và bên trong bộ điều khiển, tôi tạo ra các hình thức:

i have that argument that i need to pass in my action code: 
$profile_id = $this->getRequest()->getSession()->get('profile_id'); 
... 
and then i create my form like this 
$form = $this->createForm(new PlumeOptionsType(), $plumeOptions); 

các $ plumeOptions chỉ là một lớp học để tồn tại. Nhưng nó có mối quan hệ một-một với một lớp khác gọi là PhysicalPlume. Bây giờ, khi tôi muốn hiển thị 'framePlume' trong mã của tôi, tôi muốn hiển thị một thực thể PhysicalPlume được lọc.

+0

đã trả lời ... kiểm tra http://stackoverflow.com/questions/6716776/symfony-2-how-to-pass-data-to-for mbuilder – xeon

Trả lời

38

Bạn có thể truyền tham số cho các lớp mẫu như sau:

//PlumeOptionsType.php 
protected $profile; 

public function __construct (Profile $profile) 
{ 
    $this->profile = $profile; 
} 

Sau đó, sử dụng nó trong các query_builder của buildForm của bạn:

$profile = $this->profile; 

$builder->add('framePlume', 'entity', array(
    'class' => 'DessinPlumeBundle:PhysicalPlume', 
    'query_builder' => function(EntityRepository $er) use ($profile) { 
          return $er->createQueryBuilder('pp') 
           ->where("pp.profile = :profile") 
           ->orderBy('pp.index', 'ASC') 
           ->setParameter('profile', $profile) 
          ; 
         }, 

)); 

Và cuối cùng trong điều khiển của bạn:

// fetch $profile from DB 
$form = $this->createForm(new PlumeOptionsType($profile), $plumeOptions); 
+0

thx để trả lời, tôi nghĩ rằng bạn đã chính xác những gì tôi có nghĩa là ... Tuy nhiên, tôi đã nhận được một lỗi làm chính xác những gì bạn đề nghị. Sử dụng $ this khi không ở trong bối cảnh đối tượng trong PlumeBundle \ Form \ Type \ PlumeOptionsType.php – xeon

+0

http://pastebin.com/RVLFCxL4 http://pastebin.com/778ygFgR – xeon

+0

http://pastebin.com/RVLFCxL4 http://pastebin.com/778ygFgR http://pastebin.com/q81k8w9A Tôi nghĩ rằng sự cố của tôi liên quan đến chức năng gọi lại. tôi có thể đọc hồ sơ từ bên trong PlumeOptionsType, nhưng không phải từ bên trong 'query_builder' => chức năng (EntityRepository $ er) – xeon

4

Bạn có thể sử dụng $plumeOptions để chuyển tất cả mọi thứ cho đối số của mình, nhưng bạn cần thêm getDefaultOptions() vào PlumeOptionsType để chỉ định giá trị mặc định cho tùy chọn của mình. Xem ví dụ https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php để xem phương pháp này sẽ trông như thế nào.

+1

Bạn có thể xây dựng thêm ..? bạn có thể cụ thể hơn không ?? – xeon

+0

Tôi đã chỉnh sửa thông điệp của mình để thêm các biện pháp khác vào phương thức 'getDefaultOptions()' – greg0ire

+0

Ok, giả sử tôi đã thêm một profile_id mặc định trong mảng getDefaultOptions() trả về của PlumeOptionsType. và tôi đã sử dụng -> setParameter ('hồ sơ', $ tùy chọn ['profile_id']) ... nhưng cách chuyển profile_id sang $ plumeOptions ở địa điểm đầu tiên ?? Cảm ơn sự giúp đỡ của bạn !! – xeon

Các vấn đề liên quan