简体   繁体   English

Symfony,控制器中的ManyToMany方法

[英]Symfony, ManyToMany setters and getters in controller

I have little problem with ManyToMany setters and getters 我对ManyToMany setter和getter没什么问题

Company.php Company.php

/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="companies")
*/
protected $employees;

User.php user.php的

    /**
    * @ORM\ManyToMany(targetEntity="Company", inversedBy="employees")
    * @ORM\JoinColumn(name="company_id")
    */
protected $companies;

then in Controller I try set user value into company 然后在Controller中,我尝试将用户价值设置为公司

$company->setEmployees($company->getEmployees($user));

I keep getting 我不断

"Attempted to call an undefined method named "setEmployees" of class ..\\Entity\\Company". “试图调用名为.. \\ Entity \\ Company的未定义方法“ setEmployees”。 Did you mean to call eg "addEmployees" or "getEmployees"? 您是说打电话给“ addEmployees”还是“ getEmployees”?

Can someone help me out? 有人可以帮我吗? Thanks! 谢谢!

That is because of your ManyToMany relation between your two entities. 那是由于两个实体之间的ManyToMany关系。 By default, the getters & setters will become setEmployee() (singular, because it is used to add a single Employee object) and addEmployees() (plural, because it is used to add an array of Employees). 默认情况下,getter和setter分别为setEmployee() (单数,因为它用于添加单个Employee对象)和addEmployees() (复数,因为用于添加Employees数组)。 Check out if everything is spelled correctly and respects this principle. 检查所有内容的拼写是否正确,并遵守此原则。

Given that $user seems to be a singular user, I'd make the following change : 鉴于$user似乎是一个单一用户,我将进行以下更改:

$company->setEmployee($company->getEmployees($user));

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM