PHP code example of oxyaction / yii2-polymorphic-relation-behavior
1. Go to this page and download the library: Download oxyaction/yii2-polymorphic-relation-behavior library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
oxyaction / yii2-polymorphic-relation-behavior example snippets
class Article extends ActiveRecord
{
const ARTICLE = 1;
public function behaviors()
{
return [
'polymorphic' => [
'class' => RelatedPolymorphicBehavior::className(),
'polyRelations' => [
'comments' => Comment::className()
],
'polymorphicType' => self::TYPE_ARTICLE
]
];
}
}
class Photo extends ActiveRecord
{
const PHOTO = 2;
public function behaviors()
{
return [
'polymorphic' => [
'class' => RelatedPolymorphicBehavior::className(),
'polyRelations' => [
'comments' => Comment::className()
],
'polymorphicType' => self::TYPE_PHOTO
]
];
}
}
...
public function behaviors()
{
return [
'polymorphic' => [
'class' => RelatedPolymorphicBehavior::className(),
'polyRelations' => [
'comments' => Comment::className()
],
'polymorphicType' => 'article'
]
];
}
...
public function behaviors()
{
return [
'polymorphic' => [
'class' => RelatedPolymorphicBehavior::className(),
'polyRelations' => [
'comments' => [
'type' => RelatedPolymorphicBehavior::HAS_MANY,
'class' => Comment::className(),
'pkColumnName' => 'id',
'foreignKeyColumnName' => 'external_id',
'typeColumnName' => 'type',
'deleteRelated' => false,
]
],
'polymorphicType' => 'article'
]
]
}
public function behaviors()
{
return [
'polymorphic' => [
'class' => RelatedPolymorphicBehavior::className(),
'polyRelations' => [
'tags' => [
'type' => RelatedPolymorphicBehavior::MANY_MANY,
'class' => Tag::className(),
'viaTable' => 'taggable_tag',
]
],
'polymorphicType' => 'article'
]
]
}
public function behaviors()
{
return [
'polymorphic' => [
'class' => RelatedPolymorphicBehavior::className(),
'polyRelations' => [
'tags' => [
'type' => RelatedPolymorphicBehavior::MANY_MANY,
'class' => Tag::className(),
'viaTable' => 'taggable_tag',
'pkColumnName' => 'id',
'foreignKeyColumnName' => 'external_id',
'otherKeyColumnName' => 'tag_id',
'typeColumnName' => 'type',
'relatedPkColumnName' => 'id',
]
],
'polymorphicType' => 'photo'
]
]
}
class Article extends ActiveRecord {
public function behaviors()
{
return [
'polymorphic' => [
'class' => RelatedPolymorphicBehavior::className(),
'polyRelations' => [
'tags' => [
'type' => RelatedPolymorphicBehavior::MANY_MANY,
'class' => Tag::className(),
'viaTable' => 'taggable_tag',
],
'images' => [
'type' => RelatedPolymorphicBehavior::HAS_MANY,
'class' => Image::className(),
],
'comments' => [
'type' => RelatedPolymorphicBehavior::HAS_MANY,
'class' => Comment::className(),
],
],
'polymorphicType' => 'article',
'pkColumnName' => 'ID',
'foreignKeyColumnName' => 'some_external_id',
'typeColumnName' => 'entity_type',
]
]
}
}