0

I have a table (call it Table A) that needs to create multiple additional child records when a Table A record is created. Deleting the Table A record should delete the Table B records.

Deleting the records is easy using ON DELETE CASCADE in schema.yml, but adding the records is not all that simple. Overriding Doctrine_Record::postInsert doesn't do it because the Table A record doesn't have an ID yet. Overriding Doctrine_Form save doesn't do it, either.

Is an EventListener the way to go or a database trigger? If the former, do you have an example? If the latter, can the trigger be specified somehow in schema.yml?

j0k
  • 22,600
  • 28
  • 79
  • 90
Riothamus
  • 71
  • 1
  • 4

1 Answers1

0

Are you sure that postInsert doesn't work?

I use this snippet in one of my project (inside the Table.class.php):

public function postInsert($event)
{
  $dispatcher = ProjectConfiguration::getActive()->getEventDispatcher();
  $dispatcher->notify(new sfEvent($this, 'item.new', array(
    'item' => $this
  )));
}

I spread an event about a new item. Then, I catch this event in my configuration:

class frontendConfiguration extends sfApplicationConfiguration
{
  public function configure()
  {
    // [....]
    $this->dispatcher->connect('item.new', array('shareEvents', 'postToSocial'));

And in my shareEvents class I access the item id without any problems:

class shareEvents
{
  static public function postToSocial(sfEvent $event)
  {
    $item = $event['item'];

    $queue = new AutopostQueue();
    $queue->setItemId($item->getId());
    $queue->save();

By the way, you can do your action in a trigger too. But I don't think you can define a trigger inside the schema.yml since Doctrine parse it to generate model, form, etc .. But you can define a trigger.sql file inside /data/sql directory and it will be automatically loaded when you will call:

php symfony doctrine:insert-sql
j0k
  • 22,600
  • 28
  • 79
  • 90