Views: 3406
Last Modified: 30.03.2022
Getting property values requires only their names listed in query:
$elements = $iblock->getEntityDataClass()::getList([
'select' => ['ID', 'SOME_FIELD', 'ANOTHER_FIELD.ELEMENT']
])->fetchCollection();
foreach ($elements as $element)
{
echo $element->getSomeField()->getValue();
echo $element->getAnotherField()->getElement()->getTitle();
}
Standard ORM relations mechanics are supported.
When filtering, keep in mind the property structure. Property field is a link (Reference or OneToMany) to a complete entity, that's why the property name indicated in filter leads to nothing:
// incorrect example
$element = $iblock->getEntityDataClass()::query()
->where('SOME_FIELD', 'some value')
->fetchObject();
Property value is stored in the entity field, always named as VALUE. That's why it's correct to indicate it specifically in filter:
// correct example
$element = $iblock->getEntityDataClass()::query()
->where('SOME_FIELD.VALUE', 'some value')
->fetchObject();
You can use both constructor of corresponding class for creating new object,
$newElement = new \Bitrix\Iblock\Elements\EO_ElementLink;
as well as entity factory.
$newElement = $iblock->getEntityDataClass()::createObject();
Property values and property descriptions are edited directly via property object:
// setting string value
$element->getSomeString()->setValue('new value');
// setting description
$element->getSomeString()->setDescription('new descr');
// setting element binding
$element->getSomeElement()->setElement($anotherElement);
Additionally, you can set the value directly in the property field:
$element->setSomeString('new value');
You can also use pseudo-object for property value Bitrix\Iblock\ORM\PropertyValue
:
use Bitrix\Iblock\ORM\PropertyValue;
// value only
$value = new PropertyValue('new value');
// value and description
$value = new PropertyValue('new value', 'new descr');
// setting value/description
$element->setSomeString($value);
Setting values for multiple properties operates similarly with the only difference that it's performed for OneToMany and not for Reference:
use Bitrix\Iblock\ORM\PropertyValue;
foreach ($element->getOtherField() as $value)
{
$value->setValue('new value');
$value->setDescription('new descr');
}
$element->addToOtherField(new PropertyValue('new value'));
$element->addToOtherField(new PropertyValue('new value', 'new descr'));
Element object is saved in the same manner as any other ORM object:
$element->save();
Despite the property values being actually stored in different tables as relations with object, everything will be placed in correct locations upon saving.
You can Delete element via the 'delete' object-method:
$element->delete();
Property values are processed automatically when deleting as well as saving. Section associations are deleted as well.