简体   繁体   中英

Laravel DB select statement. Query returns column name as result rather than values

UPDATE: The below is for context but I figured out this is not where the problem is.

I'm pretty new to php and laravel. I do an AJAX call to a controller that uses a postgresql query from a repository. The query is a select query it has columns dated, total_sms and demo (demo is gender Male/Female).

I have an array $return[] The below is within a foreach ($result as $day). At the end the array is translated to json before returning.

$return[$day->dated][$day->demo] = $day->total_sms;

I expected the above to insert the dates as a key with an array value, the gender (Male and Female) as the keys of the inner array, with them having an array as a value and these arrays will contain the total_sms value. (that sentence must have been confusing...)

However the result is:

{2017-07-03: {d.gender: "0"}, 2017-07-04: {d.gender: "0"}}

How would I build an array like:

{2017-07-03: {Male: "0" , Female: "10"}, 2017-07-04: {Male: "0", Female: "0"}

Although I think the above is not actually how I originally described it? I am planning on decoding it and sticking the information into a chart.js chart with:

$.each(d, function(date, value) {
   window.barChart.data.labels.push(date);
      $.each(d.date, function(demographic, value) {
            // ALWAYS ASCENDING DATA

});

UPDATE: Code that is creating the issue.

I have a query in a repository that can have varying parameters inserted, to change what demographic is used. Below is the version I use in pgAdmin, and it works fine.

SELECT d.date dated, count(se.id) total_sms, demo
FROM    (
    select to_char(date_trunc('day', (current_date - offs)), 'YYYY-MM-DD') AS date, demographic.gender as demo
    FROM generate_series(0, 365, 1) AS offs
    CROSS JOIN (SELECT DISTINCT gender FROM common.profile) AS demographic
    ) d
LEFT OUTER JOIN (
    SELECT id, customer_id, client_report.insert_time, profile.gender, profile.house_income, profile.address AS postcode, profile.age AS age_group, profile.is_employed, profile.is_married, profile.no_children, profile.no_cars, profile.shopping_frequency
    FROM common.client_report
    JOIN common.profile
    ON client_report.profile_id = profile.uuid 
    WHERE sms_status = 'SUCCESS' AND customer_id = 5::int
    ) se
ON (d.date=to_char(date_trunc('day', se.insert_time), 'YYYY-MM-DD')) AND demo = gender
WHERE d.date::date BETWEEN '2017-07-01'::date AND '2017-08-01'::date
GROUP BY d.date, demo
ORDER BY d.date, demo ASC;

Anywhere that states gender, a date or the 5 after AND customer_id = are all generated from named parameters (When I receive an error I can generally fix that, when there is no error, the code works but gives me gender as the column name ['de.gender'] rather than the values ['Male', 'Female']). I mean to say that I have had errors around the parameters, and no longer believe that could be the issue. I then will receive an error around missing a conversion tool for unknown to text. I don't fully understand this but I know it means I have to cast gender as text with ::text. Doing that results in the column name appearing. However doing that in a different, simpler query without the cross join works and the results are correct. This leads me to believe there is an issue with the way my query is or that there is a bug with the DB driver and these queries.

UPDATE: Did a test, should have done it earlier! Placed the version with no parameters into my application, made the function run, and it returns exactly as needed. So this is stemming from parameters, and probably the ones that define tables like de.gender (de being the table)

UPDATE: Another test:

CROSS JOIN (SELECT DISTINCT gender FROM common.profile) AS demographic

On this line, gender is a named parameter (:demograph). However, if I change this to simply gender, the results are as expected with Male, Female values. If it stays as a parameter it seems to be creating lots of strings of 'gender' rather than grabbing the 'Male', 'Female' values.

UPDATE: Did a lot of learning today, and named or positional parameters are for values only. Meaning all inputs are encased in double quotes, which was why I was creating strings of 'gender' rather than male, female.

i donn't know i am not sure about your question

$testArray = array(
            "2017-07-03" => array("male" : "0","female" => "10"),
            "2017-07-04" => array("male" : "0","female" => "0")
    );
    return $testArray;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM