Populating with all contacts & cutomers.
Populating with all contacts & cutomers.
I have a customers & contacts table. Both tables have unique field custID. contacts table have multiple contacts from one company
I am trying to get the following result
customers.custID | contacts.firstName | contacts.firstName
500 | John | Paul
ie I need them in separate fields.
Is this possible in a MYSQL Query?
If so what is the syntax to use with the editor.
The idea is I can use responsive extension and show the company details and then have all the contacts show on expanding the row.
Or is there another approach?
Cheers
Steve Warby
This discussion has been closed.
Answers
You can do this with a one-to-many join. Because the data for the names will come back as an array, if you want names in different columns you could do something like:
That assumes you have a restricted number of the join entries, since you need a column for each. The other option is to display them in a column like the example I linked to above and then use a renderer to display them.
Allan
I may have no explained correctly.
So in the datatable I only have one row for each customer.
The PHP is
{
"data": [
{
"DT_RowId": "row_501",
"customers": {
"custID": "501",
"companyName": "Crystal Architectural Aluminium Ltd",
"address1": "Unit 1 Mackeson Road",
"address2": "Ashton Under Lyne",
"address3": "Lancashire",
"Post_Code": "OL6 8HZ",
"Mobile": "xxx",
"Telephone": "xxxx"
},
"contacts": {
"custID": "501",
"FirstName": "Andy"
}
},
{
"DT_RowId": "row_501",
"customers": {
"custID": "501",
"companyName": "Crystal Architectural Aluminium Ltd",
"address1": "Unit 1 Mackeson Road",
"address2": "Ashton Under Lyne",
"address3": "Lancashire",
"Post_Code": "OL6 8HZ",
"Mobile": "xxxx",
"Telephone": "xxxxx"
},
"contacts": {
"custID": "501",
"FirstName": "Greg"
}
}
],
"options": [],
"files": []
}
```
I am seeing
What I need to see is
one row for custID501
with
CustNo - Company - firstName(1) - firstName(2)
The reason for this is I can enter a company name or first name in the search field and quickly find a customer.
Hope I'm making sense
Cheers
Steve Warby
Is
customers.custID
unique and the primary key in thecustomer's
table? I'm not sure why it would be showing two rows if so - could you add->debug( true )
before the->process(...)
call and show me the data returned from the server if that is the case?If it isn't the primary key, and it isn't unique, it can't be used as the primary key (third parameter in the Editor constructor).
Allan
Hi Allan,
customers.custID is unique
It looks like you have two leftJoin statements, which isn't shown above:
There is no select from the
companyTypes
table, which is what is causing the issue. if you don't want anything from that table, remove that leftJoin.Allan
Sorry I was playing around.
I get the same result without the second join.
Formatted it looks like this:
That looks like it should work to me. If you run it directly against your database, do you only get a single row for each customer ID?
Allan
I get the same ie if a company has 5 contacts I get 5 rows.
Ah - I see what you mean, sorry I hadn't quite got it before. Yes, that is how a left join would operate in these circumstances. If the joined table has multiple records, it would fill in the details for the remainder of the row for each entry.
What you want here is an Mjoin rather than a left join (since the relationship is one-to-many). Ideally there would also be a link table in between the two tables so that you can change the links if needed.
Allan
Hi allan got it working ( nearly).
What would work is the following:
but concat the fields and make the mobile number into an sms
Can't work out the syntax but this
render: '[</br> ].FirstName' + '<a href="sms:' + [].Mobile + '">' + [</br> ].Mobile + '</a>'
so the output would be
John 07861xxxxx
Paul 07861yyyyy
Andrew 07861zzzzzz
Where the mobile numbers are SMS links.
Cheers
Steve Warby
You'd need to use a function with a loop since you want to build a more complex string:
Allan
Got it working as follows
Once again thanks for all the help Allan.
Cheers
Steve Warby