Guest User

Untitled

a guest
Jul 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. <?php
  2. /**
  3. * Listener which cascade CRUD action on the model to the File's relations.
  4. */
  5.  
  6. class FileableListener extends Doctrine_Record_Listener
  7. {
  8. protected $_options = array();
  9.  
  10. public function __construct($options = null)
  11. {
  12. $this->_options = $options;
  13. }
  14.  
  15. public function preDqlSelect(Doctrine_Event $event)
  16. {
  17. $query = $event->getQuery();
  18. $components = $this->_getDqlCallbackComponents($query);
  19. foreach ($components as $alias => $component)
  20. {
  21. if (isset($component['relation']))
  22. {
  23. $query->addWhere($alias.'.object_type = ?', get_class($event->getInvoker()));
  24. }
  25. }
  26. }
  27.  
  28. /**
  29. * Cascade delete to FileRelation
  30. * @param Doctrine_Event $event
  31. */
  32. public function postDelete(Doctrine_Event $event)
  33. {
  34. $record = $event->getInvoker();
  35.  
  36. FileRelationTable::getInstance()
  37. ->createQuery('f')
  38. ->delete()
  39. ->where('f.object_type = ?', get_class($record))
  40. ->andWhere('f.object_id = ?', $record->getPrimaryKey())
  41. ->execute();
  42. }
  43.  
  44. /**
  45. * Cascade update to FileRelation
  46. * @param Doctrine_Event $event
  47. */
  48. public function preUpdate(Doctrine_Event $event)
  49. {
  50. $record = $event->getInvoker();
  51. $modified = $record->getModified();
  52.  
  53. if(isset($modified['id']))
  54. {
  55. FileRelationTable::getInstance()
  56. ->createQuery('f')
  57. ->update()
  58. ->set('object_id',$record->getId())
  59. ->where('object_type = ?', get_class($record))
  60. ->andWhere('object_id = ?', $record->getPrimaryKey())
  61. ->execute();
  62. }
  63. }
  64.  
  65. protected function _getDqlCallbackComponents($query)
  66. {
  67. $params = $query->getParams();
  68. $componentsBefore = array();
  69. if ($query->isSubquery())
  70. {
  71. $componentsBefore = $query->getQueryComponents();
  72. }
  73.  
  74. $copy = $query->copy();
  75. $copy->getSqlQuery($params);
  76. $componentsAfter = $copy->getQueryComponents();
  77.  
  78. if ($componentsBefore !== $componentsAfter)
  79. {
  80. return array_diff($componentsAfter, $componentsBefore);
  81. }
  82. else
  83. {
  84. return $componentsAfter;
  85. }
  86. }
  87. }
Add Comment
Please, Sign In to add comment