We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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


2.4k

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



125.7k
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.



2.4k
edited Mar '20

Thanks for your reply.

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



8.4k

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