Yii Framework Forum: Taggable Behaviour - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Taggable Behaviour This extension allows active record model to manage tags. Rate Topic: -----

#21 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,339
  • Joined: 17-January 09
  • Location:Russia

Posted 04 March 2011 - 05:01 AM

That means you don't have $model->tags. How you've attached behavior?
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#22 User is offline   psikocrisis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 15-July 10
  • Location:Madrid / Sardinia

Posted 04 March 2011 - 05:08 AM

View Postsamdark, on 04 March 2011 - 05:01 AM, said:

That means you don't have $model->tags. How you've attached behavior?


I have debugged this, $model->tags returns an Array of Tag objects...

In my model I have:
public function behaviors() {
		return array(
				'tags' => array(
						'class' => 'application.extensions.taggable.ETaggableBehavior',
						'tagTable' => 'Tag',
						'tagBindingTable' => 'Event_has_Tag',
						'tagBindingTableTagId' => 'tag_id',
						'modelTableFk' => 'event_id',
						'tagTablePk' => 'id',
						'tagTableName' => 'name',
						'scope' => array(
								'condition' => 'public = 1 OR user_id ='. Yii::app()->user->id,
						),
						'insertValues' => array(
								'user_id' => Yii::app()->user->id,
						),
				)
		);
}

0

#23 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,339
  • Joined: 17-January 09
  • Location:Russia

Posted 04 March 2011 - 05:13 AM

Looks like you have same named AR relation.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
1

#24 User is offline   psikocrisis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 15-July 10
  • Location:Madrid / Sardinia

Posted 04 March 2011 - 05:51 AM

View Postsamdark, on 04 March 2011 - 05:13 AM, said:

Looks like you have same named AR relation.


:lol:

Thanks man!!!
0

#25 User is offline   dhaos 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 10-August 11

Posted 10 August 2011 - 01:58 AM

the tag_entity to record tag_id, entity_id and entity_type, that make tag fits any other entity(table)
and I've modified class for our use, by adding variable named 'insertBidingValues' and related snappet
but 'insertValues' is not work
	public function behaviors(){
		return array(
			'tags'=>array(
				'class'=>'ext.yiiext.behaviors.model.taggable.EARTaggableBehavior',
				'tagTable'=>'tag',
				'tagBindingTable'=>'tag_entity',
				'modelTableFk'=>'entity_id',
				'tagTablePk'=>'id',
				'tagTableName'=>'title',
				'tagBindingTableTagId'=>'tag_id',
				'cacheID'=>'tag_cache',
				'createTagsAutomatically'=>true,
				'insertValues'=>array(
					'parent_id'=>0
				),
				'insertBindingValues'=>array(
					'entity_type'=>'article'
				),
			)
		);
	}

0

#26 User is offline   julianm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 04-January 12

Posted 07 April 2012 - 04:08 PM

Is this still active? Anybody using it successfully? I am trying to download the code but seems only source code is available and no a release version yet. Anybody can advice? I am looking for an extension or behavior for Yii that let me manage Tags as you exposed. Thanks.
0

#27 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,339
  • Joined: 17-January 09
  • Location:Russia

Posted 23 April 2012 - 04:55 AM

It's alive and kicking :)

http://yiiext.github...vior/index.html
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#28 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 23 April 2012 - 05:33 AM

I've used it successfully in several projects. :)
"Less noise - more signal"
0

#29 User is offline   NCS_One 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 24-August 11

Posted 12 June 2012 - 01:04 PM

Hi,

Thanks for the great extension.

I'm trying to use it but getting a error.

// Post.php

  public function behaviors()
  {
    return array(
      'tags-with-model'=>array(
        'class'=>'ext.taggable.EARTaggableBehavior',
        'tagTable'=>'tag',
        'tagModel'=>'Tag',
        'tagBindingTable'=>'post_tag',
        'modelTableFk'=>'post_id',
        'tagTablePk'=>'id',
        'tagTableName'=>'name',
        'tagTableCount'=>'count',
        'tagBindingTableTagId'=>'tag_id',
      ),
    );
  }



// PostController.php

  public function actionCreate()
  {
    ...
    try
    {
      $model->setTags('test')->save('false');
      ...
    ...
  }


I'm using Postgresql 8.4
// tag.sql

DROP TABLE IF EXISTS tag CASCADE;
DROP TABLE IF EXISTS review_tag CASCADE;

CREATE TABLE tag
(
  id serial,
  name character varying(255) NOT NULL,
  count integer DEFAULT 1,
  PRIMARY KEY (id),
  CONSTRAINT tag_name UNIQUE (name)
);

CREATE TABLE post_tag
(
  post_id integer NOT NULL,
  tag_id integer NOT NULL,
  PRIMARY KEY (post_id, tag_id)
);


// application.log

2012/06/12 18:49:22 [error] [application] actionCreate(); exception 'PDOException' with message 'SQLSTATE[42602]: Invalid name: 7 ERROR:  invalid name syntax' in /var/www/yii-1.1.10.r3566/framework/db/CDbConnection.php:535


// postgresql log

2012-06-12 18:49:22 WEST ERROR:  invalid name syntax
2012-06-12 18:49:22 WEST STATEMENT:  SELECT CURRVAL($1)
2012-06-12 18:49:22 WEST ERROR:  current transaction is aborted, commands ignored until end of transaction block
2012-06-12 18:49:22 WEST STATEMENT:  DEALLOCATE pdo_stmt_0000000f


Thanks for all the help.
0

#30 User is offline   NCS_One 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 24-August 11

Posted 12 June 2012 - 05:12 PM

I I enabled postgresql parameter logging.

2012-06-12 22:40:22 WEST DETAIL: parameters: $1 = 'tag_id_seq'
2012-06-12 22:40:22 WEST LOG: duration: 0.022 ms execute <unnamed>: SELECT CURRVAL($1)
2012-06-12 22:40:22 WEST DETAIL: parameters: $1 = 'tag_id_seq'
2012-06-12 22:40:22 WEST LOG: duration: 0.051 ms statement: DEALLOCATE pdo_stmt_00000014
2012-06-12 22:40:22 WEST LOG: duration: 0.062 ms parse <unnamed>: SELECT CURRVAL($1)
2012-06-12 22:40:22 WEST ERROR: invalid name syntax
2012-06-12 22:40:22 WEST STATEMENT: SELECT CURRVAL($1)
2012-06-12 22:40:22 WEST ERROR: current transaction is aborted, commands ignored until end of transaction block
2012-06-12 22:40:22 WEST STATEMENT: DEALLOCATE pdo_stmt_0000000f
2012-06-12 22:40:22 WEST LOG: duration: 0.040 ms statement: ROLLBACK

0

#31 User is offline   NCS_One 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 24-August 11

Posted 19 June 2012 - 03:04 PM

Ok,

My problem is on ETaggableBehavior->afterSave, on line 373:

  $tagId = $this->getConnection()->getLastInsertID();

0

#32 User is offline   NCS_One 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 24-August 11

Posted 20 June 2012 - 12:27 AM

If I change that line to:

  $tagId = $this->getConnection()->getLastInsertID('tag_id_seq');


it works.

what could be the issue?
0

#33 User is offline   PrplHaz4 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 107
  • Joined: 28-September 09
  • Location:Boston, MA

Posted 30 January 2013 - 10:03 AM

Can anyone recommend an approach to save the user who set a tag and when it was set?

I'm thinking that the best approach is a custom TaggableBehavior class that overrides afterSave() and saves the additional info using a technique similar to $insertValues in this block of code:


// bind tag to it's model
        $builder->createInsertCommand(
       	$this->getTagBindingTableName(),
				array(
					$this->getModelTableFkName() => $this->getOwner()->primaryKey,
					$this->tagBindingTableTagId => $tagId
       	)
     	)->execute();

0

#34 User is offline   Arely 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 15-February 13

Posted 26 September 2013 - 03:34 PM

Hi, is there some Step by Step for this extension? I'm new and I want to apply this on my project. I have the extension on the extension folder, the tag and post_tag tables. What I have to do now? The wiki is not helping too much:c Thanks!
0

#35 User is offline   PrplHaz4 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 107
  • Joined: 28-September 09
  • Location:Boston, MA

Posted 26 September 2013 - 08:21 PM

You need to also add the "tags" behavior to the Post AR Model (Post.php), then in your controller, you need to set the tags (usually based on form input), and then save the model (the behavior will take care of saving the needed tags/associations)
$post->setTags('tag1, tag2, tag3');
$post->save();


0

#36 User is offline   Arely 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 15-February 13

Posted 30 September 2013 - 11:12 AM

View PostPrplHaz4, on 26 September 2013 - 08:21 PM, said:

You need to also add the "tags" behavior to the Post AR Model (Post.php), then in your controller, you need to set the tags (usually based on form input), and then save the model (the behavior will take care of saving the needed tags/associations)
$post->setTags('tag1, tag2, tag3');
$post->save();





Ok, I want to use tags on a "Restaurant" Module. I put the behavior into Tag.ph. This is what i have on behavior:


function behaviors() {
	    return array(
	        'tags' => array(
	            'class'=>'ext.taggable.EARTaggableBehavior',
		        'tagTable'=>'tag',
		        'tagModel'=>'Tag',
		        'tagBindingTable'=>'post_tag',
		        'modelTableFk'=>'post_id',
		        'tagTablePk'=>'id',
		        'tagTableName'=>'name',
		        'tagTableCount'=>'count',
		        'tagBindingTableTagId'=>'tag_id',
		   	)
	    );
	}


I have a field named "tag" on my Restaurant _form because here is where I want to put the tags that will save.
<div class="row" >
        <?php echo $form->labelEx($model, 'tag'); ?>
        <?php echo $form->textArea($model,'tag'); ?>
        <?php echo $form->error($model, 'tag'); ?>
        
    </div>




On create function on RestaurantsController I have the next lines for extracting the tags from the field and trying to save them
$taag = $_POST['Restaurants']['tag'];
        	
        	$tagg =array();
        	$tagg = explode(" ", $taag);
        	
        	$post = new Tag();
        	foreach ($tagg as $t) {
        		$post->setTags($tagg);
        		$post->save();
        	}




But now is sending me this error Property "Tag.title" is not defined.

Am I doing something wrong. Is not the correct way I'm doing it? Thanks in advice.
0

#37 User is offline   PrplHaz4 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 107
  • Joined: 28-September 09
  • Location:Boston, MA

Posted 30 September 2013 - 11:15 PM

View PostArely, on 30 September 2013 - 11:12 AM, said:

Ok, I want to use tags on a "Restaurant" Module. I put the behavior into Tag.ph. This is what i have on behavior:


Sorry, not much time to really go over this, but I see something immediately - the tag behavior should be going in your Restaurant model - since that is the model that will be using the tags. I'm guessing it is Restaurant.php
0

#38 User is offline   LarryTX 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 22-June 10

Posted 10 March 2014 - 07:09 AM

I would like to have a CGridView column that displays all tags for each model listed in the index (list) and admin views. I've tried some thing like this in CGridView:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'              => 'disks-grid',
    'dataProvider'    => $model->search(),
    'filter'          => $model,
    'columns'         => array(
      // additional column definitions
        array(
            'name'   => 'tags_with_model',
            'header' => 'Tags',
            'type'   => 'raw',
            'filter' => FALSE,
            'value'  => $model->tags_with_model->toString(),
        ),


Unfortunately, that produces nothing. No errors, just a totally blank column. (Interestingly, I was able to use <?php echo $model->tags_with_model->toString(); ?> to display the tags in view.php, and it worked quite nicely.) Apparently, something happens in CGridView to prevent toString() from working.

By the way, my behavior in the model looks like:

return array(
				'tags_with_model' => array(
					'class'                   => 'ext.yiiext.behaviors.model.taggable.EARTaggableBehavior',
					'tagTable'                => 'video_tags',
					'tagModel'                => 'VideoTags',
					'tagBindingTable'         => 'video_disks_tags',
					'modelTableFk'            => 'disksId',
					'tagTablePk'              => 'id',
					'tagTableName'            => 'name',
					'tagTableCount'           => 'frequency',
					'tagBindingTableTagId'    => 'tagId',
					'cacheID'                 => 'cache',
					'createTagsAutomatically' => TRUE,
				)
			);
		}


I've also tried a virtual attribute like this:

public function getVideoTags() {
	return $this->tags_with_model->toString();
}


I added that to the CActiveDataProvider attributes in search() in the model and to CGridView, but I still get an empty column.

Can anyone tell me how to get a column in CGridView that displays the tags for each model?
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users