Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Iterate and query, or joins?
  • Hullo. I'm just beginning with FuelPHP and I like it. It feels really friendly coming from CodeIgniter, and I am starting out with a tutorial. It presents me with the following code: (I've converted it to shortened pseudo-code)

    $messages = Model_Message::find('all');
    foreach ($messages as $message) {
        $query = DB::get_comment();
    }

    Should I use that in my actual production code, or is it there for the sake of simplicity? I feel like it would be better to do some sort of join, rather than a ton of queries. What should I do?


  • HarroHarro
    Accepted Answer
    As always, it depends. Sometimes a join is faster, sometimes it isn't. Fetching all comments for every message probably warants a join.

    If Model_Message is an ORM model, just define the relation, and you can fetch them in one go using

    Model_Message::find('all', array('related' => array('comments')));

    If it's Model_Crud based (or hand-coded), you have to deal with the join and the hydration of the result yourself.
  • That's really cool! Thanks
  • If you prefer method chaining, instead of this complex array notation, ORM allows you to do that too:

    Model_Message::query()->related('comments')->get();
  • Thank you. That does look nicer to me. I didn't want to make a separate thread for another question I have.

    I want to make a RESTful application where I can make 1 controller per resource, but also have different front ends like web, android, IOS, and maybe native apps for desktops.

    I believe I would output JSON (I prefer this over XML) responses, but I am unsure of how to make that work with views (keeping views out of the controller)
  • Restful controllers should extend Controller_Rest. Ideally you don't mix them with frontend functionality (altough you could using Controller_Hybrid), also think about proper versioning of your API, and about consistency in your responses.

    Controller_Rest uses automatic detection, it supports multiple response types, based on what the client requests.

    See http://docs.fuelphp.com/general/controllers/rest.html#/format_determination on how the detection process works. In general, send an ACCEPT header to tell the API with response format you want.
  • Okay, I see that, but I'm not understanding how I would go about making it render a view if it comes from the web.

    class Controller_Test extends Controller_Rest
    {
        public function get_index()
        {
            return $this->response(array(
                'hello'=>'world'
            ));
        }
    }

    localhost/test.json gives me a JSON output like I would want. I'm trying to make it so that if there is no extension, then it will send the data to a view and display that
  • I don't understand that. ;-)

    Either you have an API, which returns a block of data in some form (json or other), or you have an interactive application that returns html to be rendered in a browser.

    Ideally you should not mix the two, and you should absolutely not have the two functions under the same URI, that is going to lead to complex code and a maintenance nightmare.

    If you insist you can use Controller_Hybrid, and do something like

    if (\Input::is_ajax())
    {
            return $this->response(array(
                'hello'=>'world'
            ));
    }
    else
    {
            return \View::forge('myview', array(
                'hello'=>'world'
            ));
    }
  • "Either you have an API, which returns a block of data in some form"

    Alright, alright. Does this mean my web app has to be ajax based? That doesn't sound entirely bad, but it seems like it would cause there to be more HTTP requests than necessary, i.e.

    Visits test.php and fetches assets
    Then an AJAX request is made to the controller to fetch the data?

    that doesn't sound right, so maybe i'm crazy. Is there a guide anywhere for doing what you're explaining to me and I'm missing?
  • wait wait wait, I was brushing my teeth and it clicked.

    i think i've got it. thank you
  • No, that's entirely up to you.

    What I meant was that you should not design an application where "/controller/method/parameter" returns a data structure in one case, an html webpage in another. Keep your API and your web frontend separate.

    I usually use the prefix "api", which will be a folder in the Controller directory. You could version that using further folders, like "v1", etc. You then get URI's like "/api/v1/users/get/10", which will then call Controller_Api_V1_Users.

    It allows you to develop your api separate from your frontend application, and keep it simple to maintain and expand in the future.
  • If you setup your API properly, you can use HMVC requests you have your web frontend controller call your API controller to fetch the required data. You should return an array or object instead of json then, it saves you a bit of parsing.

    That will keep your code DRY.

Howdy, Stranger!

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

In this Discussion