gpt4 book ai didi

php - Zend 2-理论如何为三个实体一对多插入?

转载 作者:行者123 更新时间:2023-11-29 23:07:11 25 4
gpt4 key购买 nike

首先,对不起我的英语不好。

我的场景有三个表(有自己的ID)

Concurso (id 130)
Country Canada (id 1)
Languages French (id 20)
English (id 40)
Country USA (id 2)
Language English (id 40)
Spanish (id 33)
Country Italy (id 5)
Language Italian (id 99)


我们的目标是将这些字段放入表concursoCountry中

con_id(concurso_id)
cou_id(country_id)
lan_id(language_id)

我将插入这些记录

130 1 20
130 1 40
130 2 40
130 2 33
130 5 99


所有表都是与表Concurso,国家和语言正确相关的FK,并且使用我将要发布的代码,我只能在表ConcursoCountry中插入一条记录。

我想我只能同时对所有记录进行一次持久化和刷新操作,但是我不知道该怎么做。

这是我的代码,在此先感谢您的任何建议

public function create($params) { 
//echo "<pre>";
//print_r($params);

$stringCountry = "cou_id";
$arrayIdLanguage = array();

$concurso = new $this->entity;
$concursoCountry = new $this->entityConcursoCountry;

$host = new \Application\Model\DB\Host($this->em);
$concurso->setHos($host->find(1));

$image = new \Application\Model\DB\Image($this->em);
$concurso->setConIma($image->find(1));

$concurso->setConName($params['con_name']);
$concurso->setConDescription($params['con_description']);
$concurso->setConTitle($params['con_name']);

... other information fields...

$this->em->persist($concurso);
$this->em->flush();

$concursoCountry->setCon($concurso);

foreach ($params as $keyCounty=>$valueCountry) {
$posCounntry = strpos($keyCounty, $stringCountry);

if($posCounntry === false){
//
}else{
$country = new \Application\Model\DB\Country($this->em);
$concursoCountry->setCou($country->find($params[$keyCounty]));

$piecesCountry = explode("_", $keyCounty);
$indexCountry = $piecesCountry[2];

$stringLanguage = "lan_id_".$indexCountry;

foreach ($params as $keyLan=>$valueLan) {
$posLang = strpos($keyLan, $stringLanguage);

if($posLang === false){
//
}else{

$language = new \Application\Model\DB\Language($this->em);
$concursoCountry->setLan($language->find($params[$keyLan]));
$this->em->persist($concursoCountry);
$this->em->flush();
}
}
}
}

return true;
}
}


这是Entity / Concurso.php
(有几个信息字段不是必需的



命名空间Application \ Entity;

使用Doctrine \ ORM \ Mapping作为ORM;

/**
* Concurso
*
* @ORM\Table(name="concurso", indexes={@ORM\Index(name="fk_concurso_host_idx", columns={"hos_id"}), @ORM\Index(name="fk_concurso_image_idx", columns={"con_ima_id"})})
* @ORM\Entity
*/
class Concurso
{
/**
* @var integer
*
* @ORM\Column(name="con_id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $conId;

/**
* @var string
*
* @ORM\Column(name="con_name", type="string", length=45, precision=0, scale=0, nullable=false, unique=false)
*/
private $conName;

/**
* @var string
*
* @ORM\Column(name="con_description", type="string", length=250, precision=0, scale=0, nullable=false, unique=false)
*/
private $conDescription;

/**
* @var string
*
* @ORM\Column(name="con_title", type="string", length=45, precision=0, scale=0, nullable=false, unique=false)
*/
private $conTitle;

/**
* @var string
*
* @ORM\Column(name="con_template_header", type="string", length=20, precision=0, scale=0, nullable=false, unique=false)
*/
private $conTemplateHeader;

/**
* @var string
*
* @ORM\Column(name="con_template_footer", type="string", length=20, precision=0, scale=0, nullable=false, unique=false)
*/
private $conTemplateFooter;

/**
* @var string
*
* @ORM\Column(name="con_version", type="string", length=45, precision=0, scale=0, nullable=false, unique=false)
*/
private $conVersion;

/**
* @var string
*
* @ORM\Column(name="con_email_notice", type="string", length=45, precision=0, scale=0, nullable=false, unique=false)
*/
private $conEmailNotice;

/**
* @var integer
*
* @ORM\Column(name="con_conf_res_ent", type="integer", precision=0, scale=0, nullable=false, unique=false)
*/
private $conConfResEnt;

/**
* @var boolean
*
* @ORM\Column(name="con_conf_uni_res_ent", type="boolean", precision=0, scale=0, nullable=false, unique=false)
*/
private $conConfUniResEnt;

/**
* @var integer
*
* @ORM\Column(name="con_conf_uni_res_job", type="integer", precision=0, scale=0, nullable=false, unique=false)
*/
private $conConfUniResJob;

/**
* @var \Application\Entity\Host
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Host")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="hos_id", referencedColumnName="id_host", nullable=true)
* })
*/
private $hos;

/**
* @var \Application\Entity\Image
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Image")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="con_ima_id", referencedColumnName="ima_id", nullable=true)
* })
*/
private $conIma;

/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Application\Entity\Currency", inversedBy="con")
* @ORM\JoinTable(name="concurso_currency",
* joinColumns={
* @ORM\JoinColumn(name="con_id", referencedColumnName="con_id", nullable=true)
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="cur_id", referencedColumnName="cur_id", nullable=true)
* }
* )
*/
private $cur;

/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Application\Entity\Module", inversedBy="con")
* @ORM\JoinTable(name="concurso_module",
* joinColumns={
* @ORM\JoinColumn(name="con_id", referencedColumnName="con_id", nullable=true)
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="mod_id", referencedColumnName="mod_id", nullable=true)
* }
* )
*/
private $mod;

/**
* Constructor
*/
public function __construct()
{
$this->cur = new \Doctrine\Common\Collections\ArrayCollection();
$this->mod = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
* Get conId
*
* @return integer
*/
public function getConId()
{
return $this->conId;
}

/**
* Set conName
*
* @param string $conName
* @return Concurso
*/
public function setConName($conName)
{
$this->conName = $conName;

return $this;
}

/**
* Get conName
*
* @return string
*/
public function getConName()
{
return $this->conName;
}

/**
* Set conDescription
*
* @param string $conDescription
* @return Concurso
*/
public function setConDescription($conDescription)
{
$this->conDescription = $conDescription;

return $this;
}

/**
* Get conDescription
*
* @return string
*/
public function getConDescription()
{
return $this->conDescription;
}

/**
* Set conTitle
*
* @param string $conTitle
* @return Concurso
*/
public function setConTitle($conTitle)
{
$this->conTitle = $conTitle;

return $this;
}

/**
* Get conTitle
*
* @return string
*/
public function getConTitle()
{
return $this->conTitle;
}

/**
* Set conTemplateHeader
*
* @param string $conTemplateHeader
* @return Concurso
*/
public function setConTemplateHeader($conTemplateHeader)
{
$this->conTemplateHeader = $conTemplateHeader;

return $this;
}

/**
* Get conTemplateHeader
*
* @return string
*/
public function getConTemplateHeader()
{
return $this->conTemplateHeader;
}

/**
* Set conTemplateFooter
*
* @param string $conTemplateFooter
* @return Concurso
*/
public function setConTemplateFooter($conTemplateFooter)
{
$this->conTemplateFooter = $conTemplateFooter;

return $this;
}

/**
* Get conTemplateFooter
*
* @return string
*/
public function getConTemplateFooter()
{
return $this->conTemplateFooter;
}

/**
* Set conVersion
*
* @param string $conVersion
* @return Concurso
*/
public function setConVersion($conVersion)
{
$this->conVersion = $conVersion;

return $this;
}

/**
* Get conVersion
*
* @return string
*/
public function getConVersion()
{
return $this->conVersion;
}

/**
* Set conEmailNotice
*
* @param string $conEmailNotice
* @return Concurso
*/
public function setConEmailNotice($conEmailNotice)
{
$this->conEmailNotice = $conEmailNotice;

return $this;
}

/**
* Get conEmailNotice
*
* @return string
*/
public function getConEmailNotice()
{
return $this->conEmailNotice;
}

/**
* Set conConfResEnt
*
* @param integer $conConfResEnt
* @return Concurso
*/
public function setConConfResEnt($conConfResEnt)
{
$this->conConfResEnt = $conConfResEnt;

return $this;
}

/**
* Get conConfResEnt
*
* @return integer
*/
public function getConConfResEnt()
{
return $this->conConfResEnt;
}

/**
* Set conConfUniResEnt
*
* @param boolean $conConfUniResEnt
* @return Concurso
*/
public function setConConfUniResEnt($conConfUniResEnt)
{
$this->conConfUniResEnt = $conConfUniResEnt;

return $this;
}

/**
* Get conConfUniResEnt
*
* @return boolean
*/
public function getConConfUniResEnt()
{
return $this->conConfUniResEnt;
}

/**
* Set conConfUniResJob
*
* @param integer $conConfUniResJob
* @return Concurso
*/
public function setConConfUniResJob($conConfUniResJob)
{
$this->conConfUniResJob = $conConfUniResJob;

return $this;
}

/**
* Get conConfUniResJob
*
* @return integer
*/
public function getConConfUniResJob()
{
return $this->conConfUniResJob;
}

/**
* Set hos
*
* @param \Application\Entity\Host $hos
* @return Concurso
*/
public function setHos(\Application\Entity\Host $hos = null)
{
$this->hos = $hos;

return $this;
}

/**
* Get hos
*
* @return \Application\Entity\Host
*/
public function getHos()
{
return $this->hos;
}

/**
* Set conIma
*
* @param \Application\Entity\Image $conIma
* @return Concurso
*/
public function setConIma(\Application\Entity\Image $conIma = null)
{
$this->conIma = $conIma;

return $this;
}

/**
* Get conIma
*
* @return \Application\Entity\Image
*/
public function getConIma()
{
return $this->conIma;
}

/**
* Add cur
*
* @param \Application\Entity\Currency $cur
* @return Concurso
*/
public function addCur(\Application\Entity\Currency $cur)
{
$this->cur[] = $cur;

return $this;
}

/**
* Remove cur
*
* @param \Application\Entity\Currency $cur
*/
public function removeCur(\Application\Entity\Currency $cur)
{
$this->cur->removeElement($cur);
}

/**
* Get cur
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCur()
{
return $this->cur;
}

/**
* Add mod
*
* @param \Application\Entity\Module $mod
* @return Concurso
*/
public function addMod(\Application\Entity\Module $mod)
{
$this->mod[] = $mod;

return $this;
}

/**
* Remove mod
*
* @param \Application\Entity\Module $mod
*/
public function removeMod(\Application\Entity\Module $mod)
{
$this->mod->removeElement($mod);
}

/**
* Get mod
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMod()
{
return $this->mod;
}
}


实体Language.php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Language
*
* @ORM\Table(name="language")
* @ORM\Entity
*/
class Language
{
/**
* @var integer
*
* @ORM\Column(name="lan_id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $lanId;

/**
* @var string
*
* @ORM\Column(name="code", type="string", length=2, precision=0, scale=0, nullable=false, unique=false)
*/
private $code;

/**
* @var string
*
* @ORM\Column(name="name", type="string", length=45, precision=0, scale=0, nullable=false, unique=false)
*/
private $name;

/**
* @var string
*
* @ORM\Column(name="charset", type="string", length=45, precision=0, scale=0, nullable=false, unique=false)
*/
private $charset;

/**
* @var boolean
*
* @ORM\Column(name="position", type="boolean", precision=0, scale=0, nullable=false, unique=false)
*/
private $position;

/**
* @var boolean
*
* @ORM\Column(name="main", type="boolean", precision=0, scale=0, nullable=false, unique=false)
*/
private $main;

/**
* @var boolean
*
* @ORM\Column(name="active", type="boolean", precision=0, scale=0, nullable=false, unique=false)
*/
private $active;


/**
* Get lanId
*
* @return integer
*/
public function getLanId()
{
return $this->lanId;
}

/**
* Set code
*
* @param string $code
* @return Language
*/
public function setCode($code)
{
$this->code = $code;

return $this;
}

/**
* Get code
*
* @return string
*/
public function getCode()
{
return $this->code;
}

/**
* Set name
*
* @param string $name
* @return Language
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Set charset
*
* @param string $charset
* @return Language
*/
public function setCharset($charset)
{
$this->charset = $charset;

return $this;
}

/**
* Get charset
*
* @return string
*/
public function getCharset()
{
return $this->charset;
}

/**
* Set position
*
* @param boolean $position
* @return Language
*/
public function setPosition($position)
{
$this->position = $position;

return $this;
}

/**
* Get position
*
* @return boolean
*/
public function getPosition()
{
return $this->position;
}

/**
* Set main
*
* @param boolean $main
* @return Language
*/
public function setMain($main)
{
$this->main = $main;

return $this;
}

/**
* Get main
*
* @return boolean
*/
public function getMain()
{
return $this->main;
}

/**
* Set active
*
* @param boolean $active
* @return Language
*/
public function setActive($active)
{
$this->active = $active;

return $this;
}

/**
* Get active
*
* @return boolean
*/
public function getActive()
{
return $this->active;
}
}


实体国家

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Country
*
* @ORM\Table(name="country")
* @ORM\Entity
*/
class Country
{
/**
* @var integer
*
* @ORM\Column(name="cou_id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $couId;

/**
* @var string
*
* @ORM\Column(name="name", type="string", length=150, precision=0, scale=0, nullable=false, unique=false)
*/
private $name;

/**
* @var string
*
* @ORM\Column(name="code", type="string", length=3, precision=0, scale=0, nullable=false, unique=false)
*/
private $code;

/**
* @var string
*
* @ORM\Column(name="flag", type="string", length=45, precision=0, scale=0, nullable=false, unique=false)
*/
private $flag;

/**
* @var string
*
* @ORM\Column(name="geoip", type="string", length=45, precision=0, scale=0, nullable=false, unique=false)
*/
private $geoip;


/**
* Get couId
*
* @return integer
*/
public function getCouId()
{
return $this->couId;
}

/**
* Set name
*
* @param string $name
* @return Country
*/
public function setName($name)
{
$this->name = $name;

return $this;
}

/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Set code
*
* @param string $code
* @return Country
*/
public function setCode($code)
{
$this->code = $code;

return $this;
}

/**
* Get code
*
* @return string
*/
public function getCode()
{
return $this->code;
}

/**
* Set flag
*
* @param string $flag
* @return Country
*/
public function setFlag($flag)
{
$this->flag = $flag;

return $this;
}

/**
* Get flag
*
* @return string
*/
public function getFlag()
{
return $this->flag;
}

/**
* Set geoip
*
* @param string $geoip
* @return Country
*/
public function setGeoip($geoip)
{
$this->geoip = $geoip;

return $this;
}

/**
* Get geoip
*
* @return string
*/
public function getGeoip()
{
return $this->geoip;
}
}


实体ConcursoCountry

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* ConcursoCountry
*
* @ORM\Table(name="concurso_country", indexes={@ORM\Index(name="fk_concurso_country_country_idx", columns={"cou_id"}), @ORM\Index(name="fk_concurso_country_language_idx", columns={"lan_id"}), @ORM\Index(name="IDX_D8E1022D6639A0D9", columns={"con_id"})})
* @ORM\Entity
*/
class ConcursoCountry
{
/**
* @var \Application\Entity\Concurso
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
* @ORM\OneToOne(targetEntity="Application\Entity\Concurso")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="con_id", referencedColumnName="con_id", nullable=true)
* })
*/
private $con;

/**
* @var \Application\Entity\Country
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
* @ORM\OneToOne(targetEntity="Application\Entity\Country")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="cou_id", referencedColumnName="cou_id", nullable=true)
* })
*/
private $cou;

/**
* @var \Application\Entity\Language
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
* @ORM\OneToOne(targetEntity="Application\Entity\Language")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="lan_id", referencedColumnName="lan_id", nullable=true)
* })
*/
private $lan;


/**
* Set con
*
* @param \Application\Entity\Concurso $con
* @return ConcursoCountry
*/
public function setCon(\Application\Entity\Concurso $con)
{
$this->con = $con;

return $this;
}

/**
* Get con
*
* @return \Application\Entity\Concurso
*/
public function getCon()
{
return $this->con;
}

/**
* Set cou
*
* @param \Application\Entity\Country $cou
* @return ConcursoCountry
*/
public function setCou(\Application\Entity\Country $cou)
{
$this->cou = $cou;

return $this;
}

/**
* Get cou
*
* @return \Application\Entity\Country
*/
public function getCou()
{
return $this->cou;
}

/**
* Set lan
*
* @param \Application\Entity\Language $lan
* @return ConcursoCountry
*/
public function setLan(\Application\Entity\Language $lan)
{
$this->lan = $lan;

return $this;
}

/**
* Get lan
*
* @return \Application\Entity\Language
*/
public function getLan()
{
return $this->lan;
}
}

最佳答案

首先创建父母,然后创建子代,然后在子代中设置父代(FK)。
在创建了每个对象之后坚持,最后只进行一次刷新。

关于php - Zend 2-理论如何为三个实体一对多插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28245365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com