cakephp 学习4

 

1 验证用正则表达式
    <?php
class User extends AppModel
{
  var $name = 'User';

  var $validate = array(
    'username' => '/^.{6,40}$/',
    'password' => '/^.{6,40}$/',
    'email' => VALID_EMAIL
  );
}
?>
这个正则表达式的意思就是 “从字符串的开头开始,有至少 6 个但不超过 40 个字符,然后字符串结束”。
从左至右阅读这个正则表达式:

/ — 表示正则表达式的开头
^ — 表示字符串的开头
. — 表示任意一个字符
{6,40} — 表示至少 6 次,但不能超过 40 次
$ — 表示字符串结束
/ — 表示正则表达式结束

2 在数据进入数据库前,先判断是否重复输入
在model层中输入

function beforeValidate() {
    if (!$this->id) {
if ($this->findCount(array('User.username'
  => $this->data['User']['username'])) > 0) {
$this->invalidate('username_unique');
return false;
}
    }
    return true;
}
 并且在view层中给出信息
    echo $form->input('username', array('after' => $form->error
       ('username_unique', 'The username is taken. Please try again.')));

 

   这个方法告诉模型,在运行任何检验之前,应该检查提交的数据是否有 ID。如果没有 ID,就寻找用户名相同的其他用户。如果

找到这样的用户,就将用户名字段标为无效,并跳过其他检验(返回 false)。通过将 register.ctp 视图中的用户名输入行改为清

单 24 所示的代码,就可以启用这个功能。


2  用bake来生成框架
   注意必须建立好model层后再用!
 把cake\console以及php目录底下的php.exe所在的路径加到path中去。之后在app目录下运行
cake bake即可一步步去做control层,view层的crud.


3
  一对多的例子:
  authors,books表中,一对多的关系
author.php中
   <?php
class Author extends AppModel
{
 var $name = 'Author';
var $hasMany = array ('Book' => array(
'className' => 'Book','foreignKey'=>'author_id')
    );
  }
?>
book.php表
  <?php
class Book extends AppModel
{ var $name = 'Book';
 var $belongsTo = array ('Author' => array(
'className' => 'Author','foreignKey'=>'author_id')
    );
}
?>
然后假设authors表中已有数据
books_controller.php:
 <?php
class BooksController extends AppController {
var $name = 'Books';
var $scaffold;
}
  其中在录入book表时,应该会有authors的下拉框供选择。

 一对多中,一方如果和多个多方有关联,可以这样写:
  var $hasMany = array(
'Book' => array(
'className' => 'Book',
),
'Tutorial' => array(
'className' => 'Tutorial',
)
);

4 一对多的最终例子
   model层
   book.php:
     <?php
class Book extends AppModel
{ var $name = 'Book';
 var $belongsTo = array ('Author' => array(
'className' => 'Author','foreignKey'=>'author_id')
    );
}
?>
   author.php
    <?php
class Author extends AppModel
{
 var $name = 'Author';
var $hasMany = array ('Book' => array(
'className' => 'Book','foreignKey'=>'author_id')
    );
 
}
?>

  控制层 books_controller.php
     <?php
class BooksController extends AppController {
var $name = 'Books';
var $helpers = array('Form');
function index() {
$this->Book->recursive = 1;
$books = $this->Book->find('all',
array('fields' =>
array('Book.isbn','Book.title','Author.name')
)
);
$this->set('books',$books);
}
function add() {
if (!empty($this->data)) {
$this->Book->create();
$this->Book->save($this->data);
$this->redirect(array('action'=>'index'));
}
//注意在1.2中,不用helper类的generalist了。用find
$authors =$this->Book->Author->find('list',array('fields'=>'Author.name'));
$this->set('authors',$authors);
}
}
?>
    authors_controller.php
  <?php
class AuthorsController extends AppController {
var $name = 'Authors';
function index() {
$this->Author->recursive = 1;
$authors = $this->Author->find('all');
$this->set('authors', $authors);
}
}
?>
  view层
   views\authors\index.thtml
   <?php foreach($authors as $author): ?>
<h2><?php echo $author['Author']['name'] ?></h2>
<hr />
<h3>Book(s):</h3>
<ul>
<?php foreach($author['Book'] as $book): ?>
<li><?php echo $book['title'] ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>

    views\books\index.thtml
   <table>
<thead>
<th>ISBN</th><th>Title</th><th>Author</th>
</thead>
<?php foreach($books as $book): ?>
<tr>
<td><?php echo $book['Book']['isbn'] ?></td>
<td><?php echo $book['Book']['title'] ?></td>
<td><?php echo $book['Author']['name'] ?></td>
</tr>
<?php endforeach; ?>
</table>

  views\books\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_id');
?>
</fieldset>
<?php echo $form->end('Submit');?>


5 一对多中,删除一方时,如果要级联删除多方,这样设置
  var $hasMany = array(
'Book' => array(
'className' => 'Book',
'dependent'=> true
)
);

6 多对多,假设book和author关系为多对多
   model层
    book.php
   <?php
class Book extends AppModel
{
var $name = 'Book';
var $hasAndBelongsToMany = 'Author';
}
?>
    author.php
  <?php
class Author extends AppModel
{
var $name = 'Author';
var $hasAndBelongsToMany = 'Book';
}
?>

 controll层
  books_controller.php
    <?php
class BooksController extends AppController {
var $name = 'Books';
var $helpers = array( 'Form' );
function index() {
$this->Book->recursive = 1;
$books = $this->Book->find('all');
$this->set('books', $books);
}
function add() {
if (!empty($this->data)) {
$this->Book->create();
$this->Book->save($this->data);
$this->redirect(array('action'=>'index'), null, true);
}
$authors =$this->Book->Author->find('list',array('fields'=>'Author.name'));
$this->set('authors', $authors);
}
}
?>
   authors_controller.php
     <?php
class AuthorsController extends AppController {
var $name = 'Authors';
function index() {
$this->Author->recursive = 1;
$authors = $this->Author->find('all');
$this->set('authors', $authors);
}
}
?>

 view层
  view\book\index.thtml
   <?php foreach($books as $book): ?>
<h2><?php echo $book['Book']['title'] ?></h2>
<hr />
<h3>Author(s):</h3>
<ul>
<?php foreach($book['Author'] as $author): ?>
<li><?php echo $author['name'] ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>

   view\author\index.thtml
  <?php foreach($authors as $author): ?>
<h2><?php echo $author['Author']['name'] ?></h2>
<hr />
<h3>Book(s):</h3>
<ul>
<?php foreach($author['Book'] as $book): ?>
<li><?php echo $book['title'] ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>

  view\books\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');
?>
</fieldset>
<?php echo $form->end('Submit');?>

你可能感兴趣的:(cakephp)