Insert into realtionship

I would like to know, how I insert a new item to the relationship, and also include the "themeId" into the themeVersion:

    $theme = new Themes();

        $name = $this->request->getPost("name");

        $theme->setName($name);
        $theme->setOwnerId(1);
        $theme->setDescription("asdas");
        $theme->setVersionId(1);
        $theme->setSlug("test");

        $version = new Version();

        echo $theme->getThemeId(); // How do i get my theme_id, so i can insert it into the version?

        $version->setThemeId("1");
        $version->setType("development");
        $version->setVersion("1.0");
        $version->setName("Debut");

        $theme->version = $version;

You need to save it into database. DB will return incremental ID, which you can after use for another models.

$theme = new Themes();
$theme->setName($name);
$theme->save();

$theme->getId(); // Or depends how you called your primary key


963

I know, but then i though it would be possible to save the version with the themeid and insert them together like my current example

edited 5d ago

Assuming you have set up the Theme <-> Version relationship like this:

Themes

public function initialize(){
    $this->hasMany('id','Version','theme_id');
}

Version

public function initialize(){
    $this->belongsTo('theme_id','Themes','id');
}

then you don't need to do anything. Phalcon will auto-assign the correct Themes.id to the new Version when saving.



963
edited 5d ago

Thanks for your reply.

I will look into it as soon as I am able to!