Php 无法反转属性路径[…]的值:选项[…]不存在或不唯一
从Symfony 2.8更新到3.0并验证操作后,发现作者由于验证错误而无法保存 可能是由于规范更改使选择类型键和值颠倒 我试着把Php 无法反转属性路径[…]的值:选项[…]不存在或不唯一,php,symfony,Php,Symfony,从Symfony 2.8更新到3.0并验证操作后,发现作者由于验证错误而无法保存 可能是由于规范更改使选择类型键和值颠倒 我试着把choice\u标签放在下面的参考中,但没有成功 我还尝试了array\u flip$staff,但显示更改,无法保存 还有别的办法吗 错误 在StaffChoicelist之前 use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList; use Symfony\Component\Form\Ex
choice\u标签
放在下面的参考中,但没有成功
我还尝试了array\u flip
$staff
,但显示更改,无法保存
还有别的办法吗
错误
在StaffChoicelist之前
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\LazyChoiceList;
class StaffChoiceList extends LazyChoiceList
{
public function __construct($staffService, $loginStaff)
{
$this->staffService = $staffService;
$this->loginStaff = $loginStaff;
}
public function setCurrentStaff($currentStaff)
{
$this->currentStaff = $currentStaff;
}
public function loadChoiceList($value = null)
{
// 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;
}
return new ChoiceList($staffs, $staffs);
}
}
后工作人员
使用Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
使用Symfony\Component\Form\Choicelist\ArrayChoiceList;
类StaffChoiceLoader实现ChoiceLoaderInterface
{
公共函数loadChoiceList($value=null)
{
$staff=$this->staffService->getStaffByShop($this->loginStaff->getShop());
if($this->currentStaff&&!array_search($this->currentStaff,$staff)){
$staff[]=$this->currentStaff;
}
返回新的arrayChoiceList($staff,null);
}
公共函数loadChoicesForValues(数组$values,$value=null)
{
//没有预设数据时进行了优化
if(空($choices))
{
返回数组();
}
$values=array();
foreach($choice作为$choice)
{
$values[]=(字符串)$this->loginStaff->getId();
}
返回$value;
}
公共函数loadValuesForChoices(数组$choices,$value=null)
{
//未发送任何内容时进行了优化
if(空($value))
{
返回数组();
}
//从ID获取实体并返回所需的数据
返回$this->staffService->getStaffByShop($this->loginStaff->getShop());
}
}
类型
$authorChoiceList=newstaffchoiceloader($this->staffService,$options['login_staff']);
$builder->add(“作者”),EntityType::类,数组(
“必需”=>true,
“类”=>“AppBundle:Staff”,
“choice_loader”=>$authorChoiceList,
“选择标签”=>函数($value){
返回$value;
},
));
$builder->addEventListener(FormEvents::预设置数据、函数(FormEvent$event)使用($authorChoiceList){
$article=$event->getData();
$authorChoiceList->setCurrentStaff($article->getAuthor());
});
公共函数配置选项(选项解析器$resolver)
{
$resolver->setDefaults(数组(
“验证组”=>函数(FormInterface$表单){
$article=$form->getData();
返回$this->getValidationGroups($article->getArticleStatus());
},
));
}
Staff.php
/**
*_uuu-toString
*
*@返回字符串
*/
公共函数
{
返回$this->staffName;
}
/**
*设置职员姓名
*
*@param string$staffName
*@返回工作人员
*/
公共函数setStaffName($staffName)
{
$this->staffName=$staffName;
退还$this;
}
/**
*获取员工姓名
*
*@返回字符串
*/
公共函数getStaffName()
{
返回$this->staffName;
}
Article.php
/**
*@ORM\manytone(targetEntity=“Staff”)
*@ORM\JoinColumn(name=“author\u id”,referencedColumnName=“id”,nullable=true)
*/
受保护的作者;
/**
*集作者
*
*@param\AppBundle\Model\Entity\Staff$author
*@返回文章
*/
公共函数setAuthor(\AppBundle\Model\Entity\Staff$author=null)
{
$this->author=$author;
退还$this;
}
/**
*获取作者
*
*@return\AppBundle\Model\Entity\Staff
*/
公共函数getAuthor()
{
返回$this->author;
}
尝试过的代码
类型
public function\uuu构造($staffService,array$options=[]))
{
$this->staffService=$staffService;
$this->loginStaff=$options['login_staff'];
$this->currentStaff=$options['login_staff'];
}
错误
试着先简单一点。如果你的ChoiceLoader不起作用,尽量避免它。在得到最少的工作示例后,您可以重构它 仅当您使用QueryBuilder从数据库获取选项时,EntityType才有用 使用ChoiceType,如下例所示:
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use App\Entity\Staff;
class StaffType extends AbstractType
{
public function __construct($staffService)
{
$this->staffService = $staffService;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$currentStaff = $options['currentStaff'];
$loginStaff = $options['loginStaff'];
$staff = $this->staffService->getStaffByShop($loginStaff->getShop());
// If the current staff is not included in the acquired staff (due to transfer etc.), add it to the end
if ($currentStaff && !array_search($currentStaff, $staffs)) {
$staff[] = $currentStaff;
}
$builder->add('author', ChoiceType::class, [
'choices' => $staffs,
'choice_label' => function(Staff $employee, $key) {
return $employee->getName();
}
]);
}
}
人员数组必须包含唯一密钥,例如:
$staff = [
1 => new Staff(1),
2 => new Staff(2),
3 => new Staff(3),
];
使用自定义选项在控制器中生成表单:
$options = [
'currentStaff' => $currentStaff,
'loginStaff' => $loginStaff,
];
$this->createForm(StaffType::class, $data, $options);
要访问$this->staffService,请在表单类型中使用依赖项注入。对于$this->currentStaff和$this->loginStaff,使用$options
对你有用吗?如果没有,请提供$staff数组的结构
$staffs = $this->staffService->getStaffByShop($this->loginStaff->getShop());
我认为这在编码方面是不同的,特别是第二个问题。谢谢您的回答。我试过了,但我在依赖注入方面遇到了麻烦。如何注入
$options
?我已经添加了我尝试过的代码,所以你能检查一下吗?DI只适用于服务。要传递自定义选项,请使用表单类型的buildForm方法中的options参数。检查我编辑的答案。我尝试了,但出现以下错误:类型“object”的值无法转换为有效的数组键。
似乎$staff
是一个对象。当我做了var\u dump
时,它变成如下<代码>数组(17){[0]=>object(AppBundle\Model\Entity\Staff)#650(24){[“id”:protected]=>int(305).这似乎是因为数组的每个项都是StaffEntity的对象。
protected $password;
protected $dbName;
public function __construct ( $UserName, $Password, $DbName ) {
$this->userName = $UserName;
$this->password = $Password;...
{
public function __construct($staffService, $loginStaff)
{
$this->staffService = $staffService;
$this->loginStaff = $loginStaff;
}
public function setCurrentStaff($currentStaff)
{
$this->currentStaff = $currentStaff
}
public function loadChoiceList($value = True)
{
// Get the same shop staff as the login staff
$staffs = $this->staffService->getStaffByShop($this->loginStaff->getShop($staff);
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;
}
{
return new ChoiceList($staffs, $staffs);
} Set choices_as_values to true. If you upgrade you have to change that.
$staffs = $this->staffService->getStaffByShop($this->loginStaff->getShop());
protected $password;
protected $dbName;
public function __construct ( $UserName, $Password, $DbName ) {
$this->userName = $UserName;
$this->password = $Password;...
{
public function __construct($staffService, $loginStaff)
{
$this->staffService = $staffService;
$this->loginStaff = $loginStaff;
}
public function setCurrentStaff($currentStaff)
{
$this->currentStaff = $currentStaff
}
public function loadChoiceList($value = True)
{
// Get the same shop staff as the login staff
$staffs = $this->staffService->getStaffByShop($this->loginStaff->getShop($staff);
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;
}
{
return new ChoiceList($staffs, $staffs);
} Set choices_as_values to true. If you upgrade you have to change that.