This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Zend Lucene and ahDoctrineEasyEmbeddedRelationsPlugin

Questions relating to sfLucenePlugin, sfSearch etc

Zend Lucene and ahDoctrineEasyEmbeddedRelationsPlugin

by petersaints » Sat Aug 07, 2010 2:25 pm

I'm having a little problem indexing a field on Zend Lucene. I followed the Day 17 of the Jobeet tutorial and all fields are indexed correctly except one.

The problem is that I have a Publication form that has embedded forms (using the ahDoctrineEasyEmbeddedRelationsPlugin) for another table (the Code table). It's an 1:n relationship from Publications to Codes.

The problem is that once I press save the term that I'm getting from the Codes table isn't being indexed. If I edit the Publication and hit save again it gets indexed. After some testing I narrowed that the problem must be related with the fact that the Publication record is saved first and only after that the new codes are saved and pointed to the correct publication. However the updateLuceneIndex (following the Jobeet Tutorial) is placed on the save method for the Publication model so when the Publication is saved there's no codes to index yet.

I have already tried to call the same method on the save method of the code model but it doesn't fix the problem. I still have to save twice to index any new code that I add.

All I want to know is where can I call the updateLuceneIndex so that it will only index after everything is saved so that it indexes correctly at the first time. Any ideas?

Thank you in advance...

P.S.:
A log to help you helping me (with a few comments):
Code: Select all
The publication is inserted here ---> Ago 07 14:13:52 symfony [info] {Doctrine_Connection_Statement} execute : INSERT INTO publication (has_images, has_handwritten_notes, is_pressed, image_link, genre_id, location_id, collection, provenance, previous_owners, authors_as_printed, normalized_title, title, edition_location, editor, first_edition_characteristics, language_id, translator, description, digital_reproductions, observations, bibliography, facsimiles, images_description, images_author, handwritten_notes_description, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - (1, 1, 1, , 1, 1, , , , , Yeahh Right, , , , , 1, , , , , , , , , , 2010-08-07 14:13:52, 2010-08-07 14:13:52)
The index gets updated here ---> Ago 07 14:13:52 symfony [info] Updating Index
Ago 07 14:13:52 symfony [info] {Doctrine_Connection_Statement} execute : SELECT g.id AS g__id, g.name AS g__name, g.created_at AS g__created_at, g.updated_at AS g__updated_at FROM genre g WHERE (g.id = ?) - (1)
Ago 07 14:13:52 symfony [info] {Doctrine_Connection_Statement} execute : SELECT l.id AS l__id, l.city_id AS l__city_id, l.name AS l__name, l.created_at AS l__created_at, l.updated_at AS l__updated_at FROM location l WHERE (l.id = ?) - (1)
But the code only gets inserted here ---> Ago 07 14:13:52 symfony [info] {Doctrine_Connection_Statement} execute : INSERT INTO code (publication_id, designation, title, collection, date_type, date, code, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) - (3, Vol 1, , , , 2010, , 2010-08-07 14:13:52, 2010-08-07 14:13:52)
----> How can I force the updateLuceneIndex to run after this?!?
petersaints
Junior Member
 
Posts: 23
Joined: Tue Mar 09, 2010 9:19 pm
Location: Portugal

Re: Lucene and ahDoctrineEasyEmbeddedRelationsPlugin

by petersaints » Sat Aug 07, 2010 5:33 pm

I had already tried to update the index on the Code save function but with no effect (it still would "catch" the values before being saved to the database). Fortunately after digging into the Doctrine documentation I found the refreshRelated() method and it saved my day.

The solution is this (put it on the class of the model that is related to the main model that you are indexing):
Code: Select all
public function save(Doctrine_Connection $conn = null)
{
  $conn = $conn ? $conn : $this->getTable()->getConnection();
  $conn->beginTransaction();
  try
  {
    $ret = parent::save($conn);
    if($this->isNew() || $this->isModified()){
    $publicationRecord = $this->getPublication();
    $publicationRecord->refresh();
    $publicationRecord->refreshRelated();
    $publicationRecord->updateLuceneIndex();
    }
    $conn->commit();

    return $ret;
  }
  catch (Exception $e)
  {
    $conn->rollBack();
    throw $e;
  }
}
petersaints
Junior Member
 
Posts: 23
Joined: Tue Mar 09, 2010 9:19 pm
Location: Portugal