merged branch havvg/hotfix/propel-modelchoice-readonly-models (PR #3731)
Commits -------64c7183
clean file docs in Propel1 fixtures97ba218
accept read-only models in ModelChoiceList Discussion ---------- accept read-only models in ModelChoiceList Ping @willdurand --------------------------------------------------------------------------- by willdurand at 2012-03-30T08:57:14Z 👍
This commit is contained in:
commit
d6330e1bb3
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* this file is part of the symfony package.
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) fabien potencier <fabien@symfony.com>
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* for the full copyright and license information, please view the license
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* this file is part of the symfony package.
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) fabien potencier <fabien@symfony.com>
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* for the full copyright and license information, please view the license
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
|
27
src/Symfony/Bridge/Propel1/Tests/Fixtures/ReadOnlyItem.php
Normal file
27
src/Symfony/Bridge/Propel1/Tests/Fixtures/ReadOnlyItem.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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');
|
||||
}
|
||||
}
|
@ -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');
|
||||
|
Reference in New Issue
Block a user