| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* Utilities for managing many to many relationships in propel.
|
|
|
13 |
*
|
|
|
14 |
* @package symfony
|
|
|
15 |
* @subpackage propel
|
|
|
16 |
* @author Nick Lane <nick.lane@internode.on.net>
|
|
|
17 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
18 |
* @version SVN: $Id: sfPropelManyToMany.class.php 22881 2009-10-08 16:50:37Z Kris.Wallsmith $
|
|
|
19 |
*/
|
|
|
20 |
class sfPropelManyToMany
|
|
|
21 |
{
|
|
|
22 |
public static function getColumn($class, $middleClass, $relatedColumn = '')
|
|
|
23 |
{
|
|
|
24 |
// find the related class
|
|
|
25 |
$tableMap = call_user_func(array(constant($middleClass.'::PEER'), 'getTableMap'));
|
|
|
26 |
$object_table_name = constant(constant($class.'::PEER').'::TABLE_NAME');
|
|
|
27 |
|
|
|
28 |
if (!empty($relatedColumn))
|
|
|
29 |
{
|
|
|
30 |
$relatedColumnName = $tableMap->getColumn($relatedColumn)->getPhpName();
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
foreach ($tableMap->getColumns() as $column)
|
|
|
34 |
{
|
|
|
35 |
if ($column->isForeignKey() && $object_table_name == $column->getRelatedTableName())
|
|
|
36 |
{
|
|
|
37 |
if (!empty($relatedColumn))
|
|
|
38 |
{
|
|
|
39 |
if ($column->getPhpName() != $relatedColumnName)
|
|
|
40 |
{
|
|
|
41 |
return $column;
|
|
|
42 |
}
|
|
|
43 |
}
|
|
|
44 |
else
|
|
|
45 |
{
|
|
|
46 |
return $column;
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public static function getRelatedColumn($class, $middleClass, $relatedColumn = '')
|
|
|
53 |
{
|
|
|
54 |
// find the related class
|
|
|
55 |
$tableMap = call_user_func(array(constant($middleClass.'::PEER'), 'getTableMap'));
|
|
|
56 |
$object_table_name = constant(constant($class.'::PEER').'::TABLE_NAME');
|
|
|
57 |
|
|
|
58 |
if (!empty($relatedColumn))
|
|
|
59 |
{
|
|
|
60 |
return $tableMap->getColumn($relatedColumn);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
foreach ($tableMap->getColumns() as $column)
|
|
|
64 |
{
|
|
|
65 |
if ($column->isForeignKey() && $object_table_name != $column->getRelatedTableName())
|
|
|
66 |
{
|
|
|
67 |
return $column;
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public static function getRelatedClass($class, $middleClass, $relatedColumn = '')
|
|
|
73 |
{
|
|
|
74 |
$column = self::getRelatedColumn($class, $middleClass, $relatedColumn);
|
|
|
75 |
|
|
|
76 |
$tableMap = call_user_func(array(constant($middleClass.'::PEER'), 'getTableMap'));
|
|
|
77 |
$tableMap->getRelations();
|
|
|
78 |
|
|
|
79 |
return $tableMap->getDatabaseMap()->getTable($column->getRelatedTableName())->getPhpName();
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public static function getAllObjects($object, $middleClass, $relatedColumn = '', $criteria = null)
|
|
|
83 |
{
|
|
|
84 |
if (null === $criteria)
|
|
|
85 |
{
|
|
|
86 |
$criteria = new Criteria();
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
$relatedClass = self::getRelatedClass(get_class($object), $middleClass, $relatedColumn);
|
|
|
90 |
|
|
|
91 |
// don't show $this object for self-referential relation
|
|
|
92 |
// make sure to use all primary keys
|
|
|
93 |
if (!empty($relatedColumn))
|
|
|
94 |
{
|
|
|
95 |
$tempCriteria = $object->buildPkeyCriteria();
|
|
|
96 |
foreach ($tempCriteria->getIterator() as $criterion)
|
|
|
97 |
{
|
|
|
98 |
$criteria->add($criterion->getTable().'.'.$criterion->getColumn(), $criterion->getValue(), Criteria::NOT_EQUAL);
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
return call_user_func(array(constant($relatedClass.'::PEER'), 'doSelect'), $criteria);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Gets objects related by a many-to-many relationship, with a middle table.
|
|
|
107 |
*
|
|
|
108 |
* @param $object The object to get related objects for.
|
|
|
109 |
* @param $middleClass The middle class used for the many-to-many relationship.
|
|
|
110 |
* @param $criteria Criteria to apply to the selection.
|
|
|
111 |
*/
|
|
|
112 |
public static function getRelatedObjects($object, $middleClass, $relatedColumn = '', $criteria = null)
|
|
|
113 |
{
|
|
|
114 |
if (null === $criteria)
|
|
|
115 |
{
|
|
|
116 |
$criteria = new Criteria();
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
$relatedClass = self::getRelatedClass(get_class($object), $middleClass, $relatedColumn);
|
|
|
120 |
|
|
|
121 |
$relatedObjects = array();
|
|
|
122 |
if (empty($relatedColumn))
|
|
|
123 |
{
|
|
|
124 |
$objectMethod = 'get'.$middleClass.'sJoin'.$relatedClass;
|
|
|
125 |
$relatedMethod = 'get'.$relatedClass;
|
|
|
126 |
$rels = $object->$objectMethod($criteria);
|
|
|
127 |
}
|
|
|
128 |
else
|
|
|
129 |
{
|
|
|
130 |
// as there is no way to join the related objects starting from this object we'll use the through class peer instead
|
|
|
131 |
$localColumn = self::getColumn(get_class($object), $middleClass, $relatedColumn);
|
|
|
132 |
$remoteColumn = self::getRelatedColumn(get_class($object), $middleClass, $relatedColumn);
|
|
|
133 |
$c = new Criteria();
|
|
|
134 |
$c->add(constant(constant($middleClass.'::PEER').'::'.$localColumn->getName()), $object->getId());
|
|
|
135 |
$relatedMethod = 'get'.$relatedClass.'RelatedBy'.$remoteColumn->getPhpName();
|
|
|
136 |
$rels = call_user_func(array(constant($middleClass.'::PEER'), 'doSelectJoin'.$relatedClass.'RelatedBy'.$remoteColumn->getPhpName()), $c);
|
|
|
137 |
}
|
|
|
138 |
foreach ($rels as $rel)
|
|
|
139 |
{
|
|
|
140 |
$relatedObjects[] = $rel->$relatedMethod();
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
return $relatedObjects;
|
|
|
144 |
}
|
|
|
145 |
}
|