N
N
Nicholas2014-07-06 11:18:06
Yii
Nicholas, 2014-07-06 11:18:06

It gives an error getting a HAS_MANY connection from the model (Invalid argument supplied for foreach()), how to fix it?

Actually, code:
Model:

public function relations()
  {
    return array(
      'server' => array(self::MANY_MANY, 'AmxServerinfo', 'amx_admins_servers(admin_id, server_id)'),
      'admin_server' => array(self::HAS_MANY, 'AmxAdminsServers', 'admin_id'),
    );
  }

Controller:
$model=Amxadmins::model()->findByPk($id);
var_dump($model->admin_server);

Mistake:
isxcFE.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai, 2014-07-06
Sumrak @NikolasSumrak

It's some kind of magic.
I struggled with the problem for 2 days, as soon as I asked - I immediately decided.
The problem was that the related table did not have a primary key. I overridden the primaryKey() method in the associated model and everything worked:

public function primaryKey()
  {
    //return 'id';
    // Для составного первичного ключа следует использовать массив:
    return array('admin_id', 'server_id');
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question