0
votes

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

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

3 Answers

0
votes

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.

0
votes

Thank you for your replay. But I've just did how you suggest, but it doesn't run. I'll post only the necessary code.

$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

I'll have only one record in ConcursoCountry.

Note: Language and Country are parametric table, so the data are already into them before I'll execute this code.

0
votes

I modified in this way the code and also if I'll have only one record in my concursoCountry, I suppose this is the right schema to solve my problem. But I don't understand where I mistake.

.....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();