article

Showing posts with label CakePHP. Show all posts
Showing posts with label CakePHP. Show all posts

Sunday, August 4, 2013

CakePHP Add,Edit,Delete and Validate

CakePHP Add,Edit,Delete and Validate

Create Database Table
CREATE TABLE `categories` (
  `id` INTEGER(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM

Crate Model app\models\category.php

<?php
class Category extends AppModel {
    var $name = 'Category';
 var $validate = array( //validate
        'name' => array(
            'rule' => 'notEmpty',
            'message' => 'No, no, this field must not be empty!'
        )
    );
}
?>
Create Controller app/controllers/categories_controller.php
<?php
class CategoriesController extends AppController {
 
    var $name = 'Categories';
 
    function index() {
        $this->set('categories', $this->Category->find('all'));
    }
     
    function add() {
        if (!empty($this->data)) {
            if ($this->Category->save($this->data)) {
                $this->Session->setFlash('Your category has been saved.');
                $this->redirect(array('action' => 'index'));
            }
        }
    }
 function delete($id) {
        $this->Category->delete($id);
        $this->Session->setFlash('The category with id: '.$id.' has been deleted.');
        $this->redirect(array('action'=>'index'));
    }
 function edit($id = null) {
        $this->Category->id = $id;
        if (empty($this->data)) {
            $this->data = $this->Category->read();
        } else {
            if ($this->Category->save($this->data)) {
                $this->Session->setFlash('Your category has been updated.');
                $this->redirect(array('action' => 'index'));
            }
        }
    }
}
?>
Create View index app/view/categories/index.ctp
<!-- File: /app/views/categories/index.ctp -->
<h1>Categories</h1>
<?php echo $html->link('Add Category',array('controller' => 'categories', 'action' => 'add')); ?>
 
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Actions</th>
    </tr>
 
    <?php foreach ($categories as $category): ?>
    <tr>
        <td><?php echo $category['Category']['id']; ?></td>
        <td>
            <?php echo $html->link($category['Category']['name'],
array('controller' => 'categories', 'action' => 'edit', $category['Category']['id'])); ?>
        </td>
        <td>
        <?php echo $html->link('Delete', array('action' => 'delete', $category['Category']['id']), null, 'Are you sure?' )?>
        <?php echo $html->link('Edit', array('action'=>'edit', $category['Category']['id']));?>
        </td>
    </tr>
    <?php endforeach; ?>
 
</table>
Create View Edit app/view/categories/edit.ctp
<h1>Edit Category</h1>
<?php
    echo $form->create('Category', array('action' => 'edit'));
    echo $form->input('name');
    echo $form->input('id', array('type'=>'hidden'));
    echo $form->end('Save Category');
?>
Create View add app/view/categories/add.ctp
<h1>Add Category</h1>
<?php
echo $form->create('Category');
echo $form->input('name');
echo $form->end('Save Post');
?>

CakePHP Making a small application

CakePHP Making a small application

Create Database Table

CREATE TABLE `categories` (
  `id` INTEGER(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM
Create Model app\models\category.php 


<?php
class Category extends AppModel {
    var $name = 'Category';
}
?>
Create Controller app\controllers\categories_controller.php
<?php
class CategoriesController extends AppController {
    var $name = 'Categories';
    function index() {
        $this->set('categories', $this->Category->find('all'));
    }
     
    function add() {
        if (!empty($this->data)) {
            if ($this->Category->save($this->data)) {
                $this->Session->setFlash('Your category has been saved.');
                $this->redirect(array('action' => 'index'));
            }
        }
    }
}
?>
Create View index app\views\categories\index.ctp
<!-- File: /app/views/categories/index.ctp -->
 <?php echo $html->link('Add Category',array('controller' => 'categories', 'action' => 'add')); ?>
<h1>Categories</h1>

<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
    </tr>
 
    <?php foreach ($categories as $category): ?>
    <tr>
        <td><?php echo $category['Category']['id']; ?></td>
        <td>
            <?php echo $html->link($category['Category']['name'],
array('controller' => 'categories', 'action' => 'view', $category['Category']['id'])); ?>
        </td>
    </tr>
    <?php endforeach; ?>
 
</table>
Create View add app\views\categories\add.ctp
<!-- File: /app/views/categories/add.ctp -->   
<h1>Add Category</h1>
<?php
echo $form->create('Category');
echo $form->input('name');
echo $form->end('Save Post');
?>

Wednesday, May 8, 2013

What is the fastest php framework?






















Benchmarked frameworks:

Friday, June 3, 2011

CakePHP GROUP BY

CakePHP GROUP BY



$this->Product->find(‘all’,array(‘fields’=>array(‘Product.type’,'MIN(Product.price) as price’), ‘group’=> ‘Product.type’));

Monday, May 16, 2011

Cakephp Custom group by pagination and a calculated field

Cakephp Custom group by pagination and a calculated field

Example of how to use the CakePHP paginator helper with the group by condition. this is from the original article from http://wiltonsoftware.com/posts/view/custom-group-by-pagination-and-a-calculated-field




In the Controller
<?
var $helpers = array('Paginator');
var $paginator = array('limit' => 20);

function admin_index($filter=null) {
$conditions = array();
$this->Comment->recursive = 0;
if ($filter == 'count') {
$conditions = array('Comment.status = 0');
$this->paginate['Comment'] = array(
'fields' => array(
'Comment.id', 'Comment.ip', 'Count(Comment.ip) as Count'
),
'conditions' => array(
'Comment.status = 0'
),
'group' => array(
'Comment.ip'
),
'order' => array(
'Count' => 'desc'
)
);
$data = $this->paginate('Comment', $conditions);

} else {
if ($filter == 'spam') {
$conditions = array('Comment.status = 0');
} else {
$conditions = array('Comment.status > 0');
}
$this->paginate['Comment'] = array(
'order' => array(
'Comment.id' => 'desc'
)
);
}
$data = $this->paginate('Comment', $conditions);
}
?>

Model
function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$parameters = compact('conditions');
$this->recursive = $recursive;
$count = $this->find('count', array_merge($parameters, $extra));
if (isset($extra['group'])) {
$count = $this->getAffectedRows();
}
return $count;
}

View
$paginator->options(array('url' => $this->passedArgs));

Friday, September 3, 2010

Extracting Named Parameters in CakePHP

Extracting Named Parameters in CakePHP

In real php you would have collected the GET Parameters from a URL like this www.example.com/test.php?id=1 using the $_GET variable in this fashion $get_variable = $_GET['id']; You can also do the same in CakePHP. In CakePHP if you are passing parameters in the URL, then the URL would appear like this (also they are named) http://www.example.com/test/view/id:1 the controller like this



function view(id=null){

$get_variable = $this->params['named']['id'];
}

or

http://www.example.com/test/view/1/submit:yes

function view($id=null, $submit=null){
$get_variable_submit = $this->params['named']['submit'];
}

Saturday, July 10, 2010

Integrate WordPress Blog into CakePHP Application

Integrate WordPress Blog into CakePHP Application

INSTALL WORDPRESS IN CAKEPHP
1. Install wordpress under the webroot
==app\webroot\blog
2. added htacess in the root
==htaccess file in main CakePHP folder(root).

RewriteEngine on
RedirectMatch temp ^/blog/wp-admin$ http://www.example.com/blog/wp-admin/
RewriteRule blog$ /blog/ [L] RewriteRule blog/wp-admin$ /blog/wp-admin/ [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]


3. login wordpress admin
4. set up the the permalinks under Setting category menu then permalink
6. choose Custom Structure then enter this value
/%postname%/
7. above is the url blog
www.example.com/blog/Top-Firefox-Plugins-for-Web-Development

Thursday, May 27, 2010

How to define CakePHP admin routes.

How to define CakePHP admin routes.

find file
app\config\core.php

find below code
/**
* Uncomment the define below to use CakePHP admin routes.
*
* The value of the define determines the name of the route
* and its associated controller actions:
*
* 'admin' -> admin_index() and /admin/controller/index
* 'superuser' -> superuser_index() and /superuser/controller/index
*/
Configure::write('Routing.admin', 'admin');

Tuesday, May 25, 2010

cakephp - How remove the query results appears at the bottom

cakephp - How remove the query results appears at the bottom

find the file core.php

app\config\core.php


find above code


* CakePHP Debug Level:
*
* Production Mode:
* 0: No error messages, errors, or warnings shown. Flash messages redirect.
*
* Development Mode:
* 1: Errors and warnings shown, model caches refreshed, flash messages halted.
* 2: As in 1, but also with full debug messages and SQL output.
* 3: As in 2, but also with full controller dump.
*
* In production mode, flash messages redirect after a time interval.
* In development mode, you need to click the flash message to continue.
*/
//Configure::write('debug', 2);
Configure::write('debug', 0);

change the debug to 0 value



Sunday, May 16, 2010

PHP Frameworks

  • CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications. Using commonly known design patterns like MVC and ORM within the convention over configuration paradigm, CakePHP reduces development costs and helps developers write less code. (This site utilizes CakePHP framework)

    cakephp-framework

  • CodeIgniter is a powerful MVC framework. The official site is well documented and also has an active community of developers.

    codeigniter

  • ZendFramework (ZF) is more of a set of libraries than a MVC (model view controller) framework.


    zend-framework

  • Symfony is a full-stack framework, a library of cohesive classes written in PHP5.

    symfony-framework


Related Post