问题
I'm developing a website with the PHP framework Symfony 3.3 and the ORM Doctrine.
I have users, and each user can offer services. A service can be offers by more than one user.
Everything is working well, except when I want to persist a services list of a user. Nothing happens and I don't get any error. Can you see anything that is wrong in my code?
My MySQL user table:
CREATE TABLE `user` (
`id_user` INT(11) NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(255) NOT NULL,
`surname` VARCHAR(255) NOT NULL,
`address` VARCHAR(255) NOT NULL,
`zip` VARCHAR(4) NOT NULL,
`city` VARCHAR(255) NOT NULL,
`phone_number` VARCHAR(12) NULL DEFAULT NULL,
`mobile_number` VARCHAR(12) NULL DEFAULT NULL,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`birth_date` DATE NOT NULL,
`is_verified` TINYINT(4) NULL DEFAULT NULL,
`id_gender` INT(11) NOT NULL,
PRIMARY KEY (`id_user`),
UNIQUE INDEX `e-mail_UNIQUE` (`email`),
UNIQUE INDEX `id_utilisateur_UNIQUE` (`id_user`),
INDEX `fk_id_gender_idx` (`id_gender`),
CONSTRAINT `fk_id_gender` FOREIGN KEY (`id_gender`) REFERENCES `gender` (`id_gender`)
)
My MySQL service_offer table:
CREATE TABLE `service_offer` (
`id_service_offer` INT(11) NOT NULL AUTO_INCREMENT,
`id_user` INT(11) NOT NULL,
`id_service` INT(11) NOT NULL,
PRIMARY KEY (`id_service_offer`),
UNIQUE INDEX `id_user_UNIQUE` (`id_user`),
INDEX `fk_service_idx` (`id_service`),
CONSTRAINT `fk_service` FOREIGN KEY (`id_service`) REFERENCES `service` (`id_service`),
CONSTRAINT `fk_user` FOREIGN KEY (`id_user`) REFERENCES `user` (`id_user`)
)
My MySQL service table:
CREATE TABLE `service` (
`id_service` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`description` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id_service`),
UNIQUE INDEX `id_service_UNIQUE` (`id_service`)
)
A part of my user entity:
/**
* @var integer
*
* @ORM\Column(name="id_user", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idUser;
/**
*
* @ORM\ManyToMany(targetEntity="Service")
* @ORM\JoinTable(name="service_offer",
* joinColumns={@ORM\JoinColumn(name="id_user", referencedColumnName="id_user")},
* inverseJoinColumns={@ORM\JoinColumn(name="id_service", referencedColumnName="id_service")}
* )
*/
private $services;
function getServices() {
return $this->services;
}
function setServices($services) {
$this->services = $services;
return $this;
}
My Service entity class:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Service
*
* @ORM\Table(name="service", uniqueConstraints={@ORM\UniqueConstraint(name="id_service_UNIQUE", columns={"id_service"})}, indexes={@ORM\Index(name="fk_user_idx", columns={"user"})})
* @ORM\Entity
*/
class Service
{
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255, nullable=true)
*/
private $description;
/**
* @var integer
*
* @ORM\Column(name="id_service", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idService;
/**
* Set name
*
* @param string $name
*
* @return Service
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
*
* @return Service
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Get idService
*
* @return integer
*/
public function getIdService()
{
return $this->idService;
}
}
And if I'm doing that in a controller, nothing is persisting in the database:
$user->setServices($services);
$em->persist($user);
$em->flush();
This code:
print_r($services);
returns
Doctrine\Common\Collections\ArrayCollection Object ( [elements:Doctrine\Common\Collections\ArrayCollection:private] => Array ( [0] => AppBundle\Entity\Service Object ( [name:AppBundle\Entity\Service:private] => sdfgsdf [description:AppBundle\Entity\Service:private] => sdfgsdf [idService:AppBundle\Entity\Service:private] => 2 ) ) )
And this code:
echo $services[0]->getName();
returns this:
sdfgsdf
Any idea?
Thanks in advance! :)
回答1:
Can you try persisting the services as well? For example:
$user->setServices($services);
$em->persist($user);
$em->persist($services);
$em->flush();
回答2:
Finally found what was the problem!!! :) :)
I had XML files in my "\src\AppBundle\Resources\config\doctrine" folder because I generated my entity classes from an existing database. But the Many to many join wasn't generated automatically, so I had to add it manually.
Everytime I launched this command:
php bin/console doctrine:schema:update --force
I had the information that everything was up to date. But actually, Symfony used the information stored in the XML files and not in the annotations....
So, I deleted the XML files, updated my schema again and now it's working. I saw that Symfony updated the table service_offer and deleted the column "id_service_offer" which is actually not needed.
来源:https://stackoverflow.com/questions/46014746/many-to-many-doesnt-persist