Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Best practice for saving and retrieving relationships in controller
  • This framework is awesome- I've been on a bender trying to find the most efficient thing out there going through every framework I can find to convert a TON of functionality out of a wordpress based site as quickly and easily as possible, and I'm totally sold on FuelPHP. The only sticking point I'm having right now is that relationships seem a little bit cumbersome. The documentation has gotten me to a point where I can save and retrieve some relationships, but I'd love to see an example of rendering/processing a form that uses relationships, maybe one for each kind of relationship, so I know if there's a best way to do it that I can go to consistently. Right now I'm finding myself leaning towards encapsulation within model methods for converting id strings to objects, and vice versa, with inefficient queries within loops... not ideal. If anyone would be so kind as to illustrate how they might process each type of relationship within a create and edit route (bonus points if it's right out of a modified scaffold) I would very greatly appreciate it. (PS this would be a great addition to the documentation pages for each relationship type)
  • Not sure what exactly you mean by rendering a relationship, as that would be extremely application specific. But in general, you could so something like this:
    //fetch a parent and it's childeren
    $parent = Model_Parent::find()->where('id', '=', '1')->related('children')->get();
    return \View::forge('relationview', array('parent' => $parent));
    
    and then in your view
    echo "Parent information<br />";
    echo $parent->fullname, '<br />';
    
    echo "Children<br />";
    foreach ($parent->children as $child)
    {
        echo $child->name, '<br />';
    }
    
    So just iterate over the relation (in case it's a "many" relation. if there is only one, you can use the object directly).
  • Thanks Harro I think this starts me in the right direction - some details I'm still fuzzy on if you don't mind: Does the relational field ever need to be declared under model $_properties, outside the $_has_many/$_many_many/etc? Or never? If my intention is to declare all $_properties attributes, and to always render forms using this type of syntax: echo Fieldset::forge('article')->add_model($article)->populate($article, true)->build(); is there a way for me to manually add fields that represent relationships? Is there a shorthand for say, generating a dropdown or multiselect based on each saved instance of a related model for this scenario? On that note but slightly off topic, can you customize the order, or even choose subsets, for which the fields appear when rendering forms in the above manner?
  • No, properties are the columns of the model's table (which you want to expose to the model, you don't have to list them all), relations are defined in their own model properties. Note that because of this, if you have a column and a relation of the same name, you can not access the relation, as the column will have precendence. If you want to manually add more fields to the fieldset, you have to split that line:
    $fieldset = Fieldset::forge('article')->add_model($article)->populate($article, true);
    $fieldset->add('fieldname', 'Field Name', array('class' => 'pretty_input'));
    echo $fieldset->build();
    
    You should do this anyway, so you can base $fieldset to the View, and do your echo'ing in the view. ORM objects aren't really mend for running selects, so using relations for that isn't a good idea. It's a lot simpler and faster to just code a normal query. Here's an example of a query that generates an id=>name array strait from a table: http://fuelphp.com/forums/posts/view_reply/5426

Howdy, Stranger!

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

In this Discussion