Search code examples
ormsymfony1propel

Symfony 1 propel - how to get an object by ID without DB select like retrieveByPK


I need to set Channel object to ChannelMedia. I don't have Channel object, I've got ID instead. I could use ChannelPeer static method, retrieveByPK, but that makes Select to DB only to get the object.

How do I achieve that?

Check below code:

    $channel = ChannelPeer::retrieveByPK($channelID);
    $channel_media = new ChannelMedia();
    $channel_media->setMedia($this->object);

    // How to pass ID here?
    // Alternatively, how to create object from $channelID without DB conn?
    $channel_media->setChannel($channel);
    $channel_media->setStatus(3);
    $channel_media->save();

Solution

  • Your ChannelMedia class should have a setChannelId() method. You could use that to set the channel ID, thus avoiding the need to make an extra call to the DB.

    $channel_media = new ChannelMedia();
    $channel_media->setMedia($this->object);
    $channel_media->setChannelId($channelID);
    $channel_media->setStatus(3);
    $channel_media->save();