在PhpStorm Yii2中动态声明?

时间:2019-01-10 08:19:07

标签: php yii2

我在模型中有一个功能

  public static function updateDetail($model)
    {
 $model->username = $model->partnerApiDetail->username;
            $model->password = $model->partnerApiDetail->password;
}
  

用户名和密码是伙伴表中的公共属性

     

PatnerApiDetail是合作伙伴表与partner_api_detail的关系   桌子。

     

为什么phpstorm找不到关系都显示错误
  $ model-username(字段声明为动态错误)

     

$ model-> partnerApiDetail->用户名(未找到字段partnerAapiDetail)

     

我实际上要做的是改写错误

<?php

namespace common\models;

use Yii;
use yii\db\Expression;

/**
 * This is the model class for table "ad_partner_api_detail".
 *
 * @property int $id
 * @property string $username
 * @property string $password
 * @property string $access_token
 * @property bool $is_deleted
 * @property int $is_active
 * @property int $created_by
 * @property int $updated_by
 * @property string $created_at
 * @property string $updated_at
 * @property int $ad_partner_id
 *
 * @property Partner $Partner
 */
class PartnerApiDetail extends \yii\db\ActiveRecord
{
    const statusDeleted = false;
    const statusActive = 1;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'ad_partner_api_detail';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['is_deleted'], 'boolean'],
            [['is_active', 'created_by', 'updated_by', 'ad_partner_id'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['ad_partner_id'], 'required'],
            [['username', 'password', 'access_token'], 'string', 'max' => 255],
            [['ad_partner_id'], 'exist', 'skipOnError' => true, 'targetClass' => Partner::class, 'targetAttribute' => ['ad_partner_id' => 'id']],
            ['is_active', 'default', 'value' => self::statusActive],
            ['is_deleted', 'default', 'value' => self::statusDeleted],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'            => Yii::t('app', 'ID'),
            'username'      => Yii::t('app', 'Username'),
            'password'      => Yii::t('app', 'Password'),
            'access_token'  => Yii::t('app', 'Access Token'),
            'is_deleted'    => Yii::t('app', 'Is Deleted'),
            'is_active'     => Yii::t('app', 'Status'),
            'created_by'    => Yii::t('app', 'Created By'),
            'updated_by'    => Yii::t('app', 'Updated By'),
            'created_at'    => Yii::t('app', 'Created At'),
            'updated_at'    => Yii::t('app', 'Updated At'),
            'ad_partner_id' => Yii::t('app', 'Partner Name'),
        ];
    }

    /**
     * {@BlameableBehavior}
     */

    public function behaviors()
    {
        return [
            [
                'class'              => 'yii\behaviors\BlameableBehavior',
                'createdByAttribute' => 'created_by',
                'updatedByAttribute' => 'updated_by',
            ],
            [
                'class'              => 'yii\behaviors\TimestampBehavior',
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at',
                'value'              => new Expression('NOW()'),
            ],
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPartner()
    {
        return $this->hasOne(Partner::class, ['id' => 'ad_partner_id']);
    }

}

1 个答案:

答案 0 :(得分:-1)

I have found the solution myself 
Baically yii defined the property of relation with capital letter at the start 
like here
 * @property PartnerApiDetail[] $PartnerApiDetail
for this rlation
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPartnerApiDetail()
    {
        return $this->hasOne(PartnerApiDetail::class, ['ad_partner_id' => 'id']);
    }
You just need to small the first letter
 * @property PartnerApiDetail[] $partnerApiDetail
Working like cham