我正在使用PHP框架Symfony 3.3和ORM Doctrine开发一个网站。
我有用户,每个用户都可以提供服务。服务可以由多个用户提供。
除非我想要保留用户的服务列表,否则一切都运行良好。什么都没发生,我没有任何错误。你能看到我的代码中有什么问题吗?
我的用户类(部分内容):
class User implements AdvancedUserInterface, \Serializable
{
public function __construct(){
$this->services = new ArrayCollection(); // Services offered by the user
}
/**
*
* @var Doctrine\Common\Collections\ArrayCollection $services
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Service", inversedBy="users")
* @ORM\JoinTable(name="service_offer")
*
*/
private $services;
function getServices() {
return $this->services;
}
function setServices(ArrayCollection $services) {
$this->services = $services;
return $this;
}
}
我的服务类(部分内容):
class Service
{
/**
* @var integer
*
* @ORM\Column(name="id_service", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idService;
/**
* @var \Doctrine\Common\Collections\ArrayCollection $users
*
* @ManyToMany(targetEntity="AppBundle\Entity\User", mappedBy="services")
*
**/
private $users;
}
在我的控制器中,我有:
class ServiceOfferController extends Controller {
/**
*
* @param EntityManagerInterface $em
*
*
* @Route("/member/ServiceOffer", name="ServiceOffer")
*/
public function serviceOfferAction(Request $request, EntityManagerInterface $em){
//$user = $this->getUser();
$form = $this->createForm(ServiceOfferType::class);
$form->handleRequest($request);
if ($form->isSubmitted()){
// Services ticked in the formular
$services = $form->get('services')->getData();
$user = $this->getUser();
$user->setServices($services);
print_r($user->getServices());
// Persisting in the DB
$em->persist($user);
$em->flush();
}
return $this->render('serviceOffer.html.twig', array(
'form' => $form->createView()
));
}
}
“的print_r($用户> getServices());”返回数据,但表service_offer中没有任何内容。有什么想法吗?
提前致谢。
编辑:
还试图指定列,如:
/**
*
* @var Doctrine\Common\Collections\ArrayCollection $services
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Service", inversedBy="users")
* @ORM\JoinTable(name="service_offer",
* joinColumns={
* @ORM\JoinColumn(name="id_user", referencedColumnName="id_user", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="id_service", referencedColumnName="id_service", onDelete="CASCADE")
* }
* )
*
*/
private $services;
编辑2:
我的service_offer MySQL表是:
CREATE TABLE `service_offer` (
`id_service_offer` INT(11) NOT NULL AUTO_INCREMENT,
`id_user` INT(11) NULL DEFAULT NULL,
`id_service` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id_service_offer`),
INDEX `fk_service_idx` (`id_service`),
INDEX `fk_user_idx` (`id_user`),
CONSTRAINT `fk_service` FOREIGN KEY (`id_service`) REFERENCES `service` (`id_service`),
CONSTRAINT `fk_user` FOREIGN KEY (`id_user`) REFERENCES `user` (`id_user`)
)
答案 0 :(得分:1)
尝试为连接表指定列参数。
/**
*
* @var Doctrine\Common\Collections\ArrayCollection $services
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Service", inversedBy="users")
* @ORM\JoinTable(name="user_service",
* joinColumns={
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="offer_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*
*/
private $services;