This topic contains 3 replies, has 2 voices, and was last updated by tomas.hoch 5 years, 10 months ago.
- Topic
Hello,
I needed to add relation between PrivacyStatement (my own entity) and core entity Contact.
I followed your documentation:
https://oroinc.com/orocrm/doc/2.6/dev-guide/entities/adding-properties
but it didn’t work. So I debugged loading metadata a bit and I found little bit different way how to extend core entities. It works fine so far for Contact entity, but I’m not sure, if it can’t break templates, routes and other stuff, so I would like confirm with you, if I can use following approach for other entities and in rest of our project.1. As first, created migration, which create relation between PrivacyStatement and Contact:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647namespace Tv\Bundle\ContactBundle\Migrations\Schema\v2_7;use Doctrine\DBAL\Schema\Schema;use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtension;use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtensionAwareInterface;use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;use Oro\Bundle\MigrationBundle\Migration\Migration;use Oro\Bundle\MigrationBundle\Migration\QueryBag;use Oro\Bundle\MigrationBundle\Migration\OrderedMigrationInterface;class AddPrivacyStatementToContact implements Migration, OrderedMigrationInterface, ExtendExtensionAwareInterface{/*** @var ExtendExtension*/protected $extendExtension;/*** @param ExtendExtension $extendExtension*/public function setExtendExtension(ExtendExtension $extendExtension){$this->extendExtension = $extendExtension;}/*** {@inheritdoc}*/public function getOrder(){return 2;}public function up(Schema $schema, QueryBag $queries){$this->extendExtension->addManyToOneRelation($schema,'orocrm_contact', // owning side table'agreedPrivacyStatement', // owning side field name'tv_organization_privacy_stmt', // inverse side table'version', // column name is used to show related entity['extend' => ['owner' => ExtendScope::OWNER_CUSTOM]]);}}2. As second, I created my own entity Contact, which extends original \Oro\Bundle\ContactBundle\Entity\Contact, I didn’t create ExtendContact class in Model folder of my bundle as documention shows. When I did that, then it didn’t work. Oro was creating EX_TvContactBundle_Contact proxy class and remove EX_OroContactBundle_Contact proxy class from inheritance chain. Then migrations was failing due to missing property $picture, which is dynamically created in EX_OroContactBundle_Contact from configuration.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465namespace Tv\Bundle\ContactBundle\Entity;use Doctrine\ORM\Mapping as ORM;use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\Config;/*** @ORM\Entity(repositoryClass="Oro\Bundle\ContactBundle\Entity\Repository\ContactRepository")* @ORM\Table(* name="orocrm_contact",* indexes={* @ORM\Index(name="contact_name_idx",columns={"last_name", "first_name", "id"}),* @ORM\Index(name="contact_updated_at_idx",columns={"updatedAt"}),* }* )* @ORM\HasLifecycleCallbacks()* @Config(* routeName="oro_contact_index",* routeView="oro_contact_view",* defaultValues={* "entity"={* "icon"="fa-users",* "contact_information"={* "email"={* {"fieldName"="primaryEmail"}* },* "phone"={* {"fieldName"="primaryPhone"}* }* }* },* "ownership"={* "owner_type"="USER",* "owner_field_name"="owner",* "owner_column_name"="user_owner_id",* "organization_field_name"="organization",* "organization_column_name"="organization_id"* },* "security"={* "type"="ACL",* "group_name"="",* "category"="account_management"* },* "form"={* "form_type"="oro_contact_select",* "grid_name"="contacts-select-grid",* },* "dataaudit"={* "auditable"=true* },* "grid"={* "default"="contacts-grid",* "context"="contacts-for-context-grid"* },* "tag"={* "enabled"=true* },* "merge"={* "enable"=true* }* }* )*/class Contact extends \Oro\Bundle\ContactBundle\Entity\Contact{}When doctrine loads metadata, I have following inheritance chain:
– Oro\Bundle\BusinessEntitiesBundle\Entity\BasePerson
– Extend\Entity\EX_OroContactBundle_Contact (so configuration for Contact entity can be generate and applied)
– Oro\Bundle\ContactBundle\Entity\Contact
– Tv\Bundle\ContactBundle\Entity\Contact
What is exactly what I want.3. As next, I needed force ORO app use my exteded Contact entity instead of original one. So I reconfigure parameter %oro_contact.entity.class:% in services config.
1oro_contact.entity.class: Tv\Bundle\ContactBundle\Entity\Contact4. and for search indexer I needed copy & paste search.yml file from ContactBundle and create my own with my own entity:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687search:Tv\Bundle\ContactBundle\Entity\Contact:alias: oro_contactlabel: oro.contact.entity_plural_labeltitle_fields: [namePrefix, firstName, lastName]route:name: oro_contact_viewparameters:id: idsearch_template: OroContactBundle:Contact:searchResult.html.twigfields:-name: namePrefixtarget_type: texttarget_fields: [namePrefix]-name: firstNametarget_type: texttarget_fields: [firstName]-name: middleNametarget_type: texttarget_fields: [middleName]-name: lastNametarget_type: texttarget_fields: [lastName]-name: nameSuffixtarget_type: texttarget_fields: [nameSuffix]-name: descriptiontarget_type: texttarget_fields: [description]-name: primaryEmailtarget_type: texttarget_fields: [primaryEmail]-name: primaryPhonetarget_type: texttarget_fields: [primaryPhone]-name: faxtarget_type: texttarget_fields: [fax]-name: skypetarget_type: texttarget_fields: [skype]-name: jobTitletarget_type: texttarget_fields: [jobTitle]-name: twittertarget_type: texttarget_fields: [twitter]-name: facebooktarget_type: texttarget_fields: [facebook]-name: linkedIntarget_type: texttarget_fields: [linkedIn]-name: googlePlustarget_type: texttarget_fields: [googlePlus]-name: phonesrelation_type: one-to-manyrelation_fields:-name: phonetarget_type: texttarget_fields: [contactPhones]-name: emailsrelation_type: one-to-manyrelation_fields:-name: emailtarget_type: texttarget_fields: [email]I reached exactly what I wanted:
1. I have own class of Contact entity, so I can override auto-generated methods and implement my own behaviour (for example add additional validations and protect entity against setting into incorrect state)
2. I can write unit tests for code or parts of application, which use Contact entityI’m very new in ORO environment, so I don’t know if my solution has any disadvantages or can crash application in other parts of application. It works nice so far. Original (Core) Oro code uses my new entity, I can create / update / display contact, I can write unit test.
Thanks in advance for your opinion.
The forum ‘OroPlatform – How do I? Questions’ is closed to new topics and replies.