php new field not in the database
php new field not in the database
Hello,
I need to, while keeping the original field from the db, add a new field (country) which is not in the db but depends on a field in the db "isobic.BIC".
Here is the inital code:
Editor::inst( $db, 'isobic', 'id' )
->fields(
Field::inst( 'isobic.BIC' )
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'Requis' )))
->validator( Validate::maxLen( 11 ) )
->validator( Validate::unique( ValidateOptions::inst()
->message( 'Existe déjà' )))
)
I tried the following:
Editor::inst( $db, 'isobic', 'id' )
->fields(
Field::inst( 'isobic.BIC' )
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'Requis' )))
->validator( Validate::maxLen( 11 ) )
->validator( Validate::unique( ValidateOptions::inst()
->message( 'Existe déjà' ))),
new Field( 'country' )
->getFormatter(function ($val, $data, $opts) {
return substr($data['isobic.BIC'], 4, 2);
})
->set(false),
)
and
Editor::inst( $db, 'isobic', 'id' )
->fields(
Field::inst( 'isobic.BIC' )
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'Requis' )))
->validator( Validate::maxLen( 11 ) )
->validator( Validate::unique( ValidateOptions::inst()
->message( 'Existe déjà' ))),
new Field( 'country' )
->setValue( substr($data['isobic.BIC'], 4, 2))
->set(false),
)
But I am getting an error message: "An SQL error occurred: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'country' in 'field list'".
Is there a way to achieve what I am trying to do?
Thanks and regards.
This question has accepted answers - jump to:
Answers
Add
->get(false)and it will stop the script from attempting to read it from the DB. Alternatively, if you need there to be a value for that field in the JSON sent to the client-side, use->getValue('myValue')on theFieldinstance (replacing the value with whatever it is you need of course).Allan
Thank you Allan,
I tried the following:
but am getting ic (from isobic.BIC), so I tried:
but am getting the https://datatables.net/tn/7 error.
Any idea?
A
Fieldinstance can only be a parameter for theEditor->field()orEditor->fields()methods.substrdoesn't know what to do with a file instance!Since you already have
isobic.BICbeing read and returned to the client-side, I'd suggest you just use that in a second column at the client-side - e.g. incolumnsorcolumnDefs:Alternatively, if you did want to do it on the server-side:
Allan
Thank you Allan,
I prefer it on the server-side (and your code works perfectly), because I intended to add a join clause in order to, based on the 'country' (ex: GB), retrieve its name (Ex: United Kingdom).
Is it technically possible with a field that is not part of the DB?
I am getting : DataTables warning: table id=tms_sw_isobic - An SQL error occurred: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'country' in 'on clause'
when I use:
No, because of how the SQL builder works for the PHP libraries, it doesn't actually do the aliasing to
countryin the SQL, but rather in PHP. So I'm afraid that will not work as you are expecting.To do something like that, you'd need to use a VIEW for reading the data.
Allan
Thank you allan.