When I upgraded the system running Symfony3.0 to version 3.4, the following error occured.
Since choice_list
will not be supported due to version upgrade, choices
I changed it to use.
Error Code
Entity of type "AppBundle\Model\Service\StaffService" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?
Code ArticleType.php
// Contributor
$authorChoiceList = array($this->staffService, $options['login_staff']);
$builder->add("author", EntityType::class, array(
"required" => true,
"class" => "AhiSpCommonBundle:Staff",
"choices" => $authorChoiceList,
"placeholder" => "Please select",
));
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($authorChoiceList) {
$article = $event->getData();
$authorChoiceList->setCurrentStaff($article->getAuthor());
});
ChoiceList.php
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
class StaffChoiceList extends LazyChoiceList
{
private $staffService;
private $loginStaff;
private $currentStaff;
public function __construct($staffService, $loginStaff)
{
$this->staffService = $staffService;
$this->loginStaff = $loginStaff;
}
public function setCurrentStaff($currentStaff)
{
$this->currentStaff = $currentStaff;
}
protected function loadChoiceList()
{
//Get the same shop staff as the login staff
$staffs = $this->staffService->getStaffByShop($this->loginStaff->getShop());
// If the current staff is not included in the acquired staff (due to transfer etc.), add it to the end
if ($this->currentStaff && !array_search($this->currentStaff, $staffs)) {
$staffs[] = $this->currentStaff;
}
$factory = new DefaultChoiceListFactory();
return new ChoiceList($staffs, $staffs);
}
}
Version
Cent OS 6.7 PHP 7.3 Symfony 3.4
can't you just use the query_builder
option?
see doc here -> https://symfony.com/doc/current/reference/forms/types/entity.html#using-a-custom-query-for-the-entities
you could implement a custom method in the repository and call it inside you callable
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.