Development

/branches/1.2/lib/plugins/sfPropelPlugin/lib/addon/sfPropelManyToMany.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfPropelPlugin/lib/addon/sfPropelManyToMany.class.php

Revision 16948, 5.1 kB (checked in by fabien, 5 years ago)

[1.2, 1.3] replace all getColumnName() occurrences to getName() as getColumnName() is deprecated in Propel 1.3

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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$
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     // we must load all map builder classes
77     $classes = sfFinder::type('file')->name('*MapBuilder.php')->in(sfProjectConfiguration::getActive()->getModelDirs());
78     foreach ($classes as $class)
79     {
80       $omClass = basename($class, 'MapBuilder.php');
81       if (class_exists($omClass) && is_subclass_of($omClass, 'BaseObject'))
82       {
83         $class_map_builder = basename($class, '.php');
84         $map = new $class_map_builder();
85         if (!$map->isBuilt())
86         {
87           $map->doBuild();
88         }
89       }
90     }
91
92     $tableMap = call_user_func(array(constant($middleClass.'::PEER'), 'getTableMap'));
93
94     return $tableMap->getDatabaseMap()->getTable($column->getRelatedTableName())->getPhpName();
95   }
96
97   public static function getAllObjects($object, $middleClass, $relatedColumn = '', $criteria = null)
98   {
99     if (null === $criteria)
100     {
101       $criteria = new Criteria();
102     }
103
104     $relatedClass = self::getRelatedClass(get_class($object), $middleClass, $relatedColumn);
105
106     // don't show $this object for self-referential relation
107     // make sure to use all primary keys
108     if (!empty($relatedColumn))
109     {
110       $tempCriteria = $object->buildPkeyCriteria();
111       foreach ($tempCriteria->getIterator() as $criterion)
112       {
113         $criteria->add($criterion->getTable().'.'.$criterion->getColumn(), $criterion->getValue(), Criteria::NOT_EQUAL);
114       }
115     }
116
117     return call_user_func(array(constant($relatedClass.'::PEER'), 'doSelect'), $criteria);
118   }
119
120   /**
121    * Gets objects related by a many-to-many relationship, with a middle table.
122    *
123    * @param  $object        The object to get related objects for.
124    * @param  $middleClass   The middle class used for the many-to-many relationship.
125    * @param  $criteria      Criteria to apply to the selection.
126    */
127   public static function getRelatedObjects($object, $middleClass, $relatedColumn = '', $criteria = null)
128   {
129     if (null === $criteria)
130     {
131       $criteria = new Criteria();
132     }
133
134     $relatedClass = self::getRelatedClass(get_class($object), $middleClass, $relatedColumn);
135
136     $relatedObjects = array();
137     if (empty($relatedColumn))
138     {
139       $objectMethod = 'get'.$middleClass.'sJoin'.$relatedClass;
140       $relatedMethod = 'get'.$relatedClass;
141       $rels = $object->$objectMethod($criteria);
142     }
143     else
144     {
145       // as there is no way to join the related objects starting from this object we'll use the through class peer instead
146       $localColumn = self::getColumn(get_class($object), $middleClass, $relatedColumn);
147       $remoteColumn = self::getRelatedColumn(get_class($object), $middleClass, $relatedColumn);
148       $c = new Criteria();
149       $c->add(constant(constant($middleClass.'::PEER').'::'.$localColumn->getName()), $object->getId());
150       $relatedMethod = 'get'.$relatedClass.'RelatedBy'.$remoteColumn->getPhpName();
151       $rels = call_user_func(array(constant($middleClass.'::PEER'), 'doSelectJoin'.$relatedClass.'RelatedBy'.$remoteColumn->getPhpName()), $c);
152     }
153     foreach ($rels as $rel)
154     {
155       $relatedObjects[] = $rel->$relatedMethod();
156     }
157
158     return $relatedObjects;
159   }
160 }
161
Note: See TracBrowser for help on using the browser.