Yii2: Use hasOne relation Model on form return error

Created on 7 Jun 2016  路  5Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

I have model like this

namespace frontend\models;
class User extends \yii\db\ActiveRecord{
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAgent()
    {
        return $this->hasOne(\frontend\models\Agent::className(), ['user_id' => 'id']);
    }
}

and when I use this

/* @var $model frontend\models\User */
    <?= $form->field($model->getAgent()->one(), 'name')->textInput(['placeholder' => 'Name']) ?>

What is the expected result?

ActiveForm could use the hasOne Model to model requirement

What do you get instead?

generates error

Error: Call to a member function formName() on null in D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\helpers\BaseHtml.php:2113
Stack trace:
#0 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\helpers\BaseHtml.php(1242): yii\helpers\BaseHtml::getInputName(NULL, 'id')
#1 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\helpers\BaseHtml.php(1292): yii\helpers\BaseHtml::activeInput('text', NULL, 'id', Array)
#2 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\widgets\ActiveField.php(362): yii\helpers\BaseHtml::activeTextInput(NULL, 'id', Array)
#3 D:\xampp\htdocs\test-rel-trait\frontend\views\user\_form.php(32): yii\widgets\ActiveField->textInput(Array)
#4 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\base\View.php(325): require('D:\\xampp\\htdocs...')
#5 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\base\View.php(247): yii\base\View->renderPhpFile('D:\\xampp\\htdocs...', Array)
#6 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\base\View.php(149): yii\base\View->renderFile('D:\\xampp\\htdocs...', Array, NULL)
#7 D:\xampp\htdocs\test-rel-trait\frontend\views\user\create.php(19): yii\base\View->render('_form', Array)
#8 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\base\View.php(325): require('D:\\xampp\\htdocs...')
#9 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\base\View.php(247): yii\base\View->renderPhpFile('D:\\xampp\\htdocs...', Array)
#10 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\base\View.php(149): yii\base\View->renderFile('D:\\xampp\\htdocs...', Array, Object(frontend\controllers\UserController))
#11 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\base\Controller.php(378): yii\base\View->render('create', Array, Object(frontend\controllers\UserController))
#12 D:\xampp\htdocs\test-rel-trait\frontend\controllers\UserController.php(89): yii\base\Controller->render('create', Array)
#13 [internal function]: frontend\controllers\UserController->actionCreate()
#14 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\base\InlineAction.php(55): call_user_func_array(Array, Array)
#15 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\base\Controller.php(154): yii\base\InlineAction->runWithParams(Array)
#16 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\base\Module.php(454): yii\base\Controller->runAction('create', Array)
#17 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\web\Application.php(84): yii\base\Module->runAction('user/create', Array)
#18 D:\xampp\htdocs\test-rel-trait\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#19 D:\xampp\htdocs\test-rel-trait\frontend\web\index.php(18): yii\base\Application->run()
#20 {main}
2016-06-07 08:20:44 [::1][1][uqb0bhumv1mlind24cajj4dfj2][info][application] $_GET = [
    'r' => 'user/create'
]

Additional info

| Q | A |
| --- | --- |
| Yii version | 2.0.8 |
| PHP version | 7.0.4 |
| Operating system | Win 10 Pro x64 |

All 5 comments

It's expected and documented. It should be used as $model->agent.

I've tried $model->agent too, it still not work..
When I var_dump($model->agent) it returns null because the model is new.

What do you mean by new? Model wasn't saved yet? If yes, how should it get relations at all?

OK, then I'm wrong..Thank you for your time..

very trash yii2, best speed, poor structure

Was this page helpful?
0 / 5 - 0 ratings