accept read-only models in ModelChoiceList

This commit is contained in:
Toni Uebernickel 2012-03-30 10:49:44 +02:00
parent e18fcd852a
commit 97ba2189c9
4 changed files with 75 additions and 0 deletions

View File

@ -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;
@ -343,6 +344,11 @@ class ModelChoiceList extends ObjectChoiceList
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();
}
}

View File

@ -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;
}
}

View File

@ -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');
}
}

View File

@ -14,6 +14,7 @@ namespace Symfony\Bridge\Propel1\Tests\Form\ChoiceList;
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
@ -38,6 +39,20 @@ class ModelChoiceListTest extends Propel1TestCase
$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');