I want to create a new task from Command in a custom Bundle. But having troubles with setting the task status. I took the fixture from Backend developer's guide and adapted it to Command
$task = new Task();
$task->setSubject('Important task');
$task->setDescription('This is an important task');
$defaultPriority = $this->doctrine->getRepository(TaskPriority::class)->find('normal');
if ($defaultPriority) {
$task->setTaskPriority($defaultPriority);
}
$task->setOwner($taskDataArray['user']);
$task->setOrganization($this->getOrganization());
$this->getEntityManager(Task::class)->persist($task);
$this->getEntityManager(Task::class)->flush();
The database record is created, but by default status_id
field is empty.
Without status it isn't shown in data grid.
The status has type AbstractEnumValue
. The method which sets the status is defined in Model as * @method Task setStatus(AbstractEnumValue $status)
How to set up the status correctly in CRUD operation?
Thanks ahead.
Status is a enum field. Internally enums are entities with autogenerated class names. To work with the enum entity, first, you have to generate its name, then you can access it using doctrine, as a regular entity.
// generate enum entity class name by the enum code
$statusClass = ExtendHelper::buildEnumValueClassName('task_status');
// find existing status entity
$statusOpen = $this->doctrine->find($statusClass, 'open');
// assign status to the task
$task->setStatus($statusOpen);
See the reference at the OroEntityExtendBundle documentation.