Error with mass import with beans

Hello, I have a problem with an importation via beans. Having the following array received in the importer, I’m trying to retrieve that field, but it’s not being entered into the record.

array(108) { [“numero_documento_otro”]=> string(11) “111111111-1” [13]=> string(11) “111111111-1”}

$miembroExistente->num_documento_c = is_null($registro["numero_documento"]) ? $registro['numero_documento_otro'] : $registro["numero_documento"];

The issue lies in the fact that “numero_documento” is null. However, “numero_documento_otro” is not null, so it should take that value, but it’s not happening. It only takes the value if the “numero_documento” field is not null. What could be causing this? I’ve considered several alternatives such as data timing or field length, but none of them have provided a solution.

Does $miembroExistente->num_documento_c exist?

If you put a constant value there, like “12345”, does it work?

Yes, that’s strange. In fact, in the following condition, if “numero_documento” is not null, it enters it correctly. The problem lies with “numero_documento_otro”; it always remains null.

if ($registro["id_tipo_documento"] <= 5 && $registro["id_tipo_documento"] !== 4) {
    $miembroExistente->num_documento_c = $registro["numero_documento"];
    foreach ($tipoDoc as $keyDoc => $valueDoc) {
        $keyTipoDoc = explode(",", $keyDoc);
        if (in_array(trim($registro["tipo_documento"]), $keyTipoDoc)) {
            $miembroExistente->tipo_documento = $valueDoc;
        }
    }
} else {
    $miembroExistente->tipo_documento = 'extranjero';
    $miembroExistente->num_documento_c = is_null($registro["numero_documento"]) ? 1111 : $registro["numero_documento"];
}

If I use a constant, it also doesn’t take it.

Then you should focus your attention on the code that fills in the value

$registro['numero_documento_otro']

that’s probably where the bug is (earlier in the process)

1 Like

Thank you, pgr. The code was not taking the condition in a ternary operator “ ? value : value”. I changed it to an if-else statement, and it was okay. I don’t know what could have happened.

What is the condition you used in the if?

Ternary operators work just fine (I love using them) and we should always get to the bottom of any language issue we don’t understand - makes us better programmers.

So i am curious…

Well, in other field:

$miembroExistente->tipo_act= is_null($registro["act"]) ? $registro["act_otra"]: $registro["act"];

And doesn’t work. I tried to change that:

if(!is_null($registro["act"])){

$miembroExistente->tipo_act= $registro["act"]

} else {

$miembroExistente->tipo_act= $registro["act_otra"]
}

but really i don’t understand why, so, maybe in the changes i made another thing and resolve that fix, because de Ternary operators did the same work

It is not the same to test for null or !null (even if you then invert the true and the false clauses).

This is because of other edge values like "" or undefined. Maybe you just need to use a different test, like empty or isset…

But I am sure that it can’t be just the ternary operator. It is 100% equivalent to the if, with the same condition and order of clauses.

1 Like

Oh, I see, maybe that’s why. I considered it, but when I tested it with the negation, it worked correctly, so I left it like that.

Thank you for the response!

1 Like