What I am trying to achieve: After creating or updating a record (MySQL), I want to index it in ElasticSearch. The data that I am updating or creating, has many relations with other tables. I have a custom toArray() method that retrieves data from relations and merge with toArray() from the current model.
My problem (or is something that I am missing) is that if I try to do something like this:
public function afterCreate()
{
$data = $this->toArray();
// ... code, code, code
$client->index($data);
}
I get an exception regarding the relations. So for now, I can store the data in ES, by doing this:
public function addAction()
{
// ...
if ($last_record_id = $this->persistent->get('es_last_record_id')) {
$data = Record::findFirstById($last_record_id);
// ...
$client->index($data);
}
// ...
}
public function createAction()
{
// ...
if (true === $record->create()) {
$this->persistent->set('es_last_record_id', $record->getId());
}
}
I don't necessarily complain about this approach, but there might be a problem when I need to do conditional operations. For example implement a business rule that will delete a record from ES if that record becomes inactive. Anyway, my question is related to "toArray" and "afterCreat/afterUpdate" . Anyone had similar problems ?