PublishableBehavior for CakePHP

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:

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

Or, if you would like to specify your columns:

<?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:

<?php 

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

?> 

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

You can find the code our public GitHub account. This article has been published at The Bakery.


About Me

My name is Barry Jones and Brightball, Inc was my contract development company from 2008-2012. Although I no longer do contract work this site has become my personal blog.

If you enjoy my writing and want to support the habit please use my referrals to help me pay for my servers with Digital Ocean, my DNS with DNS Made Easy, my email with Sendwithus and my focus with Freedom.

You're also welcome to buy some things that I highly recommend, such as the best movie ever made, a hilarious cookbook for microwave dependent men like myself, the best travel laptop backpack I've ever owned, an awesome lap desk, some of the best headphones out there for the money or maybe an iPad Mini for the kids?

If you just came here to learn by all means keep it up. Grab a book on Elixir, Phoenix, Go, encryption throughout history, hacking stories or running a business. Thanks!

Follow Me

  • Twitter
  • LinkedIn
  • slideshare
  • StackOverflow
  • Github
  • RSS