我正在開發一個PHP框架Symfony 3.3和ORM主義的網站。多對多不堅持
我有用戶,每個用戶都可以提供服務。服務可以由多個用戶提供。
一切運作良好,除非我想堅持用戶的服務列表。什麼都沒有發生,我沒有得到任何錯誤。你能看到我的代碼中有什麼錯誤嗎?
我的MySQL用戶表:
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`)
)
我的MySQL service_offer表:
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`)
)
我的MySQL服務表:
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`)
)
我的用戶實體的一部分:
/**
* @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;
}
我的服務實體類:
<?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;
}
}
如果我做了一個控制器,沒有什麼是數據庫中的堅持:
$user->setServices($services);
$em->persist($user);
$em->flush();
此代碼:
print_r($services);
返回
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)))
而這種代碼:
echo $services[0]->getName();
返回此:
sdfgsdf
任何想法?
在此先感謝! :)
任何人有任何想法? –