Creating new entities

There are three different flavors, if we might call them that, by which we can set property (field and attribute) values on our entity. They all lead to the same result. The following few code snippets can be copied and pasted into our Crud class execute method for testing, simply by replacing /* CRUD Code Here */ with one of the following code snippets:

//Simple model, creating new entities, flavour #1
$department1 = $this->departmentFactory->create();
$department1->setName('Finance');
$department1->save();
//Simple model, creating new entities, flavour #2
$department2 = $this->departmentFactory->create();
$department2->setData('name', 'Research');
$department2->save();
//Simple model, creating new entities, flavour #3
$department3 = $this->departmentFactory->create();
$department3->setData(['name' => 'Support']);
$department3->save();

The flavour #1 approach from the preceding code is probably the preferred way of setting properties, as it is using the magic method approach we mentioned previously. Both flavour #2 and flavour #3 use the setData method, just in a slightly different manner. All three examples should yield the same result once the save method is called on an object instance.

Now that we know how to save the simple model, let's take a quick look at doing the same with the EAV model. The following are analogous code snippets:

//EAV model, creating new entities, flavour #1
$employee1 = $this->employeeFactory->create();
$employee1->setDepartment_id($department1->getId());
$employee1->setEmail('[email protected]');
$employee1->setFirstName('Goran');
$employee1->setLastName('Gorvat');
$employee1->setServiceYears(3);
$employee1->setDob('1984-04-18');
$employee1->setSalary(3800.00);
$employee1->setVatNumber('GB123451234');
$employee1->setNote('Note #1');
$employee1->save();

//EAV model, creating new entities, flavour #2
$employee2 = $this->employeeFactory->create();
$employee2->setData('department_id', $department2->getId());
$employee2->setData('email', '[email protected]');
$employee2->setData('first_name', 'Marko');
$employee2->setData('last_name', 'Tunukovic');
$employee2->setData('service_years', 3);
$employee2->setData('dob', '1984-04-18');
$employee2->setData('salary', 3800.00);
$employee2->setData('vat_number', 'GB123451234');
$employee2->setData('note', 'Note #2');
$employee2->save();

//EAV model, creating new entities, flavour #3
$employee3 = $this->employeeFactory->create();
$employee3->setData([
    'department_id' => $department3->getId(),
    'email' => '[email protected]',
    'first_name' => 'Ivan',
    'last_name' => 'Telebar',
    'service_years' => 2,
    'dob' => '1986-08-22',
    'salary' => 2400.00,
    'vat_number' => 'GB123454321',
    'note' => 'Note #3'
]);
$employee3->save();

As we can see, the EAV code for persisting the data is identical to the simple model. There is one thing here worth noting. The Employee entity has a relation defined toward department. Forgetting to specify department_id on a new employee entity save would result in an error message similar to the following:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ('magento'.'foggyline_office_employee_entity', CONSTRAINT 'FK_E2AEE8BF21518DFA8F02B4E95DC9F5AD' FOREIGN KEY ('department_id') REFERENCES 'foggyline_office_department' ('entity_id') ON), query was: INSERT INTO 'foggyline_office_employee_entity' ('email', 'first_name', 'last_name', 'entity_id') VALUES (?, ?, ?, ?)

Magento saves these types of errors under its var/report directory.

Reading existing entities

Reading an entity based on a provided entity ID value comes down to instantiating the entity and using the load method to which we pass the entity ID as shown next:

//Simple model, reading existing entities
$department = $this->departmentFactory->create();
$department->load(28);

/*
    end_Debug::dump($department->toArray());

    array(2) {
      ["entity_id"] => string(2) "28"
      ["name"] => string(8) "Research"
    }
 */

There is no real difference between loading the simple model or EAV model, as shown in the following EAV model example:

//EAV model, reading existing entities
$employee = $this->employeeFactory->create();
$employee->load(25);

/*
    end_Debug::dump($employee->toArray());

    array(10) {
      ["entity_id"] => string(2) "25"
      ["department_id"] => string(2) "28"
      ["email"] => string(14) "[email protected]"
      ["first_name"] => string(5) "Marko"
      ["last_name"] => string(9) "Tunukovic"
      ["dob"] => string(19) "1984-04-18 00:00:00"
      ["note"] => string(7) "Note #2"
      ["salary"] => string(9) "3800.0000"
      ["service_years"] => string(1) "3"
      ["vat_number"] => string(11) "GB123451234"
    }
 */

Notice how the EAV entity loads all of its field and attribute values, which is not always the case when we obtain the entity through EAV collection, as we will show later on.

Updating existing entities

Updating entities comes down to using the load method to read an existing entity, reset its value, and calling the save method in the end, like shown in the following example:

$department = $this->departmentFactory->create();
$department->load(28);
$department->setName('Finance #2');
$department->save();

Regardless of the entity being the simple model or an EAV, the code is the same.

Deleting existing entities

Calling the delete method on a loaded entity will delete the entity from the database or throw Exception if it fails. Code to delete the entity looks as follows:

$employee = $this->employeeFactory->create();
$employee->load(25);
$employee->delete();

There is no difference in deleting the simple and EAV entities. We should always use try/catch blocks when deleting or saving our entities.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.82.79