Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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


1.5k

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



109.4k
Accepted
answer
edited Mar '20

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.



1.5k
edited Mar '20

Thanks for your reply.

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



5.2k

i would like to add that $theme->getThemeId(); is identical to $theme->theme_id;

it can differ if you set the propery theme_id visibility to public and in the same time declared getters and setters in the model