dqlk31541 2015-01-30 22:01
浏览 50

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

First of all, I'm sorry for my bad english.

I have this scenary with three table (with its own 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)

The goal is that into the table concursoCountry where I'll have these fields

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

I'll insert these records

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

All the tables are the correctly relation FK with tables Concurso, Country and Languages, and using the code that I'm going to post after, I can insert only one record into table ConcursoCountry.

I suppose that I'll have to make a only persist and flush for all the records at the same time, but I don't understand how.

This is my code and thank you in advance for any suggestions

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;
    }  
}

This is Entity/Concurso.php (There are several information fields not necessary

namespace Application\Entity;

use Doctrine\ORM\Mapping as 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;
    }
}

Entity 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;
    }
}

Entity Country

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;
    }
}

Entity 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条回答 默认 最新

  • douluhaikao93943 2015-01-30 22:33
    关注

    First create the parents, then create the children, then set parents(FK) in children. Persist after every object created and in the end only one flush.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题