Taggable Behaviour

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,

						),

				)

		);

}



Looks like you have same named AR relation.

:lol:

Thanks man!!!

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'

				),

			)

		);

	}

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.

It’s alive and kicking :)

http://yiiext.github.com/extensions/taggable-behavior/index.html

I’ve used it successfully in several projects. :)

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.

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



Ok,

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




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



If I change that line to:




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



it works.

what could be the issue?

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();



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!

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.

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

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?