Accessing model using HAS_MANY relation

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?

First of all, HAS_MANY will return an array, so you will have to iterate over the result set.

Also, following the initial query, you use lazy loading for bringing in the related records, then obviously attempt to use the (already complete) result set in another filtered query.

I can’t tell what’s the best solution in your case (unless I try it out myself), but I think you would try either to include all filtering in the initial query (possibly by using eager loading), or just iterate over the already present result set and use PHP expressions for filtering.

/Tommy

Thanks!

I solved it like this:




class Connection extends CActiveRecord

{

    const storeToLanguage = 'STORELANG';

    const groupToGroup = 'GROUPGROP';


    public function map($type, $id)

    {

        return ConnectionMapping::model()->findByAttributes(array(

                                                        'connection_id'=>$this->id,

                                                        'type'=>$type,

                                                        'from'=>$id))->to;

    }