1 資料庫表的設計要根據cakephp的規範.比如表名要以小寫+複數的形式,如books.如果是兩個單詞的話,要這樣.
author_name,用底線分開.
2 每個表必須有主鍵id
3 model的檔案名稱,必須是資料庫名去掉其複數形式,取其單數形式,比如book.而model的類名必須是大寫單數形式,比如Book.
4 腳手架:
<?php
class BooksController extends AppController {
var $name = 'Books';
var $scaffold;
}
?>
5 find用法
$books = $this->Book->find('all',
array(
'fields' => array(
'Book.isbn',
'Book.title',
'Book.author_name'
),
'order' => 'Book.title ASC'
)
);
$this->set('books', $books);
}
fields指明要用的欄位。
更複雜一點的用法和等效的sql
$count = $this->Book->find('count', array('conditions' =>
array('Book.title' => 'LIKE A%'));
等於:
SELECT COUNT(*) AS `count` FROM `books` AS `Book` WHERE
`Book`.`title` LIKE 'A%';
$book = $this->Book->find('first',
array(
'fields' => array('isbn', 'title'),
'order' => 'Book.id DESC'
)
);
等於SELECT `Book`.`isbn`, `Book`.`title` FROM `books` AS `Book`
WHERE 1 = 1 ORDER BY `Book`.`created` DESC LIMIT 1;
$books = $this->Book->find('all',
array(
'fields' => array('title'),
'conditions' => array(
'Book.author_name' => 'LIKE David Barnes'
),
'order' => 'Book.title ASC'
)
);
等於:
SELECT `Book`.`title` FROM `books` AS `Book` WHERE `Book`.`author_
name` LIKE 'David Barnes' ORDER BY
`Book`.`title` ASC
$this->Book->find('first', array(
'conditions' => array(
'Book.isbn' => '= 1234567890'
)
)
);
等於:
SELECT * FROM `books` AS `Book` WHERE `Book`.`isbn` = '1234567890';
$this->Book-> find('all', array(
'conditions' => array(
'Book.author_name' => array(
'Anupom Syam',
'Ahsanul Bari',
'David Barnes'
)
)
)
);
等於:
SELECT * FROM `books` AS `Book` WHERE Book.author_name IN (
'Anupom Syam', 'Ahsanul Bari', 'David Barnes'
)
$this->Book-> find('all', array(
'conditions' => array(
"or" =>
array
(
"Book.author_name" => "David Barnes",
"Book.title" => "%CAKEPHP%"
)
)
)
);
等於:
SELECT * FROM `books` AS `Book` WHERE Book.author_name LIKE (
'David Barnes' OR Book.title LIKE '%CakePHP%')
$this->Book->find('all', array(
'conditions' => array(
'or' => array(
array(
'Book.author_name'=> 'LIKE David Mercer',
'Book.title'=> 'LIKE %Drupal%'
),
array(
'Book.author_name'=> 'LIKE James Kennard',
'Book.title'=> 'LIKE %Joomla%'
)
)
)
)
);
等於:
SELECT * FROM `books` AS `Book` WHERE (
(Book.author_name LIKE 'David Mercer' AND Book.title LIKE
'%Drupal%') OR (Book.author_name LIKE 'James Kennard' AND Book.
title LIKE '%Joomla%')
)
$books = $this->Book->findAllByAuthorName('David Barnes', array('Book.title'),'Book.title ASC'));
這裡是用了findAllBy+欄位名,找出作者是david barnes的所寫的書的標題的集合,並且排序.
$this->Book->field('title', array('isbn' => '1904811299'));
這裡是找一條記錄了,找符合isbn所指示的條件的title.
6 儲存等的例子
<?php
class BooksController extends AppController {
var $name = 'Books';
var $helpers = array('Form' );
function index() {
$books = $this->Book->find('all',
array(
'fields' => array(
'Book.isbn',
'Book.title',
'Book.author_name'
),
'order' => 'Book.title ASC'
)
);
$this->set('books', $books);
}
function add() {
if (!empty($this->data)) {
$this->Book->create();
if(!!$this->Book->save($this->data)) {
$this->Session->setFlash('Book is Saved!', true);
$this->redirect(array('action'=>'index'));
}
}
}
}
?>
add.thtml:
<?php echo $form->create('Book');?>
<fieldset>
<legend>Add New Book</legend>
<?php
echo $form->input('isbn');
echo $form->input('title');
echo $form->input('description');
echo $form->input('author_name');
?>
</fieldset>
<?php echo $form->end('Submit');?>
<?php echo $form->create('Book');?>中,指示與model Book進行關聯.
編輯的功能:
function edit($id=null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash('Invalid Book', true);
$this->redirect(array('action'=>'index'));
}
if (empty($this->data)) {
$this->data = $this->Book->read(null, $id);
}
else {
$this->Book->create();
if(!!$this->Book->save($this->data)) {
$this->Session->setFlash('Book is Updated!', true);
$this->redirect(array('action'=>'index'), null, true);
}
}
}
其中留意$this->Book->read(null, $id);,第一個參數為null的時候,讀取所有欄位,第二個欄位就是條件欄位.
儲存單條記錄:
$this->Book->id = 9;
$this->Book->saveField('title','New Title');
就是把第id=9的記錄的title UPDATE掉了.
batch update:
$this->Book->updateAll(
array('Book.author_name' => "David Barnes"),
array('Book.starred' => 1)
);
把所有david的記錄中的book.starred欄位更新為1.
不用cakephp的,用自己的sql
function getCount()
{
return $this->query('SELECT count(*) FROM posts');
}
7 驗證規則:
在model中寫的
<?php
class Book extends AppModel
{
var $name = 'Book';
var $validate = array(
'title' => array(
'rule' => 'alphaNumeric'
),
'description' => 'alphaNumeric',
'author_name' => array(
'rule' => array('custom', '/[a-z]$/i'),
'message' => 'Only letters are allowed!'
),
'isbn' => array(
'isbn10' => array(
'rule' => array('custom', '/^[0-9]{9}[0-9xX]$/'),
'message' => 'Invalid ISBN!'
),
'unique' => array(
'rule' => 'isUnique',
'on' => 'create',
'message' => 'The ISBN is already added!'
)
)
);
function addStar($id) {
$this->id = $id;
$this->saveField('starred', 1);
}
}
?>
其中,規定'title' => array(
'rule' => 'alphaNumeric'
),
只能是數字,字母了.
'author_name' => array(
'rule' => array('custom', '/[a-z]$/i'),
'message' => 'Only letters are allowed!'
)
則是用Regex了.其中custome是固定的,因為表明是自訂.
自訂規則,比如:
<?php
class Book extends AppModel
{
var $name = 'Book';
var $validate = array(
'isbn' => array(
'rule' => array('isFirstTwoPrime')
)
);
function isFirstTwoPrime($data)
{
....
}