我有一些错误:
关联类型“AdBundle \ Entity \ AdLocation”的预期值 字段“AdBundle \ Entity \ AdBase#$ location”,改为“字符串”。
我是否需要使用DataTransformer?为什么我不能在没有其他操作的情况下仅使用EntityType字段?
AdBase实体:
<?php
namespace AdBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* AdBase
*
* @ORM\Table(name="ad_base")
* @ORM\Entity(repositoryClass="AdBundle\Repository\AdBaseRepository")
* @ORM\HasLifecycleCallbacks()
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({
"flat" = "AdFlat"
* })
*/
abstract class AdBase
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="author", type="string", length=255)
*/
private $author;
/**
* @ORM\ManyToOne(targetEntity="AdBundle\Entity\AdCategory")
*/
private $category;
/**
* @ORM\ManyToOne(targetEntity="AdBundle\Entity\AdLocation")
*/
private $location;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
* @Assert\NotBlank(message="Not specified Title")
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="text", nullable=true)
* @Assert\NotBlank(message="Not specified Description")
*/
private $description;
/**
* @ORM\OneToMany(targetEntity="AdBundle\Entity\AdPhoto", mappedBy="ad")
*/
private $photos;
/**
* @ORM\Column(type="float")
* @Assert\NotBlank()
*/
private $price;
/**
* @ORM\ManyToOne(targetEntity="AdBundle\Entity\AdPriceType")
*/
private $priceType;
/**
* @var \DateTime
*
* @ORM\Column(name="createdAt", type="datetime")
*/
private $createdAt;
/**
* @var string
*
* @ORM\Column(name="updatedAt", type="datetime")
*/
private $updatedAt;
/**
* @var bool
*
* @ORM\Column(name="visible", type="boolean")
*/
private $visible = false;
/**
* @ORM\Column(type="boolean")
*/
private $active = true;
/**
* Set id
*
* @param integer $id
*
* @return $this
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set author
*
* @param string $author
*
* @return AdBase
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* @return mixed
*/
public function getCategory()
{
return $this->category;
}
/**
* @param mixed $category
*/
public function setCategory($category)
{
$this->category = $category;
}
/**
* @return mixed
*/
public function getLocation()
{
return $this->location;
}
/**
* @param mixed $location
*/
public function setLocation($location)
{
$this->location = $location;
}
/**
* Set title
*
* @param string $title
*
* @return AdBase
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* @param string $description
*
* @return AdBase
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set photos
*
* @param string $photos
*
* @return AdBase
*/
public function setPhotos($photos)
{
$this->photos = $photos;
return $this;
}
/**
* Get photos
*
* @return string
*/
public function getPhotos()
{
return $this->photos;
}
/**
* @return mixed
*/
public function getPrice()
{
return $this->price;
}
/**
* @param mixed $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* @return mixed
*/
public function getPriceType()
{
return $this->priceType;
}
/**
* @param mixed $priceType
*/
public function setPriceType($priceType)
{
$this->priceType = $priceType;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
*
* @return AdBase
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param string $updatedAt
*
* @return AdBase
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return string
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set visible
*
* @param boolean $visible
*
* @return AdBase
*/
public function setVisible($visible)
{
$this->visible = $visible;
return $this;
}
/**
* Get visible
*
* @return bool
*/
public function getVisible()
{
return $this->visible;
}
/**
* @return mixed
*/
public function getActive()
{
return $this->active;
}
/**
* @param mixed $active
*/
public function setActive($active)
{
$this->active = $active;
}
/**
* @ORM\PrePersist()
*/
public function prePersist()
{
$this->author = 'voodoo';
$this->createdAt = new \DateTime('now', new \DateTimeZone('UTC'));
$this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
$this->location = 'Donetsk';
}
/**
* @ORM\PreUpdate()
*/
public function preUpdate()
{
$this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
}
}
AdFlat实体:
<?php
namespace AdBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* AdFlat
*
* @ORM\Table(name="ad_flat")
* @ORM\Entity(repositoryClass="AdBundle\Repository\AdFlatRepository")
*/
class AdFlat extends AdBase
{
/**
* @var integer
*
* @ORM\Column(type="integer")
* @Assert\NotBlank(message="ad_flat.rooms.not_blank")
*/
private $rooms;
/**
* @var float
*
* @ORM\Column(name="square", type="float", nullable=true)
*/
private $square;
/**
* @var float
*
* @ORM\Column(name="liveSquare", type="float", nullable=true)
*/
private $liveSquare;
/**
* @var float
*
* @ORM\Column(type="float", nullable=true)
*/
private $kitchenSquare;
/**
* @var int
*
* @ORM\Column(name="floor", type="integer")
*/
private $floor;
/**
* @var int
*
* @ORM\Column(name="floors", type="integer")
*/
private $floors;
/**
* @var string
*
* @ORM\ManyToOne(targetEntity="AdBundle\Entity\AdWallType")
*/
private $wallType;
/**
* @ORM\ManyToOne(targetEntity="AdBundle\Entity\AdWCType")
*/
private $wcType;
/**
* @return mixed
*/
public function getRooms()
{
return $this->rooms;
}
/**
* @param mixed $rooms
*/
public function setRooms($rooms)
{
$this->rooms = $rooms;
}
/**
* Set square
*
* @param float $square
*
* @return AdFlat
*/
public function setSquare($square)
{
$this->square = $square;
return $this;
}
/**
* Get square
*
* @return float
*/
public function getSquare()
{
return $this->square;
}
/**
* Set liveSquare
*
* @param float $liveSquare
*
* @return AdFlat
*/
public function setLiveSquare($liveSquare)
{
$this->liveSquare = $liveSquare;
return $this;
}
/**
* Get liveSquare
*
* @return float
*/
public function getLiveSquare()
{
return $this->liveSquare;
}
/**
* @return float
*/
public function getKitchenSquare()
{
return $this->kitchenSquare;
}
/**
* @param float $kitchenSquare
*/
public function setKitchenSquare($kitchenSquare)
{
$this->kitchenSquare = $kitchenSquare;
}
/**
* Set floor
*
* @param integer $floor
*
* @return AdFlat
*/
public function setFloor($floor)
{
$this->floor = $floor;
return $this;
}
/**
* Get floor
*
* @return int
*/
public function getFloor()
{
return $this->floor;
}
/**
* Set floors
*
* @param integer $floors
*
* @return AdFlat
*/
public function setFloors($floors)
{
$this->floors = $floors;
return $this;
}
/**
* Get floors
*
* @return int
*/
public function getFloors()
{
return $this->floors;
}
/**
* Set wallType
*
* @param string $wallType
*
* @return AdFlat
*/
public function setWallType($wallType)
{
$this->wallType = $wallType;
return $this;
}
/**
* Get wallType
*
* @return string
*/
public function getWallType()
{
return $this->wallType;
}
/**
* Set wcType
*
* @param string $wcType
*
* @return AdFlat
*/
public function setWcType($wcType)
{
$this->wcType = $wcType;
return $this;
}
/**
* Get wcType
*
* @return string
*/
public function getWcType()
{
return $this->wcType;
}
}
AdLocation实体:
<?php
namespace AdBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* AdLocation
*
* @ORM\Table(name="ad_location")
* @ORM\Entity(repositoryClass="AdBundle\Repository\AdLocationRepository")
* @ORM\HasLifecycleCallbacks()
*/
class AdLocation
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="AdBundle\Entity\AdLocation", mappedBy="parent")
*/
private $children = null;
/**
* @ORM\ManyToOne(targetEntity="AdBundle\Entity\AdLocation", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent = null;
/**
* @ORM\Column(type="integer")
*/
private $level = 0;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="string")
*/
private $locationType;
/**
* @ORM\Column(type="integer")
*/
private $weight = 0;
/**
* @var bool
*
* @ORM\Column(name="visible", type="boolean")
*/
private $visible = true;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set children
*
* @param string $children
*
* @return AdLocation
*/
public function setChildren($children)
{
$this->children = $children;
return $this;
}
/**
* Get children
*
* @return string
*/
public function getChildren()
{
return $this->children;
}
/**
* Set parent
*
* @param string $parent
*
* @return AdLocation
*/
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return string
*/
public function getParent()
{
return $this->parent;
}
/**
* @return mixed
*/
public function getLevel()
{
return $this->level;
}
/**
* @param mixed $level
*/
public function setLevel($level)
{
$this->level = $level;
}
/**
* Set title
*
* @param string $title
*
* @return AdLocation
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @return mixed
*/
public function getLocationType()
{
return $this->locationType;
}
/**
* @param mixed $locationType
*/
public function setLocationType($locationType)
{
$this->locationType = $locationType;
}
/**
* @return mixed
*/
public function getWeight()
{
return $this->weight;
}
/**
* @param mixed $weight
*/
public function setWeight($weight)
{
$this->weight = $weight;
}
/**
* Set visible
*
* @param boolean $visible
*
* @return AdLocation
*/
public function setVisible($visible)
{
$this->visible = $visible;
return $this;
}
/**
* Get visible
*
* @return bool
*/
public function getVisible()
{
return $this->visible;
}
/**
* @ORM\PrePersist()
*/
public function prePersist()
{
if (null !== $this->getParent()) {
$this->setLevel($this->getParent()->getLevel() + 1);
}
}
}
在控制器中:
public function addAction(Request $request)
{
$categoryId = $request->get('category_id');
if (!$categoryId) {
return $this->renderError('Unknown Ad Category');
}
$category = $this->getDoctrine()->getRepository('AdBundle:AdCategory')->find($categoryId);
if (null === $category) {
return $this->renderError('You cannot create Ad for this category');
}
if (null === $category->getEntity()) {
return $this->renderError('You cannot create Ad for category ' . $category->getTitle());
}
$adEntity = $this->getEntity($category->getEntity());
$adForm = $this->getForm($category->getEntity());
$form = $this->createForm($adForm, $adEntity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$photosDir = $this->getParameter('dir_ad_photo');
foreach ($form['photos']->getData() as $photo) {
$adPhoto = new AdPhoto($photo, $this->get('kernel')->getRootDir() . '/../' . $photosDir);
$adPhoto->upload();
$adPhoto->setAd($adEntity);
$em->persist($adPhoto);
}
$adEntity->setCategory($category);
$em->persist($adEntity);
$em->flush();
return $this->redirectToRoute('ad_bundle_ad_view', array(
'id' => $adEntity->getId()
));
}
return $this->render('AdBundle:Ad:add-ad-flat.html.twig', array(
'form' => $form->createView(),
'adType' => $category->getTitle()
));
}
private function getEntity($className)
{
$entityName = 'AdBundle\Entity\\' . $className;
return new $entityName();
}
private function getForm($className)
{
$formName = 'AdBundle\Form\Type\\' . $className . 'Type';
return new $formName();
}
答案 0 :(得分:0)
哦,对不起我的问题。在第一个实体版本中,$ location属性是字符串,在prePersist操作中,我执行:
/**
* @ORM\PrePersist()
*/
public function prePersist()
{
$this->author = 'voodoo';
$this->createdAt = new \DateTime('now', new \DateTimeZone('UTC'));
$this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
$this->location = 'Donetsk';
}