Hello,
I've been working on a custom "beforeSave" hook, the aim is to invoke dynamic logic on the server side for the rules defined for an entity so that the same logic would also be triggered during REST API calls (no UI).
Inside the hook method, the data for an entity is read using:
$data = $entity->getValueMap();
It works perfectly for the "Create" scenario, but it fails during the "Update" operation when the target entity has a link of "many-to-many" type to another (in my case, it is Contact -> Subaccount) and the payload doesn't include this data for the linked entity. For example, I am going to modify the description with REST API call like this:
PUT http://localhost:8080/api/v1/Contact/64b7f1f4c15f5ea87
Content-Type: application/json
X-Api-Key: cfb6ac9c33a56b79a8c31c17eece70c1
{
"description": "sample text as a description"
}
This way, the rule, which is based on a value of Contact.subAccount attribute would provide a false result considering the subaccount is empty (and it should not be), and the reason for this is - Entity::getValueMap() method doesn't set the proper value for Contact.subaccountsIds in this case
$contactEntity->hasAttribute('subaccountsIds'); /* returns true */
$contactEntity->get('subaccountsIds'); /* returns null even though SubAccount is defined for this entity */
Hence, a rule which ensures that Contact.subAccount attribute is not empty would always fail.
Perhaps, I am doing something wrong and there is a better approach, which might allow getting entity's data for joined entities without looping through the list of attributes ($entity->getRelationList() )
Please advise.
I've been working on a custom "beforeSave" hook, the aim is to invoke dynamic logic on the server side for the rules defined for an entity so that the same logic would also be triggered during REST API calls (no UI).
Inside the hook method, the data for an entity is read using:
$data = $entity->getValueMap();
It works perfectly for the "Create" scenario, but it fails during the "Update" operation when the target entity has a link of "many-to-many" type to another (in my case, it is Contact -> Subaccount) and the payload doesn't include this data for the linked entity. For example, I am going to modify the description with REST API call like this:
PUT http://localhost:8080/api/v1/Contact/64b7f1f4c15f5ea87
Content-Type: application/json
X-Api-Key: cfb6ac9c33a56b79a8c31c17eece70c1
{
"description": "sample text as a description"
}
This way, the rule, which is based on a value of Contact.subAccount attribute would provide a false result considering the subaccount is empty (and it should not be), and the reason for this is - Entity::getValueMap() method doesn't set the proper value for Contact.subaccountsIds in this case
$contactEntity->hasAttribute('subaccountsIds'); /* returns true */
$contactEntity->get('subaccountsIds'); /* returns null even though SubAccount is defined for this entity */
Hence, a rule which ensures that Contact.subAccount attribute is not empty would always fail.
Perhaps, I am doing something wrong and there is a better approach, which might allow getting entity's data for joined entities without looping through the list of attributes ($entity->getRelationList() )
Please advise.
Comment