Iterating over a model seems to skip records with empty ID - a way to bypass?


#1

Hi there,

In the model I talk about a MYSQL view which joins the actual model to each date of the year is the table. If there is no record which matches a date, a nearly empty row is returned by that view, only the ‘date’ column is not null.

When I directly SELECT that view via phpmyadmin, these nearly empty rows are there, but when I iterate the data model in php using foreach, these nearly empty rows are not iterated, they just miss.

I guess its because the ‘id’ column in these rows is null.

Yet I would like to iterate these rows. An easy trick to do that?


#2

Can you make your model or query return some other value for ID via a function, whenever the ID column is null?


#3

actually, if you don’t have id field then:

  • set id_field = null
  • set read_only = true

after that you should be able to iterate normally and use in tables, but not CRUD, because it relies on unique ID field.

Other option is this:

$this->addExpression('id_combined', 'concat(coalesce([id], 0), "-", coalesce([id2], 0))');

#4

Hi,

my description was not too good actually… here is a picture of what the MySQL view returns:

So I do have an id field (and I would like to keep the name), sometimes its null, sometimes not. I would really like this column to keep the name ‘id’ as name :slight_smile:

I also tried to set some'id_combined' field and set $this->id_field = 'id_combined', but it didnt make a change. What would be the correct place for re-setting $this->id_field?


#5

As i mentioned, if it’s not unique and non-null, it can’t be “id_field”. Simply set property id_field to null and you should still be able to work with data.

If you need to edit rows you need ID field, no way around it.


#6

Hi there,

seems the problem is not atk related: If I run the (very sensible) select query ATK produces directly, it returns the same result: Only rows where id field is not null… Will investigate there


#7

look into strong vs weak joins. By default $model->join() is using strong join.

https://agile-data.readthedocs.io/en/develop/joins.html?highlight=joinLeft#specifying-complex-on-logic


#8

It was my own stupidity, problem solved. I didnt join via ATK, I directly coded it in SQL as view. Now it works as wanted, thats improtant :slight_smile: