Question

I have a problem with certain generated query, the query does an inner join with a table that has some same field.

How can I have the query with the table name in each field, basically what i want is that:

Convert this:

 select "list_id", "date_time","plate"...

TO:

 select register."list_id", register."date_time",register."plate"...

I think with alias to the field name also could be accomplished but i dont know how to add the alias in atk4

If someone wants to see the full query and atk error:

Application Error: Database Query Failed

Exception_DB, code: 0

Additional information:

pdo_error: SQLSTATE[42702]: Ambiguous column: 7 ERROR: column reference "date_time" is ambiguous LINE 1: select "date_time","plate",(select "name" from "lane" whe... ^
mode: select
params:
query: select "date_time","plate",(select "name" from "lane" where "register"."lane_id" = "lane"."id" ) "lane",(select "name" from "camera" where "register"."camera_id" = "camera"."id" ) "camera",(select "detail"."id" from "detail" where "register"."detail_id" = "detail"."id" ) "detail","id","lane_id","camera_id","detail_id" from "register" inner join "detail" on "detail"."id" = "register"."detail_id" order by (select "detail"."id" from "detail" where "register"."detail_id" = "detail"."id" )

This is how im making the model. This model has 3 related fields in other tables, with those, all is OK. But i want to have one more field (field name from table List), and List is not directly related to Register, is only related throught Detail. So i have to get it throught Register->Detail->List..

table Register(id, plate, detail_id,..)---->hasOne(detail_id)-->table Detail(id, list_id, date..)---->hasOne(list_id)---->table List(id,name,..)

model class:

    class Model_Register extends Model_Table {
          public $table='register';
        function init(){
            parent::init();

            $this->addField('date_time')->sortable(true)->defaultValue(date('Y-m-d H:m:i'))->type('date')->mandatory(true);
            $this->addField('plate')->sortable(true)->mandatory(true);

            $this->hasOne('Lane', 'lane_id')->sortable(true)->system(true);
            $this->hasOne('Camera', 'camera_id')->sortable(true);
            $this->hasOne('Detail', 'detail_id')->sortable(true);
        }

    }

And after in the page class i do the join, yes I know is detail at this moment is redudant im only trying...

    $register = $crud->setModel('Register');
    $q = $register->_dsql();
    $q->join('detail', 'detail_id', 'inner');
    $q->join('list', 'list_id', 'inner');

How can I have this field from the List field?? Any solution will be welcomed..

Thanks in advice!! Im breaking my head with this! ;)

Was it helpful?

Solution

Thanks for your time guys, finally I found the solution with the called: Chained joins

Example from documentation:

$perm = $this->join('permission');
$res = $perm->join('resource');

Belive or not that was my real problem! Thanks all anyway

OTHER TIPS

try to add 'table_alias' property for your Models

class Model_Yours extends Model_Table {
    public $table_alias = '_alias';
}

Default value for this property is null - link

So if you add any value it can be used here

Not sure if this will help :(


$t1 = $this->add('Main_Table');
$t2 = $t1->leftJoin('joined_table_name');
$t2->addField('joined_table_field_ALIAS','joined_table_field_REALNAME');

check SQL_Model::addField() method here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top