我有两个表存储和store_timings,我想将存储时间从mon存储到sun.I创建了两个实体Store.php和Store_timings.php
Store.php
namespace Test\BotBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Store
*
* @ORM\Table(name="store", indexes={@ORM\Index(name="idxReference",
columns={"reference"}), @ORM\Index(name="idxLounges", columns={"name",
"province", "loungePage"})})
* @ORM\Entity
*/
class Store
{
/**
* @var integer
*
* @ORM\Column(name="storeID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $storeID;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=50, nullable=true)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="openinghours", type="blob", length=65535, nullable=true)
*/
private $openinghours;
/**
* @var string
*
* @ORM\Column(name="document", type="blob", length=65535, nullable=true)
*/
private $document;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="Test\BotBundle\Entity\StoreTimings",mappedBy="gameHallOpeningHours",cascade={"persist"})
*/
private $gameHallOpeningHours;
/**
* @var \DateTime
*
* @ORM\Column(name="addedTs", type="datetime", nullable=true)
*/
private $addedts;
/**
* Store constructor.
*/
public function __construct()
{
$this->gameHallOpeningHours = new ArrayCollection();
}
/**
* @return int
*/
public function getStoreId()
{
return $this->storeID;
}
/**
* @param Store $storeId
*/
public function setStoreId(Store $storeId)
{
$this->storeId = $storeId;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getOpeninghours()
{
return $this->openinghours;
}
/**
* @param string $openinghours
*/
public function setOpeninghours($openinghours)
{
$this->openinghours = $openinghours;
}
/**
* @return string
*/
public function getDocument()
{
return $this->document;
}
/**
* @param string $document
*/
public function setDocument($document)
{
$this->document = $document;
}
/**
* @return \DateTime
*/
public function getAddedts()
{
return $this->addedts;
}
/**
* @param \DateTime $addedts
*/
public function setAddedts($addedts)
{
$this->addedts = $addedts;
}
/**
* @return ArrayCollection | Store
*/
public function getGameHallOpeningHours()
{
return $this->gameHallOpeningHours;
}
/**
* @param array $gameHallOpeningHours
*/
public function setGameHallOpeningHours(array $gameHallOpeningHours)
{
$this->gameHallOpeningHours = $gameHallOpeningHours;
}
}
StoreTimings.php
namespace Test\BotBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* StoreTimings
*
* @ORM\Table(name="store_timings")
*/
class StoreTimings
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
*
* @ORM\ManyToOne(targetEntity="store",cascade={"persist"})
* @ORM\JoinColumn(name="gameHallID", referencedColumnName="storeID")
*/
private $gameHallID;
/**
* @var string
*
* @ORM\Column(name="day", type="string", length=11, nullable=true)
*/
private $day;
/**
* @var string
*
* @ORM\Column(name="opening_hours", type="string", length=11, nullable=true)
*/
private $openingHours;
/**
* @var string
*
* @ORM\Column(name="closing_hours", type="string", length=11, nullable=true)
*/
private $closingHours;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return int
*/
public function getGameHallID()
{
return $this->gameHallID;
}
/**
* @param int $gamehallid
*/
public function setGamehallid($gamehallid)
{
$this->gameHallID = $gamehallid;
}
/**
* @return string
*/
public function getDay()
{
return $this->day;
}
/**
* @param string $day
*/
public function setDay($day)
{
$this->day = $day;
}
/**
* @return string
*/
public function getOpeningHours()
{
return $this->openingHours;
}
/**
* @param string $openingHours
*/
public function setOpeningHours($openingHours)
{
$this->openingHours = $openingHours;
}
/**
* @return string
*/
public function getClosingHours()
{
return $this->closingHours;
}
/**
* @param string $closingHours
*/
public function setClosingHours($closingHours)
{
$this->closingHours = $closingHours;
}
}
我能够生成包含来自两个实体的字段的表单,但在保存时我收到了Integrity Constriant Violation。
我做了太多的SO / google了很多。我也是Symfony的新手。
此外,当我在保存到数据库之前尝试打印时,我会得到以下对象
Test\BotBundle\Entity\Store Object
(
[storeID:Test\BotBundle\Entity\Store:private] =>
[name:Test\BotBundle\Entity\Store:private] => ergeg
[address:Test\BotBundle\Entity\Store:private] => Rege
[zip:Test\BotBundle\Entity\Store:private] => rger
[city:Test\BotBundle\Entity\Store:private] => erg
[province:Test\BotBundle\Entity\Store:private] => gerg
[phone:Test\BotBundle\Entity\Store:private] => rg
[reference:Test\BotBundle\Entity\Store:private] =>
[email:Test\BotBundle\Entity\Store:private] =>
[coords:Test\BotBundle\Entity\Store:private] =>
[loungepage:Test\BotBundle\Entity\Store:private] => 1
[hasvouchers:Test\BotBundle\Entity\Store:private] => 1
[cashoutallowed:Test\BotBundle\Entity\Store:private] => 1
[hasparking:Test\BotBundle\Entity\Store:private] => 1
[hashotkeys:Test\BotBundle\Entity\Store:private] => 1
[openinghours:Test\BotBundle\Entity\Store:private] =>
[document:Test\BotBundle\Entity\Store:private] =>
[gameHallOpeningHours:Test\BotBundle\Entity\Store:private] => Array
(
[0] => Test\BotBundle\Entity\StoreTimings Object
(
[id:Test\BotBundle\Entity\StoreTimings:private] =>
[gameHallID:Test\BotBundle\Entity\StoreTimings:private]
=>
[day:Test\BotBundle\Entity\StoreTimings:private] => 1
[openingHours:Test\BotBundle\Entity\StoreTimings:private] =>
[closingHours:Test\BotBundle\Entity\StoreTimings:private] =>
)
)
[addedts:Test\BotBundle\Entity\Store:private] =>
)
StoreType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('address')
->add('zip')
->add('city')
->add('province')
->add('phone')
->add('loungepage')
->add('hasvouchers')
->add('cashoutallowed')
->add('hasparking')
->add('hashotkeys')
;
$builder->add('store_id','collection',[
'type' => 'store_timings',
'property_path' => 'game_hall_opening_hours',
'allow_add' => true,
]);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Store::class,
));
}
StoreTimingsType.php
class StoreTimingsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('day','choice',[
'choices' => [
1=>'Monday',
2=>'Tuesday',
3=>'Wednesday',
4=>'Thursday',
5=>'Friday',
6=>'Saturday',
7=>'Sunday'],
])
->add('opening_hours')
->add('closingHours');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => StoreTimings::class,
]);
}
答案 0 :(得分:1)
在您的表单中,您需要在formBuilder中为gameHallID
添加一个类似于此关系的setter
->add('brand', EntityType::class, array(
// query choices from this entity
'class' => 'YourBundle:YourClass',
))
或者您可以在字段中创建nullable=true
,如下所示:
@ORM\JoinColumn(name="gameHallID", referencedColumnName="storeID", nullable=true)
答案 1 :(得分:0)
May be i couldn't explain the question properly. But i somehow with experimentation found that below solution worked:
/**
* @param array $gameHallOpeningHours
*/
public function setGameHallOpeningHours(array $gameHallOpeningHours)
{
foreach($gameHallOpeningHours as $timeSlots){
$timeSlots->setGameHall($this);
}
$this->gameHallOpeningHours = $gameHallOpeningHours;
}
** GameHall is a private variable in the StoreTimings.php