Changeset 12955
- Timestamp:
- 11/12/08 17:17:47 (5 years ago)
- Files:
-
- plugins/sfDoctrinePlugin/trunk/lib/form/sfFormDoctrine.class.php (modified) (12 diffs)
- plugins/sfDoctrinePlugin/trunk/test/bootstrap/functional.php (modified) (2 diffs)
- plugins/sfDoctrinePlugin/trunk/test/bootstrap/unit.php (modified) (1 diff)
- plugins/sfDoctrinePlugin/trunk/test/functional/AccessorTest.php (deleted)
- plugins/sfDoctrinePlugin/trunk/test/functional/EnvironmentSetupTest.php (modified) (1 diff)
- plugins/sfDoctrinePlugin/trunk/test/functional/I18nTest.php (modified) (2 diffs)
- plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/config/ProjectConfiguration.class.php (modified) (1 diff)
- plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/config/doctrine/schema.yml (modified) (1 diff)
- plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/data/fixtures/fixtures.yml (modified) (1 diff)
- plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/data/sql/schema.sql (modified) (1 diff)
- plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/lib/filter/doctrine/base/BaseArticleFormFilter.class.php (modified) (2 diffs)
- plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/lib/form/doctrine/base/BaseArticleForm.class.php (modified) (1 diff)
- plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/lib/model/doctrine/Author.class.php (modified) (1 diff)
- plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/lib/model/doctrine/base/BaseArticle.class.php (modified) (2 diffs)
- plugins/sfDoctrinePlugin/trunk/test/functional/sfDoctrineRecordTest.php (added)
- plugins/sfDoctrinePlugin/trunk/test/unit/sfDoctrineColumnTest.php (added)
- plugins/sfDoctrinePlugin/trunk/test/unit/sfDoctrineDatabaseTest.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfDoctrinePlugin/trunk/lib/form/sfFormDoctrine.class.php
r12941 r12955 30 30 { 31 31 protected 32 $ cultures = array(),33 $object = null;32 $isNew = true, 33 $object = null; 34 34 35 35 /** … … 57 57 58 58 $this->object = $object; 59 $this->isNew = !$this->object->exists(); 59 60 } 60 61 … … 86 87 public function isNew() 87 88 { 88 return !$this->object->exists(); 89 $this->isNew = !$this->object->exists(); 90 91 return $this->isNew; 89 92 } 90 93 … … 92 95 * Embeds i18n objects into the current form. 93 96 * 94 * @param array An array of cultures 95 * @param string The format to use for widget name 96 * @param string A HTML decorator for the embedded form 97 */ 98 public function embedI18n($cultures, $nameFormat = null, $decorator = null) 97 * @param array $cultures An array of cultures 98 * @param string $decorator A HTML decorator for the embedded form 99 */ 100 public function embedI18n($cultures, $decorator = null) 99 101 { 100 102 if (!$this->isI18n()) … … 103 105 } 104 106 105 if ($this->isI18n() && !isset($this->Translation))106 {107 // lazy load translations108 $this->getObject()->loadReference('Translation');109 }110 111 $this->cultures = $cultures;112 107 $class = $this->getI18nFormClass(); 113 $i18n = new $class();114 108 foreach ($cultures as $culture) 115 109 { 116 $this->embedForm($culture, $i18n, $nameFormat, $decorator); 110 $i18nObject = $this->object->Translation[$culture]; 111 $i18n = new $class($i18nObject); 112 unset($i18n['id'], $i18n['lang']); 113 114 $this->embedForm($culture, $i18n, $decorator); 117 115 } 118 116 } … … 207 205 $values = $this->processValues($values); 208 206 209 $this->object->fromArray($values , false);207 $this->object->fromArray($values); 210 208 211 209 // embedded forms … … 235 233 public function processValues($values = null) 236 234 { 237 if (is_null($values)) 238 { 239 $values = $this->getValues(); 240 } 241 242 // remove special columns that are updated automatically 243 unset($values['id'], $values['updated_at'], $values['updated_on'], $values['created_at'], $values['created_on']); 244 245 // Move translations to the Translation key so that it will work with Doctrine_Record::fromArray() 246 foreach ($this->cultures as $culture) 247 { 248 $translation = $values[$culture]; 249 $translation['lang'] = $culture; 250 unset($translation['id']); 251 $values['Translation'][$culture] = $translation; 252 unset($values[$culture]); 253 } 254 255 foreach ($values as $field => $value) 256 { 257 $method = sprintf('update%sColumn', Doctrine_Inflector::classify($field)); 235 // see if the user has overridden some column setter 236 $valuesToProcess = $values; 237 foreach ($valuesToProcess as $field => $value) 238 { 239 $method = sprintf('update%sColumn', self::camelize($field)); 258 240 259 241 if (method_exists($this, $method)) … … 292 274 293 275 /** 276 * Returns the name of the i18n model. 277 * 278 * @return string The name of the i18n model 279 */ 280 public function getI18nModelName() 281 { 282 return $this->getObject()->getTable()->getTemplate('Doctrine_Template_I18n')->getI18n()->getOption('className'); 283 } 284 285 /** 294 286 * Returns the name of the i18n form class. 295 287 * … … 299 291 { 300 292 return $this->getI18nModelName() . 'Form'; 301 }302 303 /**304 * Returns the name of the i18n model.305 *306 * @return string The name of the i18n model307 */308 public function getI18nModelName()309 {310 return $this->getObject()->getTable()->getTemplate('Doctrine_Template_I18n')->getI18n()->getOption('className');311 293 } 312 294 … … 350 332 $this->updateObject(); 351 333 334 $this->object->save($con); 335 352 336 // embedded forms 353 337 $this->saveEmbeddedForms($con); 354 355 $this->object->save($con);356 338 } 357 339 … … 378 360 protected function updateDefaultsFromObject() 379 361 { 380 if ($this->isI18n() && !isset($this->Translation))381 {382 // lazy load translations383 $this->getObject()->loadReference('Translation');384 }385 386 362 // update defaults for the main object 387 363 if ($this->isNew()) 388 364 { 389 $this->setDefaults(array_merge($this->object->toArray( true), $this->getDefaults()));365 $this->setDefaults(array_merge($this->object->toArray(false), $this->getDefaults())); 390 366 } 391 367 else 392 368 { 393 $this->setDefaults(array_merge($this->getDefaults(), $this->object->toArray(true))); 394 } 395 396 if ($this->isI18n()) 397 { 398 $defaults = $this->getDefaults(); 399 $translations = $defaults['Translation']; 400 unset($defaults['Translation']); 401 $this->setDefaults(array_merge($defaults, $translations)); 402 } 369 $this->setDefaults(array_merge($this->getDefaults(), $this->object->toArray(false))); 370 } 371 372 $defaults = $this->getDefaults(); 373 foreach ($this->embeddedForms as $name => $form) 374 { 375 if ($form instanceof sfFormDoctrine) 376 { 377 $form->updateDefaultsFromObject(); 378 $defaults[$name] = $form->getDefaults(); 379 } 380 } 381 $this->setDefaults($defaults); 403 382 } 404 383 … … 418 397 } 419 398 399 if ($this->getValue($field.'_delete')) 400 { 401 $this->removeFile($field); 402 403 return ''; 404 } 405 420 406 if (!$this->getValue($field)) 421 407 { plugins/sfDoctrinePlugin/trunk/test/bootstrap/functional.php
r12651 r12955 9 9 */ 10 10 11 include(dirname(__FILE__).'/../../../../../test/bootstrap/unit.php'); 12 11 13 if (!isset($root_dir)) 12 14 { … … 14 16 } 15 17 16 require_once $root_dir.'/config/ProjectConfiguration.class.php';18 include $root_dir.'/config/ProjectConfiguration.class.php'; 17 19 $configuration = ProjectConfiguration::getApplicationConfiguration($app, 'test', isset($debug) ? $debug : true); 18 20 sfContext::createInstance($configuration); 19 20 21 21 22 function sf_functional_test_shutdown() plugins/sfDoctrinePlugin/trunk/test/bootstrap/unit.php
r11650 r12955 9 9 */ 10 10 11 require_once(dirname(__FILE__).'/../../../../../test/bootstrap/unit.php');11 include(dirname(__FILE__).'/../../../../../test/bootstrap/unit.php'); 12 12 13 require_once(dirname(__FILE__).'/../../../../autoload/sfSimpleAutoload.class.php');13 include(dirname(__FILE__).'/../../../../autoload/sfSimpleAutoload.class.php'); 14 14 $autoload = sfSimpleAutoload::getInstance(sfToolkit::getTmpDir().DIRECTORY_SEPARATOR.sprintf('sf_autoload_unit_doctrine_%s.data', md5(__FILE__))); 15 15 $autoload->addDirectory(realpath(dirname(__FILE__).'/../../lib')); plugins/sfDoctrinePlugin/trunk/test/functional/EnvironmentSetupTest.php
r11948 r12955 12 12 $fixtures = 'fixtures/fixtures.yml'; 13 13 require_once(dirname(__FILE__).'/../bootstrap/functional.php'); 14 require_once(dirname(__FILE__).'/../bootstrap/unit.php');15 14 16 15 $t = new lime_test(12, new lime_output_color()); plugins/sfDoctrinePlugin/trunk/test/functional/I18nTest.php
r12281 r12955 10 10 11 11 $app = 'frontend'; 12 $fixtures = 'fixtures/fixtures.yml';13 12 require_once(dirname(__FILE__).'/../bootstrap/functional.php'); 14 13 15 $b = new sfTestBrowser(); 16 $t = $b->test(); 14 $t = new lime_test(8, new lime_output_color()); 17 15 18 16 $article = new Article(); … … 28 26 $t->is($article->getTitle(), 'test'); 29 27 30 $b->get('articles/index'); 28 $article->free(true); 29 30 class MyArticleForm extends ArticleForm 31 { 32 public function configure() 33 { 34 parent::configure(); 35 36 $this->embedI18n(array('en', 'fr')); 37 38 $authorForm = new AuthorForm($this->object->Author); 39 unset($authorForm['id']); 40 41 $this->embedForm('Author', $authorForm); 42 43 unset($this['author_id']); 44 } 45 46 public function updateDefaultsFromObject() 47 { 48 parent::updateDefaultsFromObject(); 49 } 50 } 51 $article = new Article(); 52 $articleForm = new MyArticleForm($article); 53 54 $data = array( 55 'is_on_homepage' => 1, 56 'Author' => array( 57 'name' => 'i18n author test'), 58 'en' => array( 59 'title' => 'english title', 60 'body' => 'english body'), 61 'fr' => array( 62 'title' => 'french title', 63 'body' => 'french body') 64 ); 65 66 $articleForm->bind($data); 67 $t->is($articleForm->isValid(), true); 68 69 $values = array( 70 'is_on_homepage' => true, 71 'Author' => 72 array( 73 'name' => 'i18n author test', 74 ), 75 'en' => 76 array( 77 'title' => 'english title', 78 'body' => 'english body', 79 'slug' => '', 80 ), 81 'fr' => 82 array( 83 'title' => 'french title', 84 'body' => 'french body', 85 'slug' => '', 86 ), 87 'id' => null, 88 'created_at' => null, 89 'updated_at' => null, 90 ); 91 92 $t->is($articleForm->getValues(), $values); 93 94 $articleForm->save(); 95 96 $expected = array( 97 'id' => $article->id, 98 'author_id' => $article->Author->id, 99 'is_on_homepage' => true, 100 'created_at' => $article->created_at, 101 'updated_at' => $article->updated_at, 102 'Translation' => 103 array( 104 'en' => 105 array( 106 'id' => $article->id, 107 'title' => 'english title', 108 'body' => 'english body', 109 'lang' => 'en', 110 'slug' => 'english-title', 111 ), 112 'fr' => 113 array( 114 'id' => $article->id, 115 'title' => 'french title', 116 'body' => 'french body', 117 'lang' => 'fr', 118 'slug' => 'french-title', 119 ), 120 ), 121 'Author' => 122 array( 123 'id' => $article->Author->id, 124 'name' => 'i18n author test', 125 ), 126 ); 127 128 $t->is($article->toArray(true), $expected); 129 130 $articleForm->updateDefaultsFromObject(); 131 132 $expected = array( 133 'id' => $article->id, 134 'author_id' => $article->author_id, 135 'is_on_homepage' => true, 136 'created_at' => $article->created_at, 137 'updated_at' => $article->updated_at, 138 'en' => 139 array( 140 'id' => $article->id, 141 'title' => 'english title', 142 'body' => 'english body', 143 'lang' => 'en', 144 'slug' => 'english-title', 145 ), 146 'fr' => 147 array( 148 'id' => $article->id, 149 'title' => 'french title', 150 'body' => 'french body', 151 'lang' => 'fr', 152 'slug' => 'french-title', 153 ), 154 'Author' => 155 array( 156 'id' => $article->Author->id, 157 'name' => 'i18n author test', 158 ), 159 ); 160 161 $t->is($articleForm->getDefaults(), $expected); plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/config/ProjectConfiguration.class.php
r11997 r12955 17 17 { 18 18 chdir(sfConfig::get('sf_root_dir')); 19 $task = new sfDoctrineDropDbTask($this->dispatcher, new sfFormatter());20 $task->run(array(), array('--no-confirmation', '--env=test'));21 19 22 20 $task = new sfDoctrineBuildAllTask($this->dispatcher, new sfFormatter()); plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/config/doctrine/schema.yml
r11948 r12955 4 4 I18n: 5 5 fields: [title, body] 6 actAs: Sluggable 6 actAs: 7 Sluggable: 8 fields: [title] 7 9 Timestampable: 8 10 columns: 9 11 author_id: integer 12 is_on_homepage: boolean 10 13 title: string(255) 11 14 body: string(255) plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/data/fixtures/fixtures.yml
r11948 r12955 3 3 Article_1: 4 4 Author: jwage 5 is_on_homepage: false 5 6 Translation: 6 7 en: plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/data/sql/schema.sql
r12543 r12955 1 1 CREATE TABLE doctrine__record__abstract (id INTEGER PRIMARY KEY AUTOINCREMENT); 2 CREATE TABLE article (id INTEGER PRIMARY KEY AUTOINCREMENT, author_id INTEGER, created_at DATETIME, updated_at DATETIME);2 CREATE TABLE article (id INTEGER PRIMARY KEY AUTOINCREMENT, author_id INTEGER, is_on_homepage INTEGER, created_at DATETIME, updated_at DATETIME); 3 3 CREATE TABLE author (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255)); 4 4 CREATE TABLE subscription (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), status VARCHAR(255)); plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/lib/filter/doctrine/base/BaseArticleFormFilter.class.php
r12650 r12955 15 15 { 16 16 $this->setWidgets(array( 17 'author_id' => new sfWidgetFormDoctrineChoice(array('model' => 'Author', 'add_empty' => true)), 18 'created_at' => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => true)), 19 'updated_at' => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => true)), 17 'author_id' => new sfWidgetFormDoctrineChoice(array('model' => 'Author', 'add_empty' => true)), 18 'is_on_homepage' => new sfWidgetFormChoice(array('choices' => array('' => 'yes or no', 1 => 'yes', 0 => 'no'))), 19 'created_at' => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => true)), 20 'updated_at' => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => true)), 20 21 )); 21 22 22 23 $this->setValidators(array( 23 'author_id' => new sfValidatorDoctrineChoice(array('required' => false, 'model' => 'Author', 'column' => 'id')), 24 'created_at' => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))), 25 'updated_at' => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))), 24 'author_id' => new sfValidatorDoctrineChoice(array('required' => false, 'model' => 'Author', 'column' => 'id')), 25 'is_on_homepage' => new sfValidatorChoice(array('required' => false, 'choices' => array('', 1, 0))), 26 'created_at' => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))), 27 'updated_at' => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false)))), 26 28 )); 27 29 … … 41 43 { 42 44 return array( 43 'id' => 'Text', 44 'author_id' => 'ForeignKey', 45 'created_at' => 'Date', 46 'updated_at' => 'Date', 45 'id' => 'Text', 46 'author_id' => 'ForeignKey', 47 'is_on_homepage' => 'Boolean', 48 'created_at' => 'Date', 49 'updated_at' => 'Date', 47 50 ); 48 51 } plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/lib/form/doctrine/base/BaseArticleForm.class.php
r11948 r12955 13 13 { 14 14 $this->setWidgets(array( 15 'id' => new sfWidgetFormInputHidden(), 16 'author_id' => new sfWidgetFormDoctrineSelect(array('model' => 'Author', 'add_empty' => true)), 17 'created_at' => new sfWidgetFormDateTime(), 18 'updated_at' => new sfWidgetFormDateTime(), 15 'id' => new sfWidgetFormInputHidden(), 16 'author_id' => new sfWidgetFormDoctrineSelect(array('model' => 'Author', 'add_empty' => true)), 17 'is_on_homepage' => new sfWidgetFormInputCheckbox(), 18 'created_at' => new sfWidgetFormDateTime(), 19 'updated_at' => new sfWidgetFormDateTime(), 19 20 )); 20 21 21 22 $this->setValidators(array( 22 'id' => new sfValidatorDoctrineChoice(array('model' => 'Article', 'column' => 'id', 'required' => false)), 23 'author_id' => new sfValidatorDoctrineChoice(array('model' => 'Author', 'required' => false)), 24 'created_at' => new sfValidatorDateTime(array('required' => false)), 25 'updated_at' => new sfValidatorDateTime(array('required' => false)), 23 'id' => new sfValidatorDoctrineChoice(array('model' => 'Article', 'column' => 'id', 'required' => false)), 24 'author_id' => new sfValidatorDoctrineChoice(array('model' => 'Author', 'required' => false)), 25 'is_on_homepage' => new sfValidatorBoolean(array('required' => false)), 26 'created_at' => new sfValidatorDateTime(array('required' => false)), 27 'updated_at' => new sfValidatorDateTime(array('required' => false)), 26 28 )); 27 29 plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/lib/model/doctrine/Author.class.php
r11948 r12955 14 14 { 15 15 $this->assignIdentifier($author->identifier()); 16 $this->refresh();17 16 } else { 18 17 return $this->_set('name', $name); plugins/sfDoctrinePlugin/trunk/test/functional/fixtures/lib/model/doctrine/base/BaseArticle.class.php
r11948 r12955 10 10 $this->setTableName('article'); 11 11 $this->hasColumn('author_id', 'integer', null, array('type' => 'integer')); 12 $this->hasColumn('is_on_homepage', 'boolean', null, array('type' => 'boolean')); 12 13 $this->hasColumn('title', 'string', 255, array('type' => 'string', 'length' => '255')); 13 14 $this->hasColumn('body', 'string', 255, array('type' => 'string', 'length' => '255')); … … 20 21 21 22 $i18n0 = new Doctrine_Template_I18n(array('fields' => array(0 => 'title', 1 => 'body'))); 22 $sluggable1 = new Doctrine_Template_Sluggable( );23 $sluggable1 = new Doctrine_Template_Sluggable(array('fields' => array(0 => 'title'))); 23 24 $i18n0->addChild($sluggable1); 24 25 $timestampable0 = new Doctrine_Template_Timestampable(); plugins/sfDoctrinePlugin/trunk/test/unit/sfDoctrineDatabaseTest.php
r11787 r12955 9 9 */ 10 10 11 require_once(dirname(__FILE__).'/../bootstrap/unit.php');11 include(dirname(__FILE__).'/../bootstrap/unit.php'); 12 12 13 13 $t = new lime_test(1, new lime_output_color());