I need to convert numbers to other numbers, the conversion depends on the connection in use. For each connection there are several types of conversions. So I created two tables (MyISAM):
------------- -----------------------
| connection | BELONGS_TO | connection_mapping |
|-------------| <------------ |-----------------------|
| int id | | int id |
| str name | HAS_MANY | int connection_id |
. . . ------------> | int from |
. . . | int to |
| | | char 9 type |
------------- -----------------------
The BELONGS_TO relation is defined by this code:
class ConnectionMapping extends CActiveRecord
{
public function relations()
{
return array(
'connection' => array(self::BELONGS_TO, 'Connection', 'connection_id'),
);
}
A HAS_MANY relation is defined like this:
class Connection extends CActiveRecord
{
public function relations()
{
return array(
'storeToLangMap' => array(self::HAS_MANY, 'ConnectionMapping', 'connection_id', 'condition'=>"storeToLangMap.type='STORELANG'"),
);
}
I can use this in my class:
$this->connection = Connection::model()->findByAttributes(...);
$storeToLang = $this->connection->storeToLangMap;
$storeToLang exists of multiple rows. How can I use this to convert my integer?
I tried to use this:
$storeToLang->findByAttributes(array('from'=>$i));
But this gives an error:
I also tried this:
class ConnectionMapping extends CActiveRecord
{
public function convert($id)
{
return self::model()->findByAttributes(array('from'=>$id))->to;
}
$storeToLang->convert($id);
But this gives the same error as above.
As a last resort I tried this:
$storeToLang = $this->connection->storeToLangMap(array('condition'=>'from=1'));
Which results in this error:
How can I solve this?