Doctrine - many to one; By filling a lot, can not use the constant & ldquo; ones & rdquo; on the DB. How to manually insert FK?

advertisements

I have the following model:

    class Word{

          ...........
/**
 * @ManyToOne(targetEntity="Language", cascade={"persist"})
 * @JoinColumn(name="language_id", referencedColumnName="id")
 */

protected $language;

   ..........................    

}

     class Language{
/**
 * @Id
 * @Column(type="integer", nullable=false)
 * @GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @Column(type="string", unique=true, nullable=false)
 */
protected $language;

 ........
}

I have 3 records in the database for 3 different languages. A language is unique.

I fail to create a new word and link it to one of the existing languages. I.e. I would like to know how to set the FK of Word to the PK of the language. And since FKs in the model don't exists as int IDs but rather object references, I am confused on how to implement it. I tried to use a Doctrine_Query::create()-> ... but I also failed with that.

I have tried this:

    $this->em = $this->doctrine->em;

    $w = $this->input->post('word');
    $d = $this->input->post('description');

    $desclan = $this->input->post('desclan'); //language is selected from a drop down
                                              //list populated from the DB
    $wordlan = $this->input->post('wordlan');

        $word = new Entity\Word;
        $description = new Entity\Description;
        $language = new Entity\Language;

        $language->setLanguage($wordlan);
        $language->setLanguageId($lanId); // assuming that $lanId is obtained from DB

        $word->setWord($w);
        $word->setLanguage($language);
        $description->setDescription($d);
        $word->setDescrp($description);

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

The language is chosen by a drop-down-list. This code of course spits that the language already exists in the database. How do I simply get the ID of the language and put it as a FK language_id in the 'word' table in the database, through Doctrine code?

Thank you in advance!


It is very important to know what are you posting with your form:

$desclan = $this->input->post('desclan'); //language is selected from a drop down
                                              //list populated from the DB

You say here that the language is selected from a drop down list and populated from the DB, now I ask you what is being posted with the form? A drop down list will probably post an ID of the language that already exist in the database. You just need to find the correct language:

$languageFind = $entityManager->find('Entity\Language', $lanId); // $lanId is posted via form

Then add the language to the word:

$word->setLanguage($languageFind);

P.S: The error "language already exists" is because of this:

$language->setLanguageId($lanId); // assuming that $lanId is obtained from DB

You cant setLanguageId() of the new Language class instance with the $lanId that already exists in the database.