A
A
Artem2014-09-25 18:36:37
Yii
Artem, 2014-09-25 18:36:37

Relational Active Record in Yii. How to use?

Understood what is wrong
Good afternoon. Can't understand Active Record in Yii.
There are Test & Question models trying to connect.
test:

public function relations()
    {
        return array(
            'test'=>array(self::HAS_MANY, 'Question', 'type'),
        );
    }

Question:
public function relations()
    {
        return array(
            't_test'=>array(self::HAS_MANY, 'Test', 'name'),
        );
    }

Table Test:

- id = (1)
- name = (2)
- description = (3)

Question table:

- id = (1)
- id_test = (2)
- question = (3)

Here is the code in the controller:
public function actionIndex()
    {
        // получаем запись с ID=10
        $post=Test::model()->findByPk(1);
        var_dump($post);
        // Получаем автора записи. Здесь будет выполнен реляционный запрос.
        $author=$post->test;
        var_dump($author);
    }

Outputs the following:
object(Test)[34]
  private '_new' (CActiveRecord) => boolean false
  private '_attributes' (CActiveRecord) => 
    array (size=7)
      'id' => string '1' (length=1)
      'name' => string 'asdzsd' (length=6)
      'description' => string 'fszdfds' (length=7)
      'author' => string '0' (length=1)
      'status' => string '0' (length=1)
      'create_at' => string '2014-09-25 17:56:59' (length=19)
      'edit_at' => string '0000-00-00 00:00:00' (length=19)
  private '_related' (CActiveRecord) => 
    array (size=0)
      empty
  private '_c' (CActiveRecord) => null
  private '_pk' (CActiveRecord) => string '1' (length=1)
  private '_alias' (CActiveRecord) => string 't' (length=1)
  private '_errors' (CModel) => 
    array (size=0)
      empty
  private '_validators' (CModel) => null
  private '_scenario' (CModel) => string 'update' (length=6)
  private '_e' (CComponent) => null
  private '_m' (CComponent) => null

array (size=0)
  empty

Outputs an empty array. Totally confused(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2014-09-25
@ArtMikS

I already understood what the error is, but still the essence is a little incomprehensible. Perhaps you can recommend a few articles?
Ps it was necessary to write this in Test ( model ):

public function relations()
    {
        return array(
            'test'=>array(self::HAS_MANY, 'Question', 'id_test'),
        );
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question