Views: 2774
Last Modified: 29.03.2022
  • get

    Data reading is implemented using several methods. The most simple returns a field value or null when value is missing (for example, when field is not specified in select when retrieved):

    $book = \Bitrix\Main\Test\Typography\BookTable::getByPrimary(1)
    	->fetchObject();
    
    $title = $book->getTitle();
    
  • require

    When you are sure that field must be completed by a value and the scenario is not viable without this value, you can set this value as mandatory/required by the method require:

    $book = \Bitrix\Main\Test\Typography\BookTable::getByPrimary(1)
    	->fetchObject();
    
    $title = $book->requireTitle();
    

    In this case, the result requireTitle() won't be different from the abovementioned getTitle(). And the next example will finish with thrown exception, because field won't be containing a value:

    $book = \Bitrix\Main\Test\Typography\BookTable::getByPrimary(1, ['select' => ['ID', 'PUBLISHER_ID', 'ISBN']])
    	->fetchObject();
    
    $title = $book->requireTitle();
    // SystemException: "TITLE value is required for further operations"
    
  • remindActual

    One more getter remindActual will be useful when re-setting value to differentiate the original value from the previously set value during the session and not yet saved in the database:

    $book = \Bitrix\Main\Test\Typography\BookTable::getByPrimary(1)
    	->fetchObject();
    
    echo $book->getTitle();
    // prints "Title 1"
    
    $book->setTitle("New title");
    
    echo $book->getTitle();
    // prints "New title"
    
    echo $book->remindActualTitle();
    // prints "Title 1"
    

    As an alternative, you can use universal unnamed methods:

    $fieldName = 'TITLE';
    
    $title = $book->get($fieldName);
    $title = $book->require($fieldName);
    $title = $book->remindActual($fieldName);
    
  • primary

    System "getter" primary is implemented as a virtual read-only property, to avoid using the method getPrimary(), thus reserving the PRIMARY field name with corresponding named getter method. Property returns primary key values in the array's format independently from whether the primary key is or composite or singular:

    $book = \Bitrix\Main\Test\Typography\BookTable::getByPrimary(1)
    	->fetchObject();
    
    $primary = $book->primary;
    // returns ['ID' => 1]
    
    $id = $book->getId();
    // returns 1
    
  • collectValues

    Method collectValues is used to get all object values as an array.

    $book = \Bitrix\Main\Test\Typography\BookTable::getByPrimary(1)
    	->fetchObject();
    
    $values = $book->collectValues();
    

    This example returns all available values. When some field values are re-set using the "setter", but not yet saved, returns these specific values. Unmodified fields source actual values.

    You can use optional filters to specify set of fields and data type:

    $book = \Bitrix\Main\Test\Typography\BookTable::getByPrimary(1)
    	->fetchObject();
    
    $values = $book->collectValues(\Bitrix\Main\ORM\Objectify\Values::ACTUAL);
    // returns only current values, without not yet saved values
    
    $values = $book->collectValues(\Bitrix\Main\ORM\Objectify\Values::CURRENT);
    // returns only current values, not yet saved in the database
    
    $values = $book->collectValues(\Bitrix\Main\ORM\Objectify\Values::ALL);
    // equals to calling collectValues() without parameters - first CURRENT, then ACTUAL
    

    Second argument passes the mask, similar to the used in fill, defining field types:

    $book = \Bitrix\Main\Test\Typography\BookTable::getByPrimary(1)
    	->fetchObject();
    
    $values = $book->collectValues(
    	\Bitrix\Main\ORM\Objectify\Values::CURRENT,
    	\Bitrix\Main\ORM\Fields\FieldTypeMask::SCALAR
    );
    // returns only updated scalar field values
    
    $values = $book->collectValues(
    	\Bitrix\Main\ORM\Objectify\Values::ALL,
    	\Bitrix\Main\ORM\Fields\FieldTypeMask::ALL & ~\Bitrix\Main\ORM\Fields\FieldTypeMask::USERTYPE
    );
    // returns values for all fields, except for user fields
    
  • runtime

    Only universal get is provided for runtime fields created within individual queries:

    $author = \Bitrix\Main\Test\Typography\AuthorTable::query()
    	->registerRuntimeField(
    		new \Bitrix\Main\Entity\ExpressionField(
    			'FULL_NAME', 'CONCAT(%s, " ", %s)', ['NAME', 'LAST_NAME']
    		)
    	)
    	->addSelect('ID')
    	->addSelect('FULL_NAME')
    	->where('ID', 17)
    	->fetchObject();
    
    echo $author->get('FULL_NAME');
    // prints 'Name 17 Last name 17'
    

    Storing of such values is isolated from standard field values inside the object and, correspondingly, the rest of data handling methods are not applicable.





Courses developed by Bitrix24