OmgDef/yii2-multilingual-behavior

Implementation of Has One and has many

Closed this issue · 2 comments

Hello. I have a problem with implementation hasOne and hasMany. My class:
`namespace common\models;

use backend\components\TreeTrait;
use omgdef\multilingual\MultilingualBehavior;
use Yii;

/**

  • This is the model class for table "news_categories".

  • @Property integer $id

  • @Property integer $site_id

  • @Property integer $parent_id

  • @Property string $url

  • @Property string $img

  • @Property integer $active

  • @Property Sites $site

  • @Property NewsCategoriesLang[] $newsCategoriesLangs
    /
    class NewsCategories extends \yii\db\ActiveRecord
    {
    use TreeTrait;
    /
    *

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

    /**

    • @inheritdoc
      */
      public function rules()
      {
      return [
      [['site_id', 'parent_id', 'active', 'noindex'], 'integer'],
      [['descr'], 'string'],
      [['url', 'img', 'title', 'descr', 'seo_t', 'seo_k'], 'string', 'max' => 255],
      [['seo_d'], 'string', 'max' => 1500],
      [['site_id'], 'exist', 'skipOnError' => true, 'targetClass' => Sites::className(), 'targetAttribute' => ['site_id' => 'id']],
      ];
      }

    public function behaviors()
    {
    return [
    'ml' => [
    'class' => MultilingualBehavior::className(),
    'languages' => [
    'ru-RU' => 'Russian',
    'en-US' => 'English',
    ],
    //'languageField' => 'language',
    //'localizedPrefix' => '',
    //'requireTranslations' => false',
    //'dynamicLangClass' => true',
    'langClassName' => NewsCategoriesLang::className(), // or namespace/for/a/class/PostLang
    'defaultLanguage' => 'ru',
    'langForeignKey' => 'news_categories_id',
    'tableName' => "{{%news_categoriesLang}}",
    'attributes' => [
    'title', 'descr', 'seo_t', 'seo_d', 'seo_k', 'noindex',
    ]
    ],
    ];
    }

    public static function find()
    {
    return new MultilingualQuery(get_called_class());
    }

    /**

    • @inheritdoc
      */
      public function attributeLabels()
      {
      return [
      'id' => 'ID',
      'site_id' => 'Site ID',
      'parent_id' => 'Parent ID',
      'url' => 'Url',
      'img' => 'Img',
      'active' => 'Active',
      ];
      }

    /**

    • @return \yii\db\ActiveQuery
      */
      public function getSite()
      {
      return $this->hasOne(Sites::className(), ['id' => 'site_id']);
      }

    public function getChilds()
    {
    return $this->hasMany(NewsCategories::className(), ['parent_id' => 'id']);
    }

    public function getParent()
    {
    return $this->hasOne(NewsCategories::className(), ['id' => 'parent_id']);
    }

    /**

    • @return \yii\db\ActiveQuery
      */
      // public function getNewsCategoriesLangs()
      // {
      // return $this->hasMany(NewsCategoriesLang::className(), ['news_categories_id' => 'id']);
      // }
      }`

and when I call
$test = NewsCategories::find()->where(['id' => $id])->one(); print_r($test->parent->id); exit();
http://joxi.ru/82QM8wju1WzDj2

Hi! It's not related to this behavior. Make sure that parent of the model really exists in your DB.

Sorry, I founded an error