PublishableBehavior

This article is also available in the following languages:
By brightball
PublishableBehavior allows the use of datetime fields for start and end ranges on content. Included functionality allows for checking published status, toggling to published / unpublished status, and adding conditions to a find to properly filter those results
In order to use Publishable you only need to add to columns to your table. A datetime field for starting publishing and a datetime field for ending it. These default to 'begin_publishing' and 'end_publishing' but you can set them to anything you like. These columns also must be nullable.

Apply the behavior to your model like so:

Model Class:

<?php 
var $actsAs = array('Publishable');
?>

Or, if you would like to specify your columns:

Model Class:

<?php 
var $actsAs = array(
    
'Publishable' => array(
        
'start_column' => 'start_datetime_column',
        
'end_column' => 'end_datetime_column'
        
)
     );
?>

The Behavior checks to make sure the required columns are present when initializing and will trigger an error if they cannot be found.

With the Behavior applied, you'll now have access to these functions:

- isPublished($id = '') - Returns true or false based on the published status of the record.
//If $id is not passed, will attempt to use $model->id

- publish($id = array()) - Can either take a single id, a list of ids, or no id to attempt to use $model->id. This will set the published status of the record(s) to a start time of now and a null end time.

- unpublish($id = array()) - Can either take a single id, a list of ids, or no id to attempt to use $model->id. This will set the published status of the record(s) to a start time of null and an end time of null.

- publishConditions($published = true) - Returns a conditions array used to retrieve published or unpublished records that can be used in your own queries / model relationships.

No records are automatically filtered though. If you'd like to retrieve only published records from a query, simply add 'published' => true to the find options, like so:

Controller Class:

<?php 
$this
->SomeModel->find('all',array('published' => true));
?>

You can also pass false to get only the unpublished records.

You can find the code on my public GitHub account here:

http://github.com/brightball/open-source/tree/master
I've updated my GitHub account path, so your prior links may be broken. The new path is available above.

Comments

  • Posted 05/24/11 01:59:40 AM
    What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.
    I wait behind the visit … Techno News
  • Posted 05/29/10 07:51:58 AM
    I want to get my Published Articlecategories in my shop_app_controller.php

    Controller Class:

    <?php  
    $articlecategories
    $this->Articlecategory->generatetreelist(array('published' => true), "{n}.Articlecategory.id","{n}.Articlecategory.name""_"); 
    ?>

    in my model articlecategory.php i have:

    Model Class:

    <?php 
    public $actsAs = array('Translateable''Translate' => array('template','theme','name'=>'nameTranslated','content'),'Tree','Publishable' => array( 
            
    'start_column' => 'begin_publishing'
            
    'end_column' => 'end_publishing' 
            
    ));
    ?>

    but somehow it gives me a Warning
    "SQL Error: 1054: Unknown column 'published' in 'where clause'"

    anyone knows what i am doing wrong ?

    (Without the Condition array or with Condition e.g. (array('Articlecategory.id' => '3')) it works fine!)

    Thx 4 any Tips
    ixopo

Comments are closed for articles over a year old