Witam wszystkich,

mam pytanie do forumowiczów.

Mianowicie jak wykonać wysłanie formularza podczas gdy encje są ze sobą powiązane.

Mam encję Equipment

  1.  
  2. namespace gsm\serviceBundle\Entity;
  3.  
  4. use Doctrine\ORM\Mapping as ORM;
  5.  
  6. /**
  7.  * @ORM\Entity
  8.  * @ORM\Table(name="equipment")
  9.  */
  10. class Equipment {
  11.  
  12. /**
  13.   * @ORM\Column(type="integer")
  14.   * @ORM\Id
  15.   * @ORM\GeneratedValue(strategy="AUTO")
  16.   */
  17. protected $id;
  18.  
  19. /**
  20.   * @ORM\ManyToOne(targetEntity="Users", inversedBy="Equipment")
  21.   * @ORM\JoinColumn(name="id_user", referencedColumnName="id")
  22.   */
  23. protected $user;
  24.  
  25. /**
  26.   * @ORM\ManyToOne(targetEntity="Producer", inversedBy="Equipment")
  27.   * @ORM\JoinColumn(name="id_producer", referencedColumnName="id")
  28.   */
  29. protected $producer;
  30.  
  31. /**
  32.   * @ORM\ManyToOne(targetEntity="Model", inversedBy="Equipment")
  33.   * @ORM\JoinColumn(name="id_model", referencedColumnName="id")
  34.   */
  35. protected $model;
  36.  
  37. /**
  38.   * @ORM\ManyToOne(targetEntity="Category", inversedBy="Equipment")
  39.   * @ORM\JoinColumn(name="id_category", referencedColumnName="id")
  40.   */
  41. protected $category;
  42.  
  43. /**
  44.   * @ORM\Column(type="integer")
  45.   */
  46. protected $amount;
  47.  
  48. public function getId() {
  49. return $this->id;
  50. }
  51.  
  52. public function getUser() {
  53. return $this->user;
  54. }
  55.  
  56. public function getProducer() {
  57. return $this->producer;
  58. }
  59.  
  60. public function getModel() {
  61. return $this->model;
  62. }
  63.  
  64. public function getCategory() {
  65. return $this->category;
  66. }
  67.  
  68. public function getAmount() {
  69. return $this->amount;
  70. }
  71.  
  72. public function setId($id) {
  73. $this->id = $id;
  74. return $this;
  75. }
  76.  
  77. public function setUser(Users $user) {
  78. $this->user = $user;
  79. return $this;
  80. }
  81.  
  82. public function setProducer(Producer $producer) {
  83. $this->producer = $producer;
  84. return $this;
  85. }
  86.  
  87. public function setModel(Model $model) {
  88. $this->model = $model;
  89. return $this;
  90. }
  91.  
  92. public function setCategory(Category $category) {
  93. $this->category = $category;
  94. return $this;
  95. }
  96.  
  97. public function setAmount($amount) {
  98. $this->amount = $amount;
  99. return $this;
  100. }
  101.  
  102. }
  103.  


Oraz encję Model

  1.  
  2. namespace gsm\serviceBundle\Entity;
  3.  
  4. use Doctrine\ORM\Mapping as ORM;
  5.  
  6. /**
  7.  * @ORM\Entity
  8.  * @ORM\Table(name="model")
  9.  */
  10. class Model {
  11.  
  12. /**
  13.   * @ORM\Column(type="integer")
  14.   * @ORM\Id
  15.   * @ORM\GeneratedValue(strategy="AUTO")
  16.   */
  17. protected $id;
  18.  
  19. /**
  20.   * @ORM\Column(type="string")
  21.   */
  22. protected $name;
  23.  
  24. public function getId() {
  25. return $this->id;
  26. }
  27.  
  28. public function getName() {
  29. return $this->name;
  30. }
  31.  
  32. public function setId($id) {
  33. $this->id = $id;
  34. return $this;
  35. }
  36.  
  37. public function setName($name) {
  38. $this->name = $name;
  39. return $this;
  40. }
  41.  
  42. }
  43.  


W kontrolerze mam kod:

  1.  
  2. public function createAction($id, Request $request) {
  3.  
  4. if($id){
  5.  
  6. $equipment = $this->getDoctrine()
  7. ->getRepository('gsmserviceBundle:Equipment')
  8. ->find($id);
  9.  
  10. } else {
  11.  
  12. $equipment = new Equipment;
  13.  
  14. }
  15.  
  16. $form = $this->createFormBuilder($equipment)
  17. ->add('user', 'entity', array(
  18. 'class' => 'gsm\serviceBundle\Entity\Users',
  19. 'choice_label' => 'getId',
  20. 'label' => 'Imię i nazwisko: '
  21. ))
  22. ->add('producer', 'entity', array(
  23. 'class' => 'gsm\serviceBundle\Entity\Producer',
  24. 'choice_label' => 'getName',
  25. 'label' => 'Producent: '
  26. ))
  27. ->add('category', 'entity', array(
  28. 'class' => 'gsm\serviceBundle\Entity\Category',
  29. 'choice_label' => 'getName',
  30. 'label' => 'Kategoria: '
  31. ))
  32. ->add('model', 'entity', array(
  33. 'class' => 'gsm\serviceBundle\Entity\Model',
  34. 'choice_label' => 'getName',
  35. 'label' => 'Model: '
  36. ))
  37. ->add('amount', 'text', array(
  38. 'label' => 'Ilość: '
  39. ))
  40. ->add('createEquipment', 'submit', array(
  41. 'label' => 'OK'
  42. ))
  43. ->getForm();
  44.  
  45. $form->handleRequest($request);
  46.  
  47. if($form->isSubmitted()){
  48.  
  49. $equipment = $form->getData();
  50. $manager = $this->getDoctrine()
  51. ->getManager();
  52.  
  53. $manager->persist($equipment);
  54. $manager->flush();
  55.  
  56. if ($equipment->getId() > 0) {
  57.  
  58. echo 'ok';
  59.  
  60. }
  61.  
  62. }
  63.  
  64. return $this->render('gsmserviceBundle:Equipment:create.html.twig', array('form' => $form->createView()));
  65.  
  66. }
  67.  


Ten kontroler również służy mi do edycji.

Ale sedno problemu jest takie chciałbym, aby pole formularza model mógłbym ręcznie wpisać np. Galaxy S3, niż wybierać z listy rozwijanej wcześniej predefiniowaną wartość oraz jak to rozwiązać z relacją, aby w encji Equipment było dodawane ID.

Mam nadzieje, że dobrze opisałem mój problem.

Pozdrawiam wszystkich oraz dziękuję z góry za odpowiedzi.

PS. Przepraszam, że kod jest trochę nieczytelny.
PS2. Przepraszam, że temat umieściłem w nieodpowiednim dziale, ale pomimo dodania tagów wciąż nie mogłem dodać mojego postu, więc proszę o moderatora/admina o przeniesienie topiku do odpowiedniego działu. Pozdrawiam.