Changeset 7555
- Timestamp:
- 02/20/08 16:33:33 (9 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelImpersonatorPlugin/trunk/lib/sfPropelObjectPeerImpersonator.class.php
r7392 r7555 37 37 $connection, 38 38 $objects, 39 $objectsParameters, 39 40 $classToIndex, 40 41 $relations, … … 55 56 $args = func_get_args(); 56 57 $this->objects = array(); 58 $this->objectsParameters = array(); 57 59 58 60 if (count($args)) … … 349 351 $startcol = $rowObjects[$index]->hydrate($resultset, $startcol); 350 352 351 // if the object was only made of null values, we consider it's inconsistent and forget it.352 /*if (!$this->testConsistence($rowObjects[$index]->getPrimaryKey()))353 /* // if the object was only made of null values, we consider it's inconsistent and forget it. 354 if (!$this->testConsistence($rowObjects[$index]->getPrimaryKey())) 353 355 { 354 356 unset($rowObjects[$index]); 355 357 continue; 356 358 } 357 for now, let's say we keep it359 /* for now, let's say we keep it 358 360 359 361 reason is: if we dont populate an empty propel object, propel will think we don't know it does not exists, and … … 413 415 $foreignClass = $relation['classFrom']; 414 416 $foreignObject = $rowObjects[$this->getIndexByClass($foreignClass)]; 417 $relatedBy = (null === ($_relatedBy=$this->getParameter($index, 'related_by')))?'':'RelatedBy'.sfInflector::camelize($_relatedBy); 415 418 416 419 if ($isNewObject) … … 421 424 } 422 425 423 $currentObject->{'init'.$foreignClass.'s'}(); 426 427 $currentObject->{'init'.$foreignClass.'s'.$relatedBy}(); 424 428 } 425 429 426 $currentObject->{'add'.$foreignClass }($foreignObject);427 $foreignObject->{'set'.$relation['classTo'] }($currentObject);430 $currentObject->{'add'.$foreignClass.$relatedBy}($foreignObject); 431 $foreignObject->{'set'.$relation['classTo'].$relatedBy}($currentObject); 428 432 429 433 $linkedRelationsCounter++; … … 473 477 $foreignObject = $rowObjects[$classToIndex]; 474 478 479 $relatedBy = (null === ($_relatedBy=$this->getParameter($classToIndex, 'related_by')))?'':'RelatedBy'.sfInflector::camelize($_relatedBy); 480 // @todo: bug correction, this does not work if related_by is used this way, it tries to call setFieldRelatedByFieldId 481 // instead of setClassNameRelatedByFieldId, and sometimes (depending on order) it uses the wrong Field, as second one 482 // override first one in relations 483 475 484 // local *---- foreign (local object has one foreign object) 476 $rowObjects[$index]->{'set'.str_replace('Id','',$relation['local']) }($foreignObject);485 $rowObjects[$index]->{'set'.str_replace('Id','',$relation['local']).$relatedBy}($foreignObject); 477 486 478 487 // foreign ----* local (foreign object has many local objects) … … 480 489 // @todo: find a way not to call initXxxXxxs() on every passes, but only on first addition for each object. 481 490 482 $foreignObject->{'init'.$relation['classFrom'].'s' }($rowObjects[$index]);483 $foreignObject->{'add'.$relation['classFrom'] }($rowObjects[$index]);491 $foreignObject->{'init'.$relation['classFrom'].'s'.$relatedBy}($rowObjects[$index]); 492 $foreignObject->{'add'.$relation['classFrom'].$relatedBy}($rowObjects[$index]); 484 493 485 494 $linkedRelationsCounter++; … … 527 536 * 528 537 * @param mixed $key 538 * 529 539 * @return boolean 530 540 */ … … 554 564 555 565 /** 566 * Merge a new parameters set with existing one for object indexed by $objectIndex 567 * 568 * @param integer $objectIndex 569 * @param array $parameters 570 */ 571 protected function setParameters($objectIndex, array $parameters) 572 { 573 if (!isset($this->objectsParameters[$objectIndex])) 574 { 575 $this->objectsParameters[$objectIndex] = array(); 576 } 577 578 $this->objectsParameters[$objectIndex] = array_merge($this->objectsParameters[$objectIndex], $parameters); 579 } 580 581 /** 582 * Retrieve a parameter for an object 583 * 584 * @param mixed $objectIndexOrName 585 * @param string $parameterName 586 * @param mixed $defaultValue 587 * 588 * @return mixed 589 */ 590 public function getParameter($objectIndexOrName, $parameterName, $defaultValue=null) 591 { 592 if (!is_numeric($objectIndexOrName)) 593 { 594 $objectIndexOrName = $this->getIndexByClass($objectIndexOrName); 595 } 596 597 if (null===$objectIndexOrName || !isset($this->objects[$objectIndexOrName])) 598 { 599 throw new Exception('Tried to retrievea parameter for an unknown object name or index.'); 600 } 601 602 if (!isset($this->objectsParameters[$objectIndexOrName]) || !isset($this->objectsParameters[$objectIndexOrName][$parameterName])) 603 { 604 return $defaultValue; 605 } 606 else 607 { 608 return $this->objectsParameters[$objectIndexOrName][$parameterName]; 609 } 610 } 611 612 /** 556 613 * Adds an object instance to the current objects array 557 614 */ 558 protected function addObject($instance )615 protected function addObject($instance, $parameters=null) 559 616 { 560 617 $this->objects[] = $instance; 618 619 if (null!==$parameters) 620 { 621 $this->setParameters(count($this->objects)-1, $parameters); 622 } 561 623 } 562 624 … … 564 626 protected function addObjectByClassName($className) 565 627 { 628 // find whether or not we have parameters set for this object 629 if (false!==strpos($className, ' ')) 630 { 631 // separate classname from parameters 632 $_parameters = explode(' ', $className); 633 $className = array_shift($_parameters); 634 635 // transform parameters into useable array 636 $_parameters = array_map(create_function('$v', 'return explode(\'=\', $v);'), $_parameters); 637 $parameters = array(); 638 639 foreach ($_parameters as $parameter) 640 { 641 if (count($parameter)==2) 642 { 643 $parameters[$parameter[0]] = $parameter[1]; 644 } 645 else 646 { 647 throw new Exception('Invalid parameter given'); 648 } 649 } 650 } 651 652 // add object 566 653 if (class_exists($className, true)) 567 654 { 568 $this->addObject(new $className()); 655 if (isset($parameters)) 656 { 657 $this->addObject(new $className(), $parameters); 658 } 659 else 660 { 661 $this->addObject(new $className()); 662 } 569 663 } 570 664 else 571 665 { 572 throw new Exception('sfPropelObjectPeerImpersonator::initialize : Unknown object class given "'.$className.'"');666 throw new Exception('sfPropelObjectPeerImpersonator::initialize(): Unknown object class given "'.$className.'"'); 573 667 } 574 668 }