/crud

CRUD CakePHP Behavior Plugin

Primary LanguagePHP

CRUD Behavior Plugin

Generic CRUD functions for your model.

Background

After using CakeDC templates plugin, I realized that much of the generated code was duplicated. So I made this plugin.

Requirements

  • PHP 4+
  • CakePHP 2.0+

Installation

[Manual]

  1. Download this: http://github.com/rodrigorm/crud/zipball/master
  2. Unzip that download.
  3. Copy the resulting folder to app/plugins
  4. Rename the folder you just copied to Crud

[GIT Submodule]

In your app directory type:

git submodule add git://github.com/rodrigorm/crud.git plugins/Crud
git submodule init
git submodule update

[GIT Clone]

In your plugin directory type

git clone git://github.com/rodrigorm/crud.git Crud

Usage

Enabling the behavior on model


public $actsAs = array('Crud.Crud');

View


public function view($id = null) {
	try {
		$item = $this->Post->view($id);
	} catch (OutOfBoundsException $e) {
		$this->Session->setFlash($e->getMessage());
		return $this->redirect(array('action' => 'index'));
	}
	$this->set('post', $item);
}

Add


public function add() {
	try {
		$result = $this->Post->add($this->data);
		if ($result === true) {
			$this->Session->setFlash('The Post has been saved');
			$this->redirect(array('action' => 'index'));
		}
	} catch (OutOfBoundsException $e) {
		$this->Session->setFlash($e->getMessage());
	} catch (Exception $e) {
		$this->Session->setFlash($e->getMessage());
		$this->redirect(array('action' => 'index'));
	}
}

Edit


public function edit($id = null) {
	try {
		$result = $this->Post->edit($id, $this->data);
		if ($result === true) {
			$this->Session->setFlash(sprintf('Post saved');
			$this->redirect(array('action' => 'view', $this->Post->data['Post']['id']));
		} else {
			$this->data = $result;
		}
	} catch (OutOfBoundsException $e) {
		$this->Session->setFlash($e->getMessage());
		$this->redirect('/');
	}
}

Delete


public function delete($id = null) {
	try {
		$result = $this->Post->validateAndDelete($id, $this->data);
		if ($result === true) {
			$this->Session->setFlash('Post deleted');
			$this->redirect(array('action' => 'index'));
		} else {
			$this->set('post', $result);
		}
	} catch (Exception $e) {
		$this->Session->setFlash($e->getMessage());
		$this->redirect(array('action' => 'index'));
	}
}

License

Copyright © 2011 Rodrigo Moyle

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.