Forums › Forums › OroCRM › OroCRM – Programming Questions › Add custom Field into OroCRM Entity?
This topic contains 11 replies, has 4 voices, and was last updated by hemham914 9 years, 6 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- May 5, 2014 at 6:28 am #24757
How I can add a custom field in OroCRMAccountBundle:Account ?
I want add a field into my personal Bundle (I don’t want modify the OroCRM Bundle, but extends the Entity into my Bundle). - CreatorTopic
- AuthorReplies
- May 6, 2014 at 6:44 pm #24758
You can add your fields as extended fields. Please take a look how it is done in Account Bundle.
May 7, 2014 at 1:53 am #24759Yes I’ve read the documentation and I’ve made the migration (new field into the database) but how I can work with this new field?
I’ve added two fields into orocrm_account but how I can work with them (with getRepository etc) ?
Thanks.
May 8, 2014 at 2:22 am #24760You can use extended field as regular fields. For instance if you have added ‘test’ field in Account entity, you can do something like this:
$account = $doctrine->getRepository('OroCRMAccountBundle:Account')->find($accountId);
$account->getTest();
May 13, 2014 at 3:16 am #24761I’ve wrote this :
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556<?phpnamespace Relance\Bundle\AccountBundle\Entity;use OroCRM\Bundle\AccountBundle\Entity\Account as AccountOROCRM;class Account extends AccountOROCRM {/*** @var integer** @ORM\Column(name="external_id", type="integer", nullable=false)*/private $external_id;/*** @var boolean** @ORM\Column(name="isActiveReminder", type="boolean", nullable=true, options={"default":true})*/private $isActiveReminder;/*** @param int $external_id*/public function setExternalId($external_id){$this->external_id = $external_id;}/*** @return int*/public function getExternalId(){return $this->external_id;}/*** @param boolean $isActiveReminder*/public function setIsActiveReminder($isActiveReminder){$this->isActiveReminder = $isActiveReminder;}/*** @return boolean*/public function getIsActiveReminder(){return $this->isActiveReminder;}}The error message:
123Class "Relance\Bundle\AccountBundle\Entity\Account" sub class of "OroCRM\Bundle\AccountBundle\Entity\Account" is not a valid entity or mapped super class.I don’t know how to make. Thanks.
May 13, 2014 at 3:28 am #24762I’ve changed my code:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980<?phpnamespace Relance\Bundle\AccountBundle\Entity;use OroCRM\Bundle\AccountBundle\Entity\Account as AccountOROCRM;use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\Config;use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\ConfigField;use Oro\Bundle\DataAuditBundle\Metadata\Annotation as Oro;use Doctrine\ORM\Mapping as ORM;use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap;/*** @ORM\Entity* @ORM\Table(name="orocrm_account")*/class Account extends AccountOROCRM {/*** @ORM\Id* @ORM\Column(type="integer")* @ORM\GeneratedValue(strategy="AUTO")* @Soap\ComplexType("int", nillable=true)*/protected $id;/*** @var integer** @ORM\Column(name="external_id", type="integer", nullable=false)*/private $external_id;/*** @var boolean** @ORM\Column(name="isActiveReminder", type="boolean", nullable=true, options={"default":true})*/private $isActiveReminder;/*** @return int*/public function getId(){return $this->id;}/*** @param int $external_id*/public function setExternalId($external_id){$this->external_id = $external_id;}/*** @return int*/public function getExternalId(){return $this->external_id;}/*** @param boolean $isActiveReminder*/public function setIsActiveReminder($isActiveReminder){$this->isActiveReminder = $isActiveReminder;}/*** @return boolean*/public function getIsActiveReminder(){return $this->isActiveReminder;}And when I test this = fail.
12345678910111213class LocalController extends Controller{public function indexAction(){/** @var $account Account */$account = $this->getDoctrine()->getRepository('RelanceAccountBundle:Account')->find(52);$external_id = $account->getExternalId();return $this->render('RelanceLocalBundle:Local:index.html.twig', array('external_id' => $external_id,));}Help please ^^ thanks.
May 13, 2014 at 3:28 am #24763Error message :
1SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.id' in 'where clause'May 19, 2014 at 1:30 am #24764No ideas?
Thanks.May 19, 2014 at 4:49 pm #24765Unfortunately you cannot extend your entity class from Account entity. But if you need to add some additional fields to Account entity just add them in a migration for your bundle, for instance:
123456789101112131415161718192021222324252627282930313233343536373839404142<?phpnamespace Relance\Bundle\AccountBundle\Migrations\Schema\v1_0;use Doctrine\DBAL\Schema\Schema;use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;use Oro\Bundle\MigrationBundle\Migration\Migration;use Oro\Bundle\MigrationBundle\Migration\QueryBag;class ExtendAccountEntity implements Migration{/*** {@inheritdoc}*/public function up(Schema $schema, QueryBag $queries){$table = $schema->getTable('orocrm_account');$table->addColumn('external_id','integer',['oro_options' => ['extend' => ['is_extend' => true, 'owner' => ExtendScope::OWNER_CUSTOM],'datagrid' => ['is_visible' => false],]]);$table->addColumn('isActiveReminder','boolean',[['notnull' => false],'oro_options' => ['extend' => ['is_extend' => true, 'owner' => ExtendScope::OWNER_CUSTOM],'datagrid' => ['is_visible' => false],]]);}}Next, to update the database run oro:migration:load command:
123php app/console oro:migration:load --show-queriesAlso you need to add translations for new fields in Resources/translations/messages.en.yml. In this case:
123456orocrm:account:external_id.label: External Idis_active_reminder.label: Is Active ReminderAfter this new fields will be displayed on account view and edit pages automatically.
May 19, 2014 at 5:06 pm #24766@ruizalexandre can you please describe your use case with more details as we would like to understand better your needs. Alternative solution to custom field could be one to one relation.
May 20, 2014 at 1:20 am #24767I’ll try the Vova Soroka’s method, thanks you.
September 26, 2014 at 2:17 pm #24768I’ve attempted to implement this process in my application like so:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253<?phpnamespace Stc\Bundle\SalesBundle\Migrations\Schema\v1_99;use Doctrine\DBAL\Schema\Schema;use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtension;use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtensionAwareInterface;use Oro\Bundle\MigrationBundle\Migration\Migration;use Oro\Bundle\MigrationBundle\Migration\QueryBag;class StcSalesBundle implements Migration, ExtendExtensionAwareInterface{protected $extendExtension;public function setExtendExtension(ExtendExtension $extendExtension){$this->extendExtension = $extendExtension;}public function up(Schema $schema, QueryBag $queries){$table = $schema->getTable('orocrm_sales_lead');$this->extendExtension->addOneToManyRelation($schema,$table,'leads_bands','stc_bands',['name'],['description','tributeto'],['name'],['extend' => ['is_extend' => true]]);$this->extendExtension->addManyToManyRelation($schema,$table,'leads_venues','stc_venues',['name'],['description'],['id'],['extend' => ['is_extend' => true]]);}}It seems to work fine when i run
oro:migration:load --force --show-queries
, however when I update the schema with theoro:entity-extend:update-schema
command, I get the following error:12345678910[Doctrine\DBAL\DBALException]An exception occurred while executing 'DROP INDEX UNIQ_73DB4633261574D ON orocrm_sales_lead':SQLSTATE[HY000]: General error: 1025 Error on rename of './oro_crm/#sql-75c_2bbe3' to './oro_crm/orocrm_sales_lead' (errno: 150)[PDOException]SQLSTATE[HY000]: General error: 1025 Error on rename of './oro_crm/#sql-75c_2bbe3' to './oro_crm/orocrm_sales_lead' (errno: 150)Any ideas?
~Coding my life away~
- AuthorReplies
The forum ‘OroCRM – Programming Questions’ is closed to new topics and replies.