I have a problem with inserting data to the database with a one-to-one relationship. The problem is that I can not use FormBuilder. Because the data comes from the form js. User selects the status field from the drop-down list.Now I would like insert returned id to entity orders.
Status Entity:
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="status", type="string", length=30)
*/
private $status;
Order Entity:
/**
* @var int
*
* @OneToOne(targetEntity="AppBundle\Entity\Status", fetch="EAGER",cascade={"persist"})
* @JoinColumn(name="Status_id", referencedColumnName="id")
*/
private $Status;
Service:
$order=new Orders();
$status=new GlobalStatus();
$status->getId(1);
$order->setGlobalStatus($status);
$this->em->persist($order);
$this->em->flush();
Then doctrine tries to add data to the Status table. If I do not try to create a Status instance, I get an error :( Can someone advise me how to add this data?
Status Entity
/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(name="id", type="integer")
*/
protected $id;
/**
* @var Order
*
* @ORM\OneToOne(targetEntity="AppBundle\Entity\Order")
*/
protected $order;
Order Entity:
/**
* @var Status
*
* @ORM\Column(name="status", type="string", length=30)
*/
protected $status;
Your service:
class Foo
{
/**
* @param Status $status
*/
public function bar(Status $status)
{
if (!$status instanceof Status) {
return;
}
$order = new Order(); //Should be singular
$order->setStatus($status); //If it's `global` consider use enumType instead
$this->em->persist($order);
$this->em->flush();
}
}
How to want you to fetch Form Data and bind it to Status
entity?