1

I am using cakephp 3 to develop a website. Iam quite new to this. I am trying to retrieve data from the function getSelectedItineraryData() in my ItinerariesController. Here is my controller code:

public function getSelectedItineraryData()
    {
        $selected_itinerary = @$this->request->data('id');
        $locationdata = $this->Itineraries->find('locationdetails', [
            'selected_itinerary' => $selected_itinerary
        ]);

     return json_encode($locationdata);

    }

This uses a custom finder method located in the model file ItinerariesTable.

public function findLocationdetails(Query $query, array $options)
    {
        return $this->find()
            ->distinct(['Intineraries.id'])
            ->matching('Itineraries', function ($q) use ($options) {
                return $q->where(['Itineraries.name IN' => $options['selected_itinerary']]);
            });
    }

This is the ajax request which makes the call to the controller:

function getwypts(callback){
         $.ajax({
        url: "/Itineraries/getSelectedItineraryData/" + selected_i,
        type: "POST",
        data: {
        id: selected_i
        },
        //async: false,
        success: function(result, status) {
            var dayint = 1;
            alert(result);
            itins = JSON.parse(result);
            var day_arr = JSON.parse(itins.day_array);
            var json_day_list = JSON.parse(itins.json_day_data);
            $('#temp_dir_array').val(JSON.stringify(day_arr));
            $('#sel_id_name').text(itins.name);
            start_n = itins.start_name;
            end_n = itins.end_name;
            var tempp = JSON.parse(itins.day_array);
            waypt_obj.start = itins.start;
            waypt_obj.end = itins.end;
            waypt_obj.waypt = itins.arr_intin;
            for (dayint = 0; dayint < json_day_list.length; dayint++) {
                temp11 = tempp[dayint];
                $("#itin_list").append('<div id="itinerary_item_' + (dayint + 1) + '"  value="' + dayint + '"><a  href="#" style= "border: 2px solid red;list-style-position: inside; margin: 5px" class="list-group-item testing"><input type="hidden" id="viewport_wp_item" value="' + tempp[dayint][0] + '" ></input><div class="alert alert-success"><b>Day  ' + (dayint + 1) + ' of Trip (Overnight stay at ' + json_day_list[dayint].overnight + ')</b></div><div id="" class="alert alert-info"><b>Places Visited: ' + json_day_list[dayint].places + '</b><br>' + json_day_list[dayint].desc + '</div></a></div>');

            }
            if(callback) {
            callback();
            }
        }
    });

    }

The issue I have is that this request returns an empty result. It seems that this is because the custom finder methood does not return a correct response. What is wrong in my controller? Or is the error coming from somewhere else? Thanks in advance, been trying for hours on end now.

1
  • 1
    Have you tried just doing the standard debug() during different parts of the process to see how far it gets? Commented Dec 7, 2015 at 16:13

1 Answer 1

2

Just place it to your controller. Hope your problem will be solved!

public function getSelectedItineraryData($ID)
{
    $selected_itinerary = $ID;
    $locationdata = $this->Itineraries->find('locationdetails', [
        'selected_itinerary' => $selected_itinerary
    ]);

 return json_encode($locationdata);

}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this worked, but without the return. Instead of that i used echo json_encode($locationdata); exit(); Why is this?
Thank you, it does solve half the remaing issue, but there's one more thing, the code won't work without the exit(); part. I understand this is to exit the function, but why wouldn't it exit the function at the end anyway? please help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.