Skip to content

Commit

Permalink
accept read-only models in ModelChoiceList
Browse files Browse the repository at this point in the history
  • Loading branch information
havvg committed Mar 30, 2012
1 parent e18fcd8 commit 97ba218
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Propel1\Form\ChoiceList;

use \BaseObject;
use \Persistent;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Exception\StringCastException;
Expand Down Expand Up @@ -343,6 +344,11 @@ private function getIdentifierValues($model)
return array($model->getPrimaryKey());
}

// readonly="true" models do not implement Persistent.
if ($model instanceof BaseObject and method_exists($model, 'getPrimaryKey')) {
return array($model->getPrimaryKey());
}

return $model->getPrimaryKeys();
}
}
27 changes: 27 additions & 0 deletions src/Symfony/Bridge/Propel1/Tests/Fixtures/ReadOnlyItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* this file is part of the symfony package.
*
* (c) fabien potencier <fabien@symfony.com>
*
* for the full copyright and license information, please view the license
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Propel1\Tests\Fixtures;

use \PropelPDO;

class ReadOnlyItem extends \BaseObject
{
public function getName()
{
return 'Marvin';
}

public function getPrimaryKey()
{
return 42;
}
}
27 changes: 27 additions & 0 deletions src/Symfony/Bridge/Propel1/Tests/Fixtures/ReadOnlyItemQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* this file is part of the symfony package.
*
* (c) fabien potencier <fabien@symfony.com>
*
* for the full copyright and license information, please view the license
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Propel1\Tests\Fixtures;

class ReadOnlyItemQuery
{
public function getTableMap()
{
// Allows to define methods in this class
// to avoid a lot of mock classes
return $this;
}

public function getPrimaryKeys()
{
return array('id');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
use Symfony\Bridge\Propel1\Tests\Fixtures\Item;
use Symfony\Bridge\Propel1\Tests\Fixtures\ReadOnlyItem;
use Symfony\Bridge\Propel1\Tests\Propel1TestCase;

class ModelChoiceListTest extends Propel1TestCase
Expand All @@ -38,6 +39,20 @@ public function testEmptyChoicesReturnsEmpty()
$this->assertSame(array(), $choiceList->getChoices());
}

public function testReadOnlyIsValidChoice()
{
$item = new ReadOnlyItem();
$choiceList = new ModelChoiceList(
'\Symfony\Bridge\Propel1\Tests\Fixtures\ReadOnlyItem',
'name',
array(
$item,
)
);

$this->assertSame(array(42 => $item), $choiceList->getChoices());
}

public function testFlattenedChoices()
{
$item1 = new Item(1, 'Foo');
Expand Down

0 comments on commit 97ba218

Please sign in to comment.