cakephp学习3

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!'
)

  则是用正则表达式了.其中custome是固定的,因为表明是自定义.

自定义规则,比如:
   <?php
class Book extends AppModel
{
var $name = 'Book';
var $validate = array(
'isbn' => array(
'rule' => array('isFirstTwoPrime')
)
);
function isFirstTwoPrime($data)

{

 

....

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值