| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfPropelManyToMany |
|---|
| 20 |
{ |
|---|
| 21 |
public static function getColumn($class, $middleClass) |
|---|
| 22 |
{ |
|---|
| 23 |
|
|---|
| 24 |
$tableMap = call_user_func(array($middleClass.'Peer', 'getTableMap')); |
|---|
| 25 |
$object_table_name = constant($class.'Peer::TABLE_NAME'); |
|---|
| 26 |
foreach ($tableMap->getColumns() as $column) |
|---|
| 27 |
{ |
|---|
| 28 |
if ($column->isForeignKey() && $object_table_name == $column->getRelatedTableName()) |
|---|
| 29 |
{ |
|---|
| 30 |
return $column; |
|---|
| 31 |
} |
|---|
| 32 |
} |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
public static function getRelatedColumn($class, $middleClass) |
|---|
| 36 |
{ |
|---|
| 37 |
|
|---|
| 38 |
$tableMap = call_user_func(array($middleClass.'Peer', 'getTableMap')); |
|---|
| 39 |
$object_table_name = constant($class.'Peer::TABLE_NAME'); |
|---|
| 40 |
foreach ($tableMap->getColumns() as $column) |
|---|
| 41 |
{ |
|---|
| 42 |
if ($column->isForeignKey() && $object_table_name != $column->getRelatedTableName()) |
|---|
| 43 |
{ |
|---|
| 44 |
return $column; |
|---|
| 45 |
} |
|---|
| 46 |
} |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
public static function getRelatedClass($class, $middleClass) |
|---|
| 50 |
{ |
|---|
| 51 |
$column = self::getRelatedColumn($class, $middleClass); |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
$classes = sfFinder::type('file')->ignore_version_control()->name('*MapBuilder.php')->in(sfLoader::getModelDirs()); |
|---|
| 55 |
foreach ($classes as $class) |
|---|
| 56 |
{ |
|---|
| 57 |
$class_map_builder = basename($class, '.php'); |
|---|
| 58 |
$map = new $class_map_builder(); |
|---|
| 59 |
$map->doBuild(); |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
$tableMap = call_user_func(array($middleClass.'Peer', 'getTableMap')); |
|---|
| 63 |
|
|---|
| 64 |
return $tableMap->getDatabaseMap()->getTable($column->getRelatedTableName())->getPhpName(); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
public static function getAllObjects($object, $middleClass, $criteria = null) |
|---|
| 68 |
{ |
|---|
| 69 |
if (null === $criteria) |
|---|
| 70 |
{ |
|---|
| 71 |
$criteria = new Criteria(); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
$relatedClass = self::getRelatedClass(get_class($object), $middleClass); |
|---|
| 75 |
|
|---|
| 76 |
return call_user_func(array($relatedClass.'Peer', 'doSelect'), $criteria); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
* Gets objects related by a many-to-many relationship, with a middle table. |
|---|
| 81 |
* |
|---|
| 82 |
* @param $object The object to get related objects for. |
|---|
| 83 |
* @param $middleClass The middle class used for the many-to-many relationship. |
|---|
| 84 |
* @param $criteria Criteria to apply to the selection. |
|---|
| 85 |
*/ |
|---|
| 86 |
public static function getRelatedObjects($object, $middleClass, $criteria = null) |
|---|
| 87 |
{ |
|---|
| 88 |
if (null === $criteria) |
|---|
| 89 |
{ |
|---|
| 90 |
$criteria = new Criteria(); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
$relatedClass = self::getRelatedClass(get_class($object), $middleClass); |
|---|
| 94 |
|
|---|
| 95 |
$relatedObjects = array(); |
|---|
| 96 |
$objectMethod = 'get'.$middleClass.'sJoin'.$relatedClass; |
|---|
| 97 |
$relatedMethod = 'get'.$relatedClass; |
|---|
| 98 |
$rels = $object->$objectMethod($criteria); |
|---|
| 99 |
foreach ($rels as $rel) |
|---|
| 100 |
{ |
|---|
| 101 |
$relatedObjects[] = $rel->$relatedMethod(); |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
return $relatedObjects; |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|