best practice to add related data

  • Hi,

    i have to create an extension with many tables and relations.

    Now i know how to fetch related data but i don't know how to set some related data.

    Can you give me advice how to do that with the different relation types?

  • Just to be sure:

    You got something like:


    My Item

    - ID

    - Title

    - @hasMany things

    Your question is how to store the hasMany-relation in this example, right?

    As far as I remember this will be stored automatically if your saved object has a property that matches the relation's name. But I will have a look at it as soon as I am back on my mac.

  • Yes, that's exactly what i mean :-)

    I also thought so, but i tried it and the value was still not saved...

    then i also have something like that:

    Now i want to save the complete order...

  • i need to store the price additionally to prevent changing previous orders in case of price-changes.

  • here you can see my API Method for saving... I really don't know how to optimize that...

  • Hello,

    I just discovered that I already handled something like that almost a year ago.

    I got a model states that hasMany steps.

    The StateApiController has the following save-method:

    The saveSteps-method is implemented in a StateTrait.php like that:

    Just for understanding, these are the add- and remove-methods for the steps:

    1.         public function addStep( Step $step )
    2. {
    3. $this->steps[ $step->id ] = $step;
    4. }
    5. public function removeStep( Step $step )
    6. {
    7. unset( $this->steps[ $step->id ] );
    8. }

    $this->steps is declared as follows:

    1. /**
    2. * @HasMany(targetEntity="Step", keyFrom="id", keyTo="state_id")
    3. * @OrderBy({"date" = "DESC"})
    4. * @var Step[]
    5. */
    6. public $steps;

    Well, let's check out, what Bixie did once:

    He also declares the relation in the model. Well - and that's all: He fetches the relation in his controller but I do not see a specific save-Method for this.

    I will check that out (need to read a lot of code for that ;) ) - maybe MalteScharenberg or saschad can help out here?

  • ok, i solved my problem by saving all entities after each other. There is no mechanism for saving all related data, build in pagekit. I have to memory all new id's and i will delete all stuff in case of an error during the saving process. So i have no fragmented data in my database.