Created
November 21, 2010 10:22
-
-
Save arghav/708623 to your computer and use it in GitHub Desktop.
Filters Lazy-load
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace app\models; | |
| use lithium\util\collection\Filters; | |
| use \lithium\util\Inflector; | |
| Filters::apply('app\models\Post', 'save', function($self, $params, $chain) { | |
| // Custom pre-dispatch logic goes here | |
| //create a slug for new posts. | |
| if(!($params['entity']->exists())){ | |
| $slug = Inflector::slug($params['data']['title']); | |
| //count of slugs which start with $slug | |
| $count = Post::find('count', array( | |
| 'fields' => array('id'), | |
| 'conditions' => array('slug' => array('like' => '/^(?:' . $slug . ')(?:-?)(?:\d?)$/i')), | |
| )); | |
| $params['data']['slug'] = $slug . ($count ? "-" . (++$count) : ''); //add slug-X only if $count > 0 | |
| } | |
| $response = $chain->next($self, $params, $chain); | |
| // $response now contains the return value of the dispatched request, | |
| // and can be modified as appropriate | |
| // ... | |
| return $response; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment