Is there a method for incorporating a conditional clause within a Laminas Db Select join operation?

168 views Asked by At

I have been working on two tables (A and B), what I want to do is fetch data from these tables using a left join in laminas db. The following code is of importance $select->join(array('b' => 'custom_table1'), "a.id = b.column1_id AND b.coulmn2_id = 1", array('b' => 'custom_column_name'), Select::JOIN_LEFT);

The problem is that it gives Statement could not be executed (42S22 - 1054 - Unknown column '1' in 'on clause') at the AND b.coulmn2_id = 1.

The join must take it as a simple condition, not a table column. This query generated by the above code works in MySQL but in laminas it is emitting errors.

There is one thing the b.coulmn_id is shown in quotes "1". It may be a issue. If it is how to fix it?

1

There are 1 answers

0
ali-hassan On BEST ANSWER

The issue is correctly identified, the Id is taken as column name instead of a value. The solution I found is using Laminas\Db\Sql\Expression. Like:

$select->join(
            array('b' => 'custom_table'),
            new Expression('b.a_id = a.id AND b.s_id ='.(int) $s->getId()), 
            array(),
            Select::JOIN_LEFT
        );