Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) 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
 * A database behavior that adds default symfony behaviors.
13
 *
14
 * @package     sfPropelPlugin
15
 * @subpackage  behavior
16
 * @author      Kris Wallsmith <kris.wallsmith@symfony-project.com>
17
 * @version     SVN: $Id: SfPropelBehaviorSymfony.php 23737 2009-11-09 23:23:25Z Kris.Wallsmith $
18
 */
19
class SfPropelBehaviorSymfony extends SfPropelBehaviorBase
20
{
21
  protected $parameters = array(
22
    'form'   => 'true',
23
    'filter' => 'true',
24
  );
25
 
26
  public function modifyDatabase()
27
  {
28
    foreach ($this->getDatabase()->getTables() as $table)
29
    {
30
      $behaviors = $table->getBehaviors();
31
 
32
      if (!isset($behaviors['symfony']))
33
      {
34
        $behavior = clone $this;
35
        $table->addBehavior($behavior);
36
      }
37
 
38
      // symfony behaviors
39
      if (!isset($behaviors['symfony_behaviors']) && $this->getBuildProperty('propel.builder.addBehaviors'))
40
      {
41
        $class = Propel::importClass($this->getBuildProperty('propel.behavior.symfony_behaviors.class'));
42
        $behavior = new $class();
43
        $behavior->setName('symfony_behaviors');
44
        $table->addBehavior($behavior);
45
      }
46
 
47
      // timestampable
48
      if (!isset($behaviors['symfony_timestampable']))
49
      {
50
        $parameters = array();
51
        foreach ($table->getColumns() as $column)
52
        {
53
          if (!isset($parameters['create_column']) && in_array($column->getName(), array('created_at', 'created_on')))
54
          {
55
            $parameters['create_column'] = $column->getName();
56
          }
57
 
58
          if (!isset($parameters['update_column']) && in_array($column->getName(), array('updated_at', 'updated_on')))
59
          {
60
            $parameters['update_column'] = $column->getName();
61
          }
62
        }
63
 
64
        if ($parameters)
65
        {
66
          $class = Propel::importClass($this->getBuildProperty('propel.behavior.symfony_timestampable.class'));
67
          $behavior = new $class();
68
          $behavior->setName('symfony_timestampable');
69
          $behavior->setParameters($parameters);
70
          $table->addBehavior($behavior);
71
        }
72
      }
73
    }
74
  }
75
 
76
  public function objectAttributes()
77
  {
78
    if ($this->isDisabled())
79
    {
80
      return;
81
    }
82
 
83
    return <<<EOF
84
 
85
const PEER = '{$this->getTable()->getPhpName()}Peer';
86
 
87
EOF;
88
  }
89
 
90
  public function staticAttributes()
91
  {
92
    if ($this->isDisabled())
93
    {
94
      return;
95
    }
96
 
97
    $behaviors = $this->getTable()->getBehaviors();
98
    $isI18n = isset($behaviors['symfony_i18n']) ? 'true' : 'false';
99
 
100
    return <<<EOF
101
 
102
/**
103
 * Indicates whether the current model includes I18N.
104
 */
105
const IS_I18N = {$isI18n};
106
 
107
EOF;
108
  }
109
 
110
  public function staticMethods()
111
  {
112
    if ($this->isDisabled())
113
    {
114
      return;
115
    }
116
 
117
    $unices = array();
118
    foreach ($this->getTable()->getUnices() as $unique)
119
    {
120
      $unices[] = sprintf("array('%s')", implode("', '", $unique->getColumns()));
121
    }
122
    $unices = implode(', ', array_unique($unices));
123
 
124
    return <<<EOF
125
 
126
/**
127
 * Returns an array of arrays that contain columns in each unique index.
128
 *
129
 * @return array
130
 */
131
static public function getUniqueColumnNames()
132
{
133
  return array({$unices});
134
}
135
 
136
EOF;
137
  }
138
}