Development

#3050 (Loading easily a tree from data/fixtures)

You must first sign up to be able to contribute.

Ticket #3050 (new enhancement)

Opened 9 months ago

Last modified 5 months ago

Loading easily a tree from data/fixtures

Reported by: jug Assigned to: trivoallan
Priority: minor Milestone:
Component: sfPropelActAsNestedSetBehaviorPlugin Version:
Keywords: Cc:
Qualification: Accepted

Description

The following permit to easily load tree datas with propel-load-data command

We have the following class : config/schema.yml (using sfPropelAlternativeSchemaBehavior) :

classes:
  Family:
    columns:
      name: varchar(255)
      tree_scope: integer
      tree_parent: { type: integer, foreignClass: MyTreeClass, foreignReference: id, onDelete: cascade }
      tree_left: integer
      tree_right: integer
    behaviors:
      actasbested:
        columns:
          scope: tree_scope
          parent: tree_parent
          left: tree_left
          right: tree_right

And we want to create this tree :

                 Arnold
           ________|______
          |                      |
        Bob              Emilie
     ____|_____
    |              |
 Clara    Dave

So we just define our datas like this fixtures/test_data.yml :

Familly:
  A:
    name: Arnold
  B:
    name: Bob
    tree_parent: A
  C:
    name: Clara
    tree_parent: B
  D:
    name: Dave
    tree_parent: B
  E:
    name: Emilie
    tree_parent: A

And the command symfony propel-build-all-load myapp will do the rest thanks to this piece of code replacement of sfPropelActAsNestedBehavior::preSave function :

  public function preSave(BaseObject $node)
  {
    // If left or right value are missing, there is a problem !
    if ( !($node->getLeftValue() and $node->getRightValue()) )
    {
      // If parent id is set, we will correct the values of left and right
      if ( $node->getParentIdValue() )
      {
        $parent_node = call_user_func( array( get_class($node->getPeer()), 'retrieveByPk' ), $node->getParentIdValue() );
        $node->insertAsLastChildOf( $parent_node );
      }
      // If no parent is set, let's make it at least root!
      else
      {
        $node->makeRoot();
      }
    }
    $this->processPreSaveStack();
  }

Change History

03/06/08 14:05:39 changed by trivoallan

  • version deleted.
  • qualification changed from Unreviewed to Design decision.

Excellent idea. implementation seems fine to me. But we need a proper patch.

07/18/08 15:57:41 changed by Blade McKain

  • qualification changed from Design decision to Accepted.

Yeah. This code works great! Please add to the stable sfPropelActAsNestedSetBehaviorPlugin