Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
many to many create question
  • First Things First.
    Great thanks for your work!
    I have a question:
    For example, I have a model of a character (Model_Character [id, name]), and I have a model of clothing (Model_Cloth[id, name]). Each Model_Character may or may not have a piece(s) of Model_Cloth (shirt, pants for example), and every piece of clothing can belong to several characters.
    Following the documentation http://docs.fuelphp.com/packages/orm/relations/many_many.html I created a junction table (Character_Cloth[character_id, cloth_id]).
    The very question is how do I create a new character and choose a piece(s) of clothing for him using Fieldset or may be i must do it myself using DB::query.
    Thanks.
    And sorry for my english (google translator).
  • The name of the relation will become an array in the model instance. So for Model_Character, the instance will have an array called 'cloth' (assuming you haven't named the relation differently). For Model_Cloth, it will have an array called 'character'. The array will contain objects for all related records. You remove the relation by unsetting an element in this array, so add to the relation by adding a new related model to the array. This is documented in the example on that same page (relation between users and posts). In case of form input, the post usually results a list of id's of the items selected. Unset the currently related id's not selected on the form, retrieve the related records using the id's posted, and assign the objects in the result set to the relation. Note that you can no operate on the array as a whole, you have to set and unset individual array elements. If you would replace the entire array at once, the set/unset methods will not be called and the relationships will be altered incorrectly.
  • Thank you.
  • So i do it. Thanks again, but got one more problem: i need some custom ordering on "cloth" belongs to "character", for example, someone wants to wear trousers over shorts. So i add one column to my table "Character_Cloth" and name it "order" and now i dont understand how to get that order. Please help.
  • Define your many to many using a 'table_through', and you should be able to use order_by('Character_Cloth.order', 'ASC').
  • Sorry but you may be, misunderstand me, I do not want to keep order in the "Model_Cloth". As each piece of "cloth" may be weared by many other "characters". Sorry for my language (may be here is a problem).
  • And sorry again ))) I will try to explain more: p1. Table Characters:
    id - index
    name - text
    ...and many... p2. Table cloth
    id - index
    name - text
    color ... or some... p3. Table Character_Cloth:
    character_id - index
    cloth_id - index
    order - int (cloth order for each character) so each p1 (charater) has many p2 (cloth) through p3 (Character_Cloth) ordering by (p3 Character_Cloth.order) only quering?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion