Zend 2 - 学说如何为三个实体插入一个到多个?

时间:2015-01-30 22:01:16

标签: php mysql zend-framework2 doctrine entity

首先,我很抱歉我的英语不好。

我有三个表(有自己的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

所有表格都是FK与表格Concurso,Country和Languages的正确关系,并且使用我要发布的代码后,我只能在表格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;
    }
}

实体语言.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;
    }
}

3 个答案:

答案 0 :(得分:0)

首先创建父项,然后创建子项,然后在子项中设置父项(FK)。 在每个创建的对象之后保留,最后只有一次刷新。

答案 1 :(得分:0)

感谢您的重播。 但我刚刚做了你的建议,但它没有运行。 我只发布必要的代码。

$this->em->persist($concurso);  
$this->em->flush(); <--- CREATE THE PARENT
$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])); <--- SET COUNTRY in ConcursoCountry

        $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); <--- SET AN D PERSIST LANGUAGE for ConcursoCountry
            }
        }
    }
}

$this->em->flush(); <--- ONLY ONE FLUSH

我在ConcursoCountry中只有一条记录。

注意:语言和国家/地区是参数表,因此在我执行此代码之前,数据已经存在。

答案 2 :(得分:0)

我用这种方式修改了代码,如果我在concursoCountry中只有一条记录,我想这是解决我问题的正确架构。 但我不明白我错在哪里。

.....other data for the table Concurso

$concurso->setconConfUniResJob(1);

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

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

    if($posCounntry === false){
        //
    }else{
        ${"concursoCountry".$iCountry} =  new $this->entityConcursoCountry;
        ${"concursoCountry".$iCountry}->setCon($concurso);

        ${"country".$iCountry} = new \Application\Model\DB\Country($this->em);
        ${"concursoCountry".$iCountry}->setCou(${"country".$iCountry}->find($params[$keyCounty]));

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

        $stringLanguage = "lan_id_".$indexCountry;

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

            if($posLang === false){
                //
            }else{
                ${"language".$iLang} = new \Application\Model\DB\Language($this->em);
                ${"concursoCountry".$iCountry}->setLan(${"language".$iLang}->find($params[$keyLan]));

                $this->em->persist(${"concursoCountry".$iCountry}); 


            }
        }

    }
}

$this->em->flush();