Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Example of search form processing
  • Hello!
    Can you give me an example of serach form processing. Can I do it with ORM?
    For example I need to process the next form:
    <form>
    <select name="from_year">
    <select name="to_year">
    <input name="from_price">
    <input name="to_price">
    <button>
    </form>

    And one more question. How can I set "where", if, for examlpe, user did'nt set "from_price".

    Thank you.
  • HarroHarro
    Accepted Answer
    // create a query object
    $query = Model_Something::query();

    // add optional where clauses
    $input = Input::post('from_year', false) and $query->where('from_year', '=', $input);
    $input = Input::post('to_year', false) and $query->where('to_year', '=', $input);

    // get the result with a limit/offset for pagination
    $result = $query->limit($limit)->offset($offset)->get();
  • Harro Verton - you are great!
  • Can we process multiple select? I tried to make it on my form, but Input::post('myselect') gives only last element.
    <span class="tag"><span class="attribute"></span></span>
  • Just pass 'multiple' to Form::select() in the attribute list.
  • I tried to do this, but when I choose, for example, three elements,  Input::post('mymultipleselect') gives  only the last chosen element.
    May be I do something wrong?
  • If you define the select as "multiple", your input form field must be an array, otherwise you're only getting the last element.
  • I didn't understand it. Can you give an example?

    <?php echo Form::select('body_type', 'none', Model_Advert::bodyoption(), array('class' => 'span2', 'multiple'));?>
    This expression gives only last element.
    What do you mean "input form field must be an array"?

  • Your input field must be an array:

    <?php echo Form::select('body_type[]', 'none', Model_Advert::bodyoption(), array('class' => 'span2', 'multiple'));?>
  • It's very simple! Thank you! 
    And in the statements, written above ($input = Input::post('from_year', false) and $query->where('from_year', '=', $input);) nothing changes?
  • That depends. I assumed these were normal input fields. If they are multi-selects too, that needs to be changed to:

    if ($input = Input::post('from_year', array()))
    {
        $query->and_where_open();
        foreach ($input as $value)
        {
            $query->or_where('from_year', '=', $value);
        }
        $query->and_where_close();
    }


  • On the basis of this form processing I try to make something like API.

    So, I useController_Rest

    class Controller_Getlist extends Controller_Rest
    {
        public function post_search()
        {
            $query = Model_Advert::query();
            $input = Input::post('brand_id', false) and $query->where('brand_id', '=', $input);

          ... (another filtration)

            return $query->get();
        }
    }

    Now I want to add "array" of original_path's to images belongs to every advert_id (reverce relations belongs to and has many are already described in models -as you already understend, each advert has many images).

    This is properties array in model Images:
     protected static $_properties = array
        (
            'image_id',
            'advert_id',
            'original_path',
        );

    So, I have no idea how to add in  "return"  array of original_paths to every advert. I think that it is simple if you know how to do it :) But I don't know.
  • The return value of a REST method must be either an array, or null (nothing) in case you've used $this->response() to set the response value(s).

    You can not return ORM objects, the REST base controller doesn't know how to convert that to an array (which is required to create a json object that is usable).

    So you need to convert the query result to an array that you can return.
  • Fast answer! Cool!

    OK, I've  changed "return" to "$this->response()" but I can not understand how to add "images" to response.

    So, for example, if now answer is something like "array ('4' => array ('title' => 'MyTitle', 'brand_id'=>'7'))"

    How can I make the result like "array ('4' => array ('title' => 'MyTitle', 'brand_id'=>'7', 'images' => array ('25' =>'qwerty.jpg','26'=>'wertyu.jpg')))"

    where 25 and 26 - image_id
    and
     qwerty.jpg and wertyu.jpg - original_path
    for the advert_id = 4.
  • You'll probably have to loop over the results, and for every result over the related images, to fill your array,
  • You can use to_array() on a model object as an alternative.
  • Someone in IRC mentioned you could have a look at Arr::pluck(), it allows you to extract a subset of fields from a nested structure.
  • Why the heck didn't you give me this much help on my *very* similar question?  Apparently, I didn't see this beforehand.  This was very helpful.  
  • Because your question was not *very* similar. It was extremely vague, nothing specific, and no reply to any of my questions be become more specific.

    This question is a very specific question, which can be anwered.

Howdy, Stranger!

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

In this Discussion