Feed a select field with data from multiple mysql tables
Feed a select field with data from multiple mysql tables
carrarachristophe
Posts: 113Questions: 25Answers: 2
in Editor
Hello,
I am using the following:
Field::inst( 'bibliotheque_bibliotheques.oeuvre' )
->options( Options::inst()
->table( 'bibliotheque_oeuvres' )
->value( 'oeuvre_id' )
->label( array('oeuvre_id', 'titre', 'tome', 'auteur') )
->render( function ( $row ) {
if ( $row['tome'] & $row['auteur']) {
return $row['oeuvre_id'].' - '.$row['titre'].' ('.$row['tome'].') - ' .$row['auteur'] ;
}
if ( $row['tome']) {
return $row['oeuvre_id'].' - '.$row['titre'].' ('.$row['tome'].')' ;
}
return $row['oeuvre_id'].' - '.$row['titre'].' - ' .$row['auteur'].'' ;
}))
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'Requis' ))),
To feed a select2 field:
{
label: "Oeuvre:",
name: "bibliotheque_bibliotheques.oeuvre",
type: "select2"
},
The problem is that:
* if oeuvre_id, titre and tome are all part of the mysql table bibliotheque_oeuvres
* I would rather like to use the field bibliotheque_auteurs.auteur (the name) instead of auteur (the id, foreign key)
Anyone would know how I can trick the above code to achieve that?
Answers
One option is to use the the Options class with a function that will get the data from the database using a join.
Another option is to use a VIEW in the database which will do the join and then you just read from the VIEW (rather than using the table name, use the view name in the options class).
Allan
Hi Allan,
Are you suggesting to replace that part
by
correct?
But how can I replace the value and label (closed list in the example) by some fields from various tables?
Correct. Something like:
A few gaps to be filled in, but hopefully that will help!
Allan
Hi Allan,
Thank you for the tip.
Unfortunatly, I cannoy get it work, even when I simplify it at its maximum:
I am getting the following message, related to the above line 13:
Notice: Undefined variable: row in C:\xampp\htdocs\carrara.family\bibliotheque\php\table.bibliotheque_bibliotheques.php on line 39
Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\carrara.family\bibliotheque\php\table.bibliotheque_bibliotheques.php on line 39
My PHP isn't best, but shouldn't lines 13 and 14 be referencing
$rows[i]
, as the error saysrow
is undefined. Also, you're in a loop - the value of$out
is being reset on each iteration,Colin
Thanks Colin.
I think this is out of my abilities.
There is no way to do that in the js side instead?
No - you need to get the information from the database, so you must do this server-side.
It is exactly as Colin says, you need to use:
Or you could have
$row = $rows[$i]
at the top of the loop.Allan
Dear both,
Thank. But unfortunately I could not get it work.
I am getting the following message:
Notice: Undefined index: bibliotheque_oeuvres.oeuvre_id in C:\xampp\htdocs\carrara.family\bibliotheque\php\table.bibliotheque_bibliotheques.php on line 39
Notice: Undefined index: titre in C:\xampp\htdocs\carrara.family\bibliotheque\php\table.bibliotheque_bibliotheques.php on line 40
Please note that I tried with and without the table name.
You seem to have ignored the advice you were given by Colin and Allan.
Try:
Allan
Thank you Allan.
It works with the following code:
But in addition to be missing the if conditions, on which I will work later, I am back to my initial problem, with bibliotheque_oeuvres.auteur quoted instead of bibliotheque_auteurs.auteur.
How can I had a leftjoin to the code? I tried the following:
But am getting the following message:
DataTables warning: table id=bibliotheque_bibliotheques - An SQL error occurred: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bibliotheque_auteurs.auteur' in 'field list'
Use the
join
method or the newerleft_join
(added because it is so common).Where
condition
is replaced with whatever your join condition is. Probably something likebibliotheque_oeuvres.auteur = bibliotheque_auteurs.id
.Allan
Thank you Allan,
Here is the code that works for me:
What I am just missing now is to include in label something close to what I used to have in render, for example only display
$rows[$i]['tome']
in the array only if this field exists.Should I use something like
if (in_array(
?Dear all,
I tried the below without success:
'render' => function ( $rows[$i] ) {
in line 16 but ir returns a http://datatables.net/tn/7 error messageCan anyone help me include some if conditions?
You don't have a
return
statement in your rendering function. So it is effectively a no-op. You probably want returns on line 21 and 23. It should return the string to display, not an array.Allan
Hi Allan,
Adding return on line 21 and 23 does not solve the issue. The select field is still blank.
Can you show me your latest code please?
Also if you could enable debug mode (add
->debug(true)
just before the->process(...)
call, and then send me the JSON response from the server from the Ajax request for the table load, that would be useful.Thanks,
Allan
Hi Allan,
Here is my latest code:
Allan needs the debug info, which will be at the tail-end of your JSON response.
My apologies:
And an excerpt of the json response:
Thanks. I can see for that debug trace that it is executing your query:
Looking at your current code, I can see that you still don't have a
return
statement in yourrender
function:That function just always returns
undefined
.However, I don't think that is actually the issue in retrospect. If you use a custom function to create the options for a select input, you are required to return an array with objects that contain
label
andvalue
properties. You are currently returningvalue
andrender
.label
is commented out for some reason? Also therender
function wouldn't be executed.I would also suggest adding some debug to make sure your SQL query is actually returning any results - i.e. what is the value of
$rows
?Allan
Hi Allan,
I neutralised the label part because when present, this is what is rendered rather than the render part of the code.
I removed the
return
because I am getting an error message. I think it is not needed anyway asreturn $out;
should be sufficient.Would you have any working example of any kind of a
'render' => function ( $rows ) {
?