populate select element with data dependent on selected row
populate select element with data dependent on selected row
Hello all,
I'm trying to populate a select element in editor with contacts but I only want to see the contacts belonging to the company(dealer) I select and edit instead of all of them. So I have to select only the contacts with a particular dealernr, how can I get this value in the query? this is the code I have
Field::inst( 'bezoekverslagen.dealercontacts_id' )
->options( Options::inst()
->table( 'dealercontacts, bezoekverslagen' )
->value( 'dealercontacts.dealercontacts_id' )
->label( array('dealercontacts.initials', 'dealercontacts.firstname', 'dealercontacts.lastname') )
->where( function ($q) {
$q->where( 'xstop', 0 );
$q ->where('dealercontacts.dealernr', 'bezoekverslagen.dealernr');
})
)
->validator( 'Validate::dbValues' )
It's not working this way. The dealernr is in the row I select and want to edit, when editing I don't want to see all contacts, only thos that belong with that particular dealernr. How can I get this value? I hope someone can help, Thanks!!
This question has accepted answers - jump to:
Answers
I also tried the following:
But then the column is not recognised
I think I just don't know what to use
How can I see what actual mysql query actually is produced?
Enable debug mode in your Editor instance.
You can then view the result in your browser's console.
Change that to be:
The problem was that it was searching for the string
bezoekverslagen.dealernr
, not the column value. Thefalse
in the forth parameter will stop that from happening and make it use the column search.If that works, that's a clever solution! I hadn't thought of doing a join with the
Options
class that way.Allan
Thanks for the answers! unfortunately still the error
Column not found: 1054 Unknown column 'bezoekverslagen.dealernr'
Could you enable debugging as tangerine suggested and then show me the JSON return from the server, which should include the executed SQL.
Allan
Sorry for the late reply but the JSON return is this:
Is there another way how I can get the value "bezoekverslagen.dealernr"? The dealernr is known because its in the record I am editing, can I put it in a variable? and if so how? thanks!!
I don't know how to put bezoekverslagen.dealernr into $dealernr (I'm just a beginner)
This is the whole file
I solved it using the dependent() option.
For anyone who likes to know
In javascript
and in contacts_dynamic.php
<?php > ``` ?>```
<?php
include_once( "dbconfig.php" );
$stmt = $db_con->prepare("SELECT dealers_dealercontacts.dealercontacts_id as value, dealercontacts.lastname as label FROM dealers_dealercontacts
LEFT JOIN dealercontacts ON dealercontacts.dealercontacts_id = dealers_dealercontacts.dealercontacts_id WHERE dealers_dealercontacts.dealernr=:dlr");
$stmt->execute(array(":dlr"=>$_REQUEST['values']['bezoekverslagen.dealernr']));
$contacts=$stmt->fetchAll();
echo json_encode( [
'options' => [
'bezoekverslagen.dealercontacts_id' => $contacts
]
] );
I know I can also use
// DataTables PHP library and database connection
include( "DataTables.php" );
Instead of my own, but I simply couldn't figure out how to do the query as I need it, I need working examples for left joins etc
Thanks for posting back - sorry I didn't manage to get back to you before you resolved it yourself!
Allan