ActiveRecord corrupt relation query

Hi @all,

try to use ActiveRecord-Models to handle my relations between different DB-Tables. If I try something like that




$model->with('cat')->findAll();



I got the error:




Column not found: 1054 Unknown column 'cat.' in 'on clause'



The On-Clause looks like




 ... `cat` ON (`t`.`cat_id`=`cat`.``) ...



I see, there is some missing, but WHY ???

My Model-Class has the relation




'cat' => array(self::BELONGS_TO, 'cat', 'cat_id')



The Cat-Model-Class has the relation to the Model-Class itself




'mod' => array(self::HAS_MANY, 'Model', 'cat_id')



Because the Cat-Table and the Model-Table have the column ‘cat_id’.

Maybe somebody can help?

Thank you very much,

Best Regards,

Leeloo

Has cat table a primary key?

From error




Column not found: 1054 Unknown column 'cat.' in 'on clause'



seems that you are missing primary key for cat table.

Hi Fabrizio,

thanks for your fast reply.

Yes, your right. There is no Primary Key. But I do not want one. How can I use the Model-Relations anyway?

Best Regards,

Leeloo

You have 2 options:

1)Override getPrimaryKey() method in your cat’s model

So relation should work.

  1. Put keys’ relation manually, as:



'cat' => array(self::BELONGS_TO, 'cat', '', 'on' => 't.cat_id = cat.id_of_cat_table')



But what is the reason that you haven’t a primary key?

When I add




'on' => 't.cat_id = cat.id_of_cat_table'



to Relation-Definition, it is only added to the "on" clause in query but not replacing the corrupt on.

If I add




public function getPrimaryKey() {

    return 'cat_id';

}



the corrupt on clause is still the same. Nothing changed ;(

Any other ideas?

Two things:

  1. Have you setted empty foreign key in relation:



'cat' => array(self::BELONGS_TO, 'cat', '', 'on' => 't.cat_id = cat.id_of_cat_table')



(note empty third paramter in function)

  1. Try to use for explicit primary key:



    public function primaryKey(){

       return 'cat_id';

    }



hi

use




Yourmodel::model()->with('cat')->findAll();



or




$model->findAll(array('with'=>'cat'));



The second version from Fabrizio works very well. There was no primary key for my ‘cat’ table. so i have to define it.

Thanks.

Best Regards,

Leeloo