Forums › Forums › OroCRM › OroCRM – How do I? Questions › alter properties in custom entity field
This topic contains 4 replies, has 3 voices, and was last updated by dnahrebecki 6 years, 5 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- October 26, 2017 at 4:00 am #29427
Hi,
I added a field with migration to OroCalendarBundle, my problem is that initially it is not a required field but now this decision causes some problems and I would like to make this field mandatory. the problem is that it’s an enum_field (foreign keys). Can I change the field’s property in a migrationBundle and what is the syntaxThank’s for your help
- CreatorTopic
- AuthorReplies
- October 26, 2017 at 6:23 am #29428
Hi, Fred33.
Please, provide your migration code (where you added the field in OroCalendarBundle, as you said). It lets understand what, actually, entity did you mean, etc.
Thank you.
October 26, 2017 at 7:37 am #29429here,
PHP123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132<?phpnamespace CL\Bundle\ExtendCalendarBundle\Migrations\Schema\v1_20;use Doctrine\DBAL\Schema\Schema;use Doctrine\DBAL\Types\Type;use Symfony\Component\DependencyInjection\ContainerAwareInterface;use Symfony\Component\DependencyInjection\ContainerInterface;use Oro\Bundle\EntityBundle\EntityConfig\DatagridScope;use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;use Oro\Bundle\EntityExtendBundle\Migration\ExtendOptionsManager;use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtension;use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtensionAwareInterface;use Oro\Bundle\EntityExtendBundle\Migration\OroOptions;use Oro\Bundle\MigrationBundle\Migration\Migration;use Oro\Bundle\MigrationBundle\Migration\OrderedMigrationInterface;use Oro\Bundle\MigrationBundle\Migration\ParametrizedSqlMigrationQuery;use Oro\Bundle\MigrationBundle\Migration\QueryBag;class AddTypeEvt implementsMigration,ExtendExtensionAwareInterface,ContainerAwareInterface,OrderedMigrationInterface{/** @var ContainerInterface */protected $container;/** @var ExtendExtension */protected $extendExtension;/*** {@inheritdoc}*/public function getOrder(){return 1;}/*** @param ExtendExtension $extendExtension*/public function setExtendExtension(ExtendExtension $extendExtension){$this->extendExtension = $extendExtension;}/*** {@inheritdoc}*/public function setContainer(ContainerInterface $container = null){$this->container = $container;}/*** {@inheritdoc}*/public function up(Schema $schema, QueryBag $queries) {/** @var ExtendOptionsManager $extendOptionsManager */$extendOptionsManager = $this->container->get('oro_entity_extend.migration.options_manager');$extendOptionsManager->removeColumnOptions('oro_calendar_event', 'typEvt');$immutableCodes = ['Rdv', 'Catalogue', 'Rapport_de_visite', 'Autre'];self::addTypeEvtField($schema, $this->extendExtension, $immutableCodes);$types = ['Rdv' => 'Rdv','Catalogue' => 'Catalogue','Rapport_de_visite' => 'Rapport de visite','Autre' => 'Autre',];self::addEnumValuesTypEvt($queries, $types);}/*** @param Schema $schema* @param ExtendExtension $extendExtension* @param array $immutableCodes*/public static function addTypeEvtField(Schema $schema, ExtendExtension $extendExtension, array $immutableCodes){$enumtable = $extendExtension->addEnumField($schema,'oro_calendar_event','typEvt','typEvt',false,false,['extend' => ['owner' => ExtendScope::OWNER_CUSTOM],'datagrid' => ['is_visible' => DatagridScope::IS_VISIBLE_TRUE],'dataaudit' => ['auditable' => true]]);$options = new OroOptions();$options->set('enum','immutable_code',$immutableCodes);$enumtable->addOption(OroOptions::KEY, $options);}public static function addEnumValuesTypEvt(QueryBag $queries, array $types, $defaultValue = 'Magasin'){$query = 'INSERT INTO oro_enum_typEvt (id, name, priority, is_default)VALUES (:id, :name, :priority, :is_default)';$i = 1;foreach ($types as $key => $value){$dropFieldsQuery = new ParametrizedSqlMigrationQuery();$dropFieldsQuery->addSql($query,['id' => $key, 'name' => $value, 'priority' => $i, 'is_default' => $defaultValue === $key],['id' => Type::STRING,'name' => Type::STRING,'priority' => Type::INTEGER,'is_default' => Type::BOOLEAN]);$queries->addQuery($dropFieldsQuery);$i++;}}}October 27, 2017 at 12:37 am #29430I think i have found a solution. Add this code
PHP12345678910->add('typEvt','oro_enum_select',['label' => 'Type Evenement','enum_code' => 'typEvt','required' => true,'constraints' => [new Assert\NotNull()]])to function builForm in CalendarEventType.php form. I tested it in the original bundle but how to extend this form
Thanks
November 3, 2017 at 6:52 am #29431Hello fred33,
there are couple of ways of how you can extend Form functionality depending on your needs and existing codebase. Please have a look at:– https://symfony.com/doc/2.8/form/create_form_type_extension.html
– https://symfony.com/doc/2.8/form/create_custom_field_type.html
– https://symfony.com/doc/2.8/form/dynamic_form_modification.htmlRegards,
Daniel Nahrebecki - AuthorReplies
The forum ‘OroCRM – How do I? Questions’ is closed to new topics and replies.