R
R
Ruslan Kasymov2014-07-22 11:48:46
Yii
Ruslan Kasymov, 2014-07-22 11:48:46

How can a relationship be described in Yii 1.x through a relationship?

For example, we have 3 models Category Object File
Category describes the relationship (object_category is a link table):

public function relations()
  {
    return array(
      'rel_object'=>array(
        self::MANY_MANY,
        'Object',
        'object_category(n_id, o_id)'
        )
      );
  }

Object describes the association to files:
public function relations()
  {
    return array(
      'rel_file'=>array(
        self::HAS_MANY,
        'File',
        '',
        'on'=>"rel_file.owner_id=t.id AND rel_file.type='gallery'"
       )
      );
  }

If you contact the link like this, then everything works:
$result = Object::model()->with('rel_file')->findByPk(1);
print_r($result->rel_file);

BUT if not directly request Object but through rel_object in Categoty:
$result = Category::model()->with('rel_object')->findByPk(1);
foreach ($result->rel_object AS $object) {
  print_r($object->rel_file);
}

There will be an error that AR does not know t.id, apparently because if you go through 2 links, then the object table already has another alias different from t, which is now assigned to the category table ???
In general, how to build cross-connections?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
v0lume, 2014-07-22
@HDAPache

www.yiiframework.com/doc/api/1.1/CActiveRelation#t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question