This is a simple behavior component.
Unpack the package and place at /protected/components
In the model where you'd want to use, declare this behavior via its behvaiors() method, as follows:
public function behaviors() { return array( 'slugmaker' => array( 'class' => 'PcSimpleSlugBehavior', // 'sourceIdAttr' => the 'id' attribute name in this model. Default value = 'id' // 'sourceStringPrepareMethod' => If defined, will be used to get the base slug string text. Use it if you need some small manipulation of data in the model object in order to 'get' the base slug string. For example, if you need to concatenate 'first_name' and 'last_name' fields together. // 'sourceStringAttr' => the 'main string attribute' from which the slug will be built. Typically 'title', which is the default as well. // 'maxChars' => Maximum allowed slug total length. resulted slug will be trimmed to this value if longer than this value (*with* the prepended 'id-'...) // 'avoidIdPrefixing' => Setting this to true (default = false) will enable you to generate and parse URLs without the ID of the model record. WARNING: you need to carefully think before choosing this method as if not well thought, it could be used in an environment where there could be two or more records with same slug! ) ); }
Then, in the code you can do as follows:
// the following will generat the slug for you: $model->generateUniqueSlug()
// get id of article from slug $dummy = MyModel::model(); $id = $dummy->getIdFromSlug($slug_fetched_from_url); $my_model_obj = MyModel::model()->findByPk($id);
Total 2 comments
Ya I got it from souce
Hi,
Very useful extension.
But I unable to get attribute "id" via "getIdFromSlug" when 'avoidIdPrefixing' => true;
Please help me...
Leave a comment
Please login to leave your comment.