Advertisement
Guest User

Phalcon Many to Many relation sync

a guest
Apr 5th, 2016
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 KB | None | 0 0
  1. /**
  2.  * Sync a many to many record to an entity
  3.  * https://laravel.com/docs/4.2/eloquent#inserting-related-models
  4.  *
  5.  * Usage:
  6.  * $entity->sync('alias_name', array(1, 2, 3), $transaction);
  7.  * @param $related_alias   string   Many to Many alias name
  8.  * @param $identifiers     array    array of ID's of the related records
  9.  * @param $transaction     object   Phalcon Trancation object if the entity is part of a transaction
  10.  * @return true | Exception
  11.  */
  12. public function sync($related_alias, array $identifiers, \Phalcon\Mvc\Model\Transaction $transaction = NULL) {
  13.     $modelsmanager = $this->getDI()->getModelsManager();
  14.     $relation = $modelsmanager->getRelationByAlias(get_class($this), $related_alias);
  15.     if(!$relation) {
  16.         $msg = sprintf('Relation alias "%s" does not exists', $related_alias);
  17.         if($transaction) {
  18.             $transaction->rollback($msg);
  19.         } else {
  20.             throw new \Exception($msg);
  21.         }
  22.     }
  23.  
  24.     $related_ids = array();
  25.     $existing_ids = array();
  26.     $new_ids = array();
  27.        
  28.     $related = $this->getRelated($related_alias);
  29.     foreach($related as $r) {
  30.         array_push($related_ids, $r->id);
  31.     }
  32.    
  33.     foreach($identifiers as $i) {
  34.         if(!in_array($i, $related_ids)) {
  35.             array_push($new_ids, $i);
  36.         } else {
  37.             array_push($existing_ids, $i);
  38.         }
  39.     }
  40.    
  41.     $delete_ids = array_diff($related_ids, array_merge($existing_ids, $new_ids));
  42.  
  43.     $intermediate_model = $relation->getIntermediateModel();
  44.     foreach($new_ids as $related_id) {
  45.         $intermediate = new $intermediate_model;
  46.         if($transaction) {
  47.             $intermediate->setTransaction($transaction);
  48.         }
  49.         $intermediate->{$relation->getIntermediateFields()} =  $this->id;
  50.         $intermediate->{$relation->getIntermediateReferencedFields()} = $related_id;
  51.         if($intermediate->create() == false) {
  52.             $msg = 'Could not create intermediate record: ';
  53.             foreach($intermediate->getMessages() as $m) {
  54.                 $msg .= $m;
  55.             }
  56.             if($transaction) {
  57.                 $transaction->rollback($msg);
  58.             } else {
  59.                 throw new \Exception($msg);
  60.             }
  61.         }
  62.     }
  63.    
  64.     foreach($delete_ids as $related_id) {
  65.         $intermediate = $intermediate_model::findFirst(array(
  66.             $relation->getIntermediateFields().' = ?0 AND '.$relation->getIntermediateReferencedFields().' = ?1',
  67.             'bind' => array(
  68.                 0 => $this->id,
  69.                 1 => $related_id
  70.             )
  71.         ));
  72.         if($intermediate) {
  73.             if($transaction) {
  74.                 $intermediate->setTransaction($transaction);
  75.             }
  76.             if($intermediate->delete() == false) {
  77.                 $msg = 'Could not delete intermediate record: ';
  78.                 foreach($intermediate->getMessages() as $m) {
  79.                     $msg .= $m;
  80.                 }
  81.                 if($transaction) {
  82.                     $transaction->rollback($msg);
  83.                 } else {
  84.                     throw new \Exception($msg);
  85.                 }
  86.             }
  87.         }
  88.     }
  89.     return true;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement