Leeloo
(Leeloo5e)
1
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.
Leeloo
(Leeloo5e)
3
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.
- 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?
Leeloo
(Leeloo5e)
5
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:
- 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)
- Try to use for explicit primary key:
public function primaryKey(){
return 'cat_id';
}
negar
(Nar8591)
7
hi
use
Yourmodel::model()->with('cat')->findAll();
or
$model->findAll(array('with'=>'cat'));
Leeloo
(Leeloo5e)
8
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