Skip to content

Commit

Permalink
Multi unique column support for naming tag names
Browse files Browse the repository at this point in the history
  • Loading branch information
fruit committed Dec 20, 2009
1 parent 7c8366a commit e9c934b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 24 deletions.
27 changes: 21 additions & 6 deletions README
Expand Up @@ -118,6 +118,15 @@ Install the plugin
#Cachetaggable:
# uniqueColumn: id # you can customize unique column name (default is "id")
# versionColumn: object_version # you can customize column name to store versions (default is "object_version")
# uniqueKeyFormat: '%d' # you can customize key format (default is "%d")
#
# # if you have more then 1 unique column, you could pass all of them
# # as array (tag name will be based on them)
#
# uniqueColumn: [id, is_enabled]
# uniqueKeyFormat: '%d-%02b' # the order of unique columns
# # matches the "uniqueKeyFormat" template variables order

columns:
id:
type: integer
Expand All @@ -131,7 +140,6 @@ Install the plugin
type: many
local: id
foreign: blog_post_id
cascade: [delete]

BlogPostComment:
tableName: blog_post_comment
Expand All @@ -151,6 +159,10 @@ Install the plugin
message: string(255)
indexes:
blog_post_id: { fields: [blog_post_id] }
relations:
BlogPost:
onUpdate: CASCADE
onDelete: CASCADE

1. Enable cache in ``settings.yml`` and add additionals helpers to ``standart_helpers``

Expand Down Expand Up @@ -382,13 +394,16 @@ Install the plugin
}
}

## Limitations ##
## Limitations / Peculiarities ##

Do not use Doctrine::getTable('BlogPost')->delete()->where('id > ?', 10)->execute();
* Do not use ``Doctrine::getTable('BlogPost')->delete()->where('id > ?', 10)->execute();``
Object deletion should be performed using ``Doctrine_Record::delete()`` method.
This is necessary to remove object tags when object is fizicaly removed.
Otherwise your cache will be not expired.

Object deletion should be performed using ``Doctrine_Record::delete()`` method.
This is necessary to remove object tags when object is fizicaly removed.
Otherwise your cache will be not expired.
* In case, when model has translations (I18n behavior), it is enough to add
"``actAs: Cachetaggable``" to the model. I18n behavior should be free from ``Cachetaggable``
behavior.

## Unit test ##

Expand Down
42 changes: 35 additions & 7 deletions lib/doctrine/template/Cachetaggable.class.php
Expand Up @@ -23,8 +23,9 @@ class Doctrine_Template_Cachetaggable extends Doctrine_Template
* @var string
*/
protected $_options = array(
'uniqueColumn' => 'id',
'versionColumn' => 'object_version',
'uniqueColumn' => 'id',
'uniqueKeyFormat' => '%d',
'versionColumn' => 'object_version',
);

/**
Expand Down Expand Up @@ -73,11 +74,38 @@ public function getTagName ()
{
throw new LogicException('To call ->getTagName() you should save it before');
}

return sprintf(
'%s_%s',
get_class($object),
$object->{$this->_options['uniqueColumn']}

$columnValues = array(get_class($object));

foreach ((array) $this->_options['uniqueColumn'] as $column)
{
$methodName = sprintf('get%s', sfInflector::camelize($column));

$callable = new sfCallable(array($object, $methodName));

try
{
$columnValues[] = $callable->call();
}
catch (Exception $e)
{
throw new sfConfigurationException(
sprintf(
'Table "%s" does not have a column "%s". ' .
'After you fix this column name, you should rebuild your models',
sfInflector::tableize(get_class($object)),
$column
)
);
}
}

return call_user_func_array(
'sprintf',
array_merge(
array("%s_{$this->_options['uniqueKeyFormat']}"),
$columnValues
)
);
}

Expand Down
36 changes: 28 additions & 8 deletions package.xml
Expand Up @@ -4,28 +4,31 @@
<channel>pear.symfony-project.com</channel>
<summary>Cache tagging plugin compatible with any cache backends</summary>
<description>
The `sfCacheTaggingPlugin` is a symfony plugin to store caches associated
with unique tags to keep cache content up-to-date based by incrementing
tags version. Work only with Doctrine.
The ``sfCacheTaggingPlugin`` is a Symfony plugin, that helps to store cache with
associated tags and to keep cache content up-to-date based by incrementing tag
version when cache objects are edited/removed or new objects are ready to be a
part of cache content.
</description>
<lead>
<name>Ilya Sabelnikov</name>
<user>ilya</user>
<email>fruit.dev@gmail.com</email>
<active>yes</active>
</lead>
<date>2009-12-18</date>
<date>2009-12-20</date>
<version>
<release>1.0.1</release>
<api>1.0.0</api>
<release>1.1.0</release>
<api>1.0.1</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.symfony-project.com/license">MIT</license>
<notes>
* ilya: correcting lexical/grammatical mistakes
* ilya: update README
* ilya: tag name could be based on many columns
* ilya: checked opportunity to work with I18n behavior
</notes>
<contents>
<dir name="/">
Expand Down Expand Up @@ -96,6 +99,23 @@
</dependencies>
<phprelease />
<changelog>
<release>
<version>
<release>1.1.0</release>
<api>1.0.1</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<date>2009-12-20</date>
<license uri="http://www.symfony-project.com/license">MIT</license>
<notes>
* ilya: update README
* ilya: tag name could be based on many columns
* ilya: checked opportunity to work with I18n behavior
</notes>
</release>
<release>
<version>
<release>1.0.1</release>
Expand Down Expand Up @@ -142,4 +162,4 @@
</notes>
</release>
</changelog>
</package>
</package>
27 changes: 24 additions & 3 deletions test/data/fixtures/fixtures.yml
@@ -1,7 +1,28 @@
BlogPost:
a: { title: Hello A }
b: { title: Bye B }
c: { title: Me away C }
a:
title: Hello A
is_enabled: 1
Translation:
en:
content: 'Hello, Hello, Hello'
ru:
content: 'Хеллоу, Хеллоу, Хеллоу'
b:
title: Bye B
is_enabled: 0
Translation:
en:
content: 'Bye, Bye, Bye'
ru:
content: 'Ата, Ата, Ата'
c:
title: Me away C
is_enabled: 1
Translation:
en:
content: 'Offline, Offline, Offline'
ru:
content: 'Отошел, Отошел, Отошел'

BlogPostComment:
ac0: { author: fruit, message: 'Hi!', BlogPost: a }
Expand Down

0 comments on commit e9c934b

Please sign in to comment.