Witam wszystkich,
mam pytanie do forumowiczów.
Mianowicie jak wykonać wysłanie formularza podczas gdy encje są ze sobą powiązane.
Mam encję
Equipment
namespace gsm\serviceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="equipment")
*/
class Equipment {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Users", inversedBy="Equipment")
* @ORM\JoinColumn(name="id_user", referencedColumnName="id")
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="Producer", inversedBy="Equipment")
* @ORM\JoinColumn(name="id_producer", referencedColumnName="id")
*/
protected $producer;
/**
* @ORM\ManyToOne(targetEntity="Model", inversedBy="Equipment")
* @ORM\JoinColumn(name="id_model", referencedColumnName="id")
*/
protected $model;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="Equipment")
* @ORM\JoinColumn(name="id_category", referencedColumnName="id")
*/
protected $category;
/**
* @ORM\Column(type="integer")
*/
protected $amount;
public function getId() {
return $this->id;
}
public function getUser() {
return $this->user;
}
public function getProducer() {
return $this->producer;
}
public function getModel() {
return $this->model;
}
public function getCategory() {
return $this->category;
}
public function getAmount() {
return $this->amount;
}
public function setId($id) {
$this->id = $id;
return $this;
}
public function setUser(Users $user) {
$this->user = $user;
return $this;
}
public function setProducer(Producer $producer) {
$this->producer = $producer;
return $this;
}
public function setModel(Model $model) {
$this->model = $model;
return $this;
}
public function setCategory(Category $category) {
$this->category = $category;
return $this;
}
public function setAmount($amount) {
$this->amount = $amount;
return $this;
}
}
Oraz encję
Model
namespace gsm\serviceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="model")
*/
class Model {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function setId($id) {
$this->id = $id;
return $this;
}
public function setName($name) {
$this->name = $name;
return $this;
}
}
W kontrolerze mam kod:
public function createAction($id, Request $request) {
if($id){
$equipment = $this->getDoctrine()
->getRepository('gsmserviceBundle:Equipment')
->find($id);
} else {
$equipment = new Equipment;
}
$form = $this->createFormBuilder($equipment)
->add('user', 'entity', array( 'class' => 'gsm\serviceBundle\Entity\Users',
'choice_label' => 'getId',
'label' => 'Imię i nazwisko: '
))
->add('producer', 'entity', array( 'class' => 'gsm\serviceBundle\Entity\Producer',
'choice_label' => 'getName',
'label' => 'Producent: '
))
->add('category', 'entity', array( 'class' => 'gsm\serviceBundle\Entity\Category',
'choice_label' => 'getName',
'label' => 'Kategoria: '
))
->add('model', 'entity', array( 'class' => 'gsm\serviceBundle\Entity\Model',
'choice_label' => 'getName',
'label' => 'Model: '
))
->add('amount', 'text', array( 'label' => 'Ilość: '
))
->add('createEquipment', 'submit', array( 'label' => 'OK'
))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted()){
$equipment = $form->getData();
$manager = $this->getDoctrine()
->getManager();
$manager->persist($equipment);
$manager->flush();
if ($equipment->getId() > 0) {
}
}
return $this->render('gsmserviceBundle:Equipment:create.html.twig', array('form' => $form->createView()));
}
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.