{"id":13411522,"url":"https://github.com/creocoder/yii2-taggable","last_synced_at":"2025-04-06T07:15:22.422Z","repository":{"id":25434611,"uuid":"28864288","full_name":"creocoder/yii2-taggable","owner":"creocoder","description":"The taggable behavior for the Yii framework.","archived":false,"fork":false,"pushed_at":"2018-10-02T11:51:11.000Z","size":539,"stargazers_count":135,"open_issues_count":14,"forks_count":33,"subscribers_count":23,"default_branch":"master","last_synced_at":"2024-05-15T17:11:45.424Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/creocoder.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-06T13:17:26.000Z","updated_at":"2023-12-20T11:29:14.000Z","dependencies_parsed_at":"2022-07-26T07:02:02.526Z","dependency_job_id":null,"html_url":"https://github.com/creocoder/yii2-taggable","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creocoder%2Fyii2-taggable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creocoder%2Fyii2-taggable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creocoder%2Fyii2-taggable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creocoder%2Fyii2-taggable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/creocoder","download_url":"https://codeload.github.com/creocoder/yii2-taggable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247445682,"owners_count":20939961,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T20:01:14.231Z","updated_at":"2025-04-06T07:15:22.403Z","avatar_url":"https://github.com/creocoder.png","language":"PHP","readme":"# Taggable Behavior for Yii 2\n\n[![Build Status](https://img.shields.io/travis/creocoder/yii2-taggable/master.svg?style=flat-square)](https://travis-ci.org/creocoder/yii2-taggable)\n[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/creocoder/yii2-taggable/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/creocoder/yii2-taggable/?branch=master)\n[![Code Quality](https://img.shields.io/scrutinizer/g/creocoder/yii2-taggable/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/creocoder/yii2-taggable/?branch=master)\n[![Packagist Version](https://img.shields.io/packagist/v/creocoder/yii2-taggable.svg?style=flat-square)](https://packagist.org/packages/creocoder/yii2-taggable)\n\nA modern taggable behavior for the Yii framework.\n\n## Installation\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```bash\n$ composer require creocoder/yii2-taggable\n```\n\nor add\n\n```\n\"creocoder/yii2-taggable\": \"~2.0\"\n```\n\nto the `require` section of your `composer.json` file.\n\n## Migrations\n\nRun the following command\n\n```bash\n$ yii migrate/create create_post_table\n```\n\nOpen the `/path/to/migrations/m_xxxxxx_xxxxxx_create_post_table.php` file,\ninside the `up()` method add the following\n\n```php\n$this-\u003ecreateTable('{{%post}}', [\n    'id' =\u003e Schema::TYPE_PK,\n    'title' =\u003e Schema::TYPE_STRING . ' NOT NULL',\n    'body' =\u003e Schema::TYPE_TEXT . ' NOT NULL',\n]);\n```\n\nRun the following command\n\n```bash\n$ yii migrate/create create_tag_table\n```\n\nOpen the `/path/to/migrations/m_xxxxxx_xxxxxx_create_tag_table.php` file,\ninside the `up()` method add the following\n\n```php\n$this-\u003ecreateTable('{{%tag}}', [\n    'id' =\u003e Schema::TYPE_PK,\n    'name' =\u003e Schema::TYPE_STRING . ' NOT NULL',\n    'frequency' =\u003e Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 0',\n]);\n```\n\nRun the following command\n\n```bash\n$ yii migrate/create create_post_tag_assn_table\n```\n\nOpen the `/path/to/migrations/m_xxxxxx_xxxxxx_create_post_tag_assn_table.php` file,\ninside the `up()` method add the following\n\n```php\n$this-\u003ecreateTable('{{%post_tag_assn}}', [\n    'post_id' =\u003e Schema::TYPE_INTEGER . ' NOT NULL',\n    'tag_id' =\u003e Schema::TYPE_INTEGER . ' NOT NULL',\n]);\n\n$this-\u003eaddPrimaryKey('', '{{%post_tag_assn}}', ['post_id', 'tag_id']);\n```\n\n## Configuring\n\nConfigure model as follows\n\n```php\nuse creocoder\\taggable\\TaggableBehavior;\n\n/**\n * ...\n * @property string $tagValues\n */\nclass Post extends \\yii\\db\\ActiveRecord\n{\n    public function behaviors()\n    {\n        return [\n            'taggable' =\u003e [\n                'class' =\u003e TaggableBehavior::className(),\n                // 'tagValuesAsArray' =\u003e false,\n                // 'tagRelation' =\u003e 'tags',\n                // 'tagValueAttribute' =\u003e 'name',\n                // 'tagFrequencyAttribute' =\u003e 'frequency',\n            ],\n        ];\n    }\n\n    public function rules()\n    {\n        return [\n            //...\n            ['tagValues', 'safe'],\n        ];\n    }\n\n    public function transactions()\n    {\n        return [\n            self::SCENARIO_DEFAULT =\u003e self::OP_ALL,\n        ];\n    }\n\n    public static function find()\n    {\n        return new PostQuery(get_called_class());\n    }\n\n    public function getTags()\n    {\n        return $this-\u003ehasMany(Tag::className(), ['id' =\u003e 'tag_id'])\n            -\u003eviaTable('{{%post_tag_assn}}', ['post_id' =\u003e 'id']);\n    }\n}\n```\n\nModel `Tag` can be generated using Gii.\n\nConfigure query class as follows\n\n```php\nuse creocoder\\taggable\\TaggableQueryBehavior;\n\nclass PostQuery extends \\yii\\db\\ActiveQuery\n{\n    public function behaviors()\n    {\n        return [\n            TaggableQueryBehavior::className(),\n        ];\n    }\n}\n```\n\n## Usage\n\n### Setting tags to the entity\n\nTo set tags to the entity\n\n```php\n$post = new Post();\n\n// through string\n$post-\u003etagValues = 'foo, bar, baz';\n\n// through array\n$post-\u003etagValues = ['foo', 'bar', 'baz'];\n```\n\n### Adding tags to the entity\n\nTo add tags to the entity\n\n```php\n$post = Post::findOne(1);\n\n// through string\n$post-\u003eaddTagValues('bar, baz');\n\n// through array\n$post-\u003eaddTagValues(['bar', 'baz']);\n```\n\n### Remove tags from the entity\n\nTo remove tags from the entity\n\n```php\n$post = Post::findOne(1);\n\n// through string\n$post-\u003eremoveTagValues('bar, baz');\n\n// through array\n$post-\u003eremoveTagValues(['bar', 'baz']);\n```\n\n### Remove all tags from the entity\n\nTo remove all tags from the entity\n\n```php\n$post = Post::findOne(1);\n$post-\u003eremoveAllTagValues();\n```\n\n### Getting tags from the entity\n\nTo get tags from the entity\n\n```php\n$posts = Post::find()-\u003ewith('tags')-\u003eall();\n\nforeach ($posts as $post) {\n    // as string\n    $tagValues = $post-\u003etagValues;\n\n    // as array\n    $tagValues = $post-\u003egetTagValues(true);\n}\n```\n\nReturn type of `getTagValues` can also be configured globally via `tagValuesAsArray` property.\n\n### Checking for tags in the entity\n\nTo check for tags in the entity\n\n```php\n$post = Post::findOne(1);\n\n// through string\n$result = $post-\u003ehasTagValues('foo, bar');\n\n// through array\n$result = $post-\u003ehasTagValues(['foo', 'bar']);\n```\n\n### Search entities by any tags\n\nTo search entities by any tags\n\n```php\n// through string\n$posts = Post::find()-\u003eanyTagValues('foo, bar')-\u003eall();\n\n// through array\n$posts = Post::find()-\u003eanyTagValues(['foo', 'bar'])-\u003eall();\n```\n\nTo search entities by any tags using custom tag model attribute\n\n```php\n// through string\n$posts = Post::find()-\u003eanyTagValues('foo-slug, bar-slug', 'slug')-\u003eall();\n\n// through array\n$posts = Post::find()-\u003eanyTagValues(['foo-slug', 'bar-slug'], 'slug')-\u003eall();\n```\n\n### Search entities by all tags\n\nTo search entities by all tags\n\n```php\n// through string\n$posts = Post::find()-\u003eallTagValues('foo, bar')-\u003eall();\n\n// through array\n$posts = Post::find()-\u003eallTagValues(['foo', 'bar'])-\u003eall();\n```\n\nTo search entities by all tags using custom tag model attribute\n\n```php\n// through string\n$posts = Post::find()-\u003eallTagValues('foo-slug, bar-slug', 'slug')-\u003eall();\n\n// through array\n$posts = Post::find()-\u003eallTagValues(['foo-slug', 'bar-slug'], 'slug')-\u003eall();\n```\n\n### Search entities related by tags\n\nTo search entities related by tags\n\n```php\n// through string\n$posts = Post::find()-\u003erelatedByTagValues('foo, bar')-\u003eall();\n\n// through array\n$posts = Post::find()-\u003erelatedByTagValues(['foo', 'bar'])-\u003eall();\n```\n\nTo search entities related by tags using custom tag model attribute\n\n```php\n// through string\n$posts = Post::find()-\u003erelatedByTagValues('foo-slug, bar-slug', 'slug')-\u003eall();\n\n// through array\n$posts = Post::find()-\u003erelatedByTagValues(['foo-slug', 'bar-slug'], 'slug')-\u003eall();\n```\n\n## Donating\n\nSupport this project and [others by creocoder](https://gratipay.com/creocoder/) via [gratipay](https://gratipay.com/creocoder/).\n\n[![Support via Gratipay](https://cdn.rawgit.com/gratipay/gratipay-badge/2.3.0/dist/gratipay.svg)](https://gratipay.com/creocoder/)\n","funding_links":[],"categories":["Behaviors 行为"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcreocoder%2Fyii2-taggable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcreocoder%2Fyii2-taggable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcreocoder%2Fyii2-taggable/lists"}