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

Setter and many relations

HI,

I write my models with getters and setters. I'm a bit confused, i'm trying to save a relation like this one :

// Get an existing artist
$artist = Artists::findFirst('name = "Shinichi Osawa"');

// Create an album
$album          = new Albums();
$album->name    = 'The One';
$album->artist  = $artist;

$songs = array();

// Create a first song
$songs[0]           = new Songs();
$songs[0]->name     = 'Star Guitar';
$songs[0]->duration = '5:54';

// Create a second song
$songs[1]           = new Songs();
$songs[1]->name     = 'Last Days';
$songs[1]->duration = '4:29';

// Assign the songs array
$album->songs = $songs;

// Save the album + its songs
$album->save();

And the thing is I don't exactly know how to do the $songs[0]->name using setters....if someone has an idea!



473
edited May '15

Hey man

If you have mutiple item for save I think you use foreach, for example code below

foreach item in items{
        $object = new Songs();
        $object->setName(item['name']);
        $object->setDuration(item['duration']);
        $object->save()
}


8.2k

The thing is I want to use the implicits transactions and just save the main entity, as describe in the documentation