关于fleaphp中的hasMany与manyToMany的用法
时间:2009-03-06 来源:xh_terry
hasMany也就是一对多的关系,比如一个用户可以有多个订单,官方的doc上只是简单说了一下用法,自带的blog实例看起来也有那么点费尽,还是有针对性的来说说吧
首先我们在数据库里见两个表
member,各字段分别为:
member_id
m_name //用户名
order_num //单数
orders
order_id
area //所在区域
phone //电话号码
member_id //外键
表建好之后,下面就可以开始写相应的model了
我的目录是
|--FLEA
|--Examples
|--Lab
|--_Shared
|--APP
|--Controller
|--Table
index.php
MandO.php
MandT.php
首先我们在数据库里见两个表
member,各字段分别为:
member_id
m_name //用户名
order_num //单数
orders
order_id
area //所在区域
phone //电话号码
member_id //外键
表建好之后,下面就可以开始写相应的model了
我的目录是
|--FLEA
|--Examples
|--Lab
|--_Shared
|--APP
|--Controller
|--Table
index.php
MandO.php
MandT.php
<?
FLEA::loadClass("FLEA_DB_TableDataGateway");
class Table_Orders extends FLEA_Db_TableDataGateway {
var $tableName = "orders";
var $primaryKey = "order_id";
}
?>
FLEA::loadClass("FLEA_DB_TableDataGateway");
class Table_Orders extends FLEA_Db_TableDataGateway {
var $tableName = "orders";
var $primaryKey = "order_id";
}
?>
<?
FLEA::loadClass("FLEA_DB_TableDataGateway");
class Table_Member extends FLEA_Db_TableDataGateway {
var $tableName = "member";
var $primaryKey = "member_id";
var $hasMany = array(
"tableClass" =>"Table_Orders",
"mappingName" =>"Orders",
"foreignKey" =>"member_id");
function create(& $row){
$this->_processTaste($row);
//dump($row);
return parent::create($row);
}
}
?>
model写好之后,再来完成control
FLEA::loadClass("FLEA_DB_TableDataGateway");
class Table_Member extends FLEA_Db_TableDataGateway {
var $tableName = "member";
var $primaryKey = "member_id";
var $hasMany = array(
"tableClass" =>"Table_Orders",
"mappingName" =>"Orders",
"foreignKey" =>"member_id");
function create(& $row){
$this->_processTaste($row);
//dump($row);
return parent::create($row);
}
}
?>
model写好之后,再来完成control
<?
class Controller_Default {
var $_MemberModel;
var $_TasteModel;
function Controller_Default(){
$this->_MemberModel = & FLEA::getSingleton("Table_Member");
$this->_TasteModel = & FLEA::getSingleton("Table_Taste");
}
function actionIndex1(){
require_once("MandO.php");
}
function actionCreateOrder(){
$order = & FLEA::getSingleton("Table_Orders");
$order->create($_POST);
$this->_MemberModel->incrField($_POST["member_id"],"order_num");
}
}
?>
最后是View,因为只是一个文件,所以为了方便我就放到了根目录下
class Controller_Default {
var $_MemberModel;
var $_TasteModel;
function Controller_Default(){
$this->_MemberModel = & FLEA::getSingleton("Table_Member");
$this->_TasteModel = & FLEA::getSingleton("Table_Taste");
}
function actionIndex1(){
require_once("MandO.php");
}
function actionCreateOrder(){
$order = & FLEA::getSingleton("Table_Orders");
$order->create($_POST);
$this->_MemberModel->incrField($_POST["member_id"],"order_num");
}
}
?>
最后是View,因为只是一个文件,所以为了方便我就放到了根目录下
<form action="<? echo url('default','CreateOrder'); ?>" method="POST">
用户ID:<input type="text" name="member_id"><br>
住址 :<input type="text" name="area"><br>
电话 :<input type="text" name="phone"><br>
<input type="submit" value="submit">
</form>
最后是index.php
用户ID:<input type="text" name="member_id"><br>
住址 :<input type="text" name="area"><br>
电话 :<input type="text" name="phone"><br>
<input type="submit" value="submit">
</form>
最后是index.php
<?
define("APP_DIR",dirname(__FILE__)."/APP");
define("NO_LEGACY_FLEAPHP",true);
$configFilename = "_Shared/DSN.php";
require_once("../../FLEA/FLEA.php");
FLEA::loadAppInf($configFilename);
FLEA::import(APP_DIR);
FLEA::runMVC();
?>
这样只要提交URL:http://127.0.0.1/FleaPHP/Example/Lab/index.php?controller=default&action=index1 就可以访问了
再来说说manyToMany,这个东西还着实卡了我半天,整整半天,最后发现是因为少了一个&,郁闷
首先在数据库里新建一个表:
taste,包含
taste_id
taste
然后创建一个新的model,Table_Taste
define("APP_DIR",dirname(__FILE__)."/APP");
define("NO_LEGACY_FLEAPHP",true);
$configFilename = "_Shared/DSN.php";
require_once("../../FLEA/FLEA.php");
FLEA::loadAppInf($configFilename);
FLEA::import(APP_DIR);
FLEA::runMVC();
?>
这样只要提交URL:http://127.0.0.1/FleaPHP/Example/Lab/index.php?controller=default&action=index1 就可以访问了
再来说说manyToMany,这个东西还着实卡了我半天,整整半天,最后发现是因为少了一个&,郁闷
首先在数据库里新建一个表:
taste,包含
taste_id
taste
然后创建一个新的model,Table_Taste
<?
FLEA::loadClass("FLEA_DB_TableDataGateway");
class Table_Taste extends FLEA_Db_TableDataGateway {
var $tableName = "taste";
var $primaryKey = "taste_id";
}
?>
修改Table_Member为
FLEA::loadClass("FLEA_DB_TableDataGateway");
class Table_Taste extends FLEA_Db_TableDataGateway {
var $tableName = "taste";
var $primaryKey = "taste_id";
}
?>
修改Table_Member为
<?
FLEA::loadClass("FLEA_DB_TableDataGateway");
class Table_Member extends FLEA_Db_TableDataGateway {
var $tableName = "member";
var $primaryKey = "member_id";
var $manyToMany = array(
"tableClass" => "Table_Taste",
"mappingName" => "Tastes",
"joinTable" =>"member_taste");
var $hasMany = array(
"tableClass" =>"Table_Orders",
"mappingName" =>"Orders",
"foreignKey" =>"member_id");
function _processTaste( & $row){ //当初就是死在了这个&上
$taste = & FLEA::getSingleTon("Table_Taste");
$getTaste = explode(",",$row["taste"]);
$arr = array();
foreach($getTaste as $t){
$tmp = array("taste" => $t);
$arr[] = $taste->create($tmp);
}
$row["Tastes"] = $arr;
}
function create(& $row){
$this->_processTaste($row);
//dump($row);
return parent::create($row);
}
}
?>
MandT.php
FLEA::loadClass("FLEA_DB_TableDataGateway");
class Table_Member extends FLEA_Db_TableDataGateway {
var $tableName = "member";
var $primaryKey = "member_id";
var $manyToMany = array(
"tableClass" => "Table_Taste",
"mappingName" => "Tastes",
"joinTable" =>"member_taste");
var $hasMany = array(
"tableClass" =>"Table_Orders",
"mappingName" =>"Orders",
"foreignKey" =>"member_id");
function _processTaste( & $row){ //当初就是死在了这个&上
$taste = & FLEA::getSingleTon("Table_Taste");
$getTaste = explode(",",$row["taste"]);
$arr = array();
foreach($getTaste as $t){
$tmp = array("taste" => $t);
$arr[] = $taste->create($tmp);
}
$row["Tastes"] = $arr;
}
function create(& $row){
$this->_processTaste($row);
//dump($row);
return parent::create($row);
}
}
?>
MandT.php
<form action="<? echo url('default','add'); ?>" method="POST">
用户名:<input type="text" name="m_name">
口味(用,号隔开) :<input type="text" name="taste">
<input type="submit" value="submit">
</form>
Cotroller下的Default.php改为
用户名:<input type="text" name="m_name">
口味(用,号隔开) :<input type="text" name="taste">
<input type="submit" value="submit">
</form>
Cotroller下的Default.php改为
<?
class Controller_Default {
var $_MemberModel;
var $_TasteModel;
function Controller_Default(){
$this->_MemberModel = & FLEA::getSingleton("Table_Member");
$this->_TasteModel = & FLEA::getSingleton("Table_Taste");
}
function actionIndex(){
require_once("MandT.php");
}
function actionIndex1(){
require_once("MandO.php");
}
function actionAdd(){
$this->_MemberModel->create($_POST);
}
function actionCreateOrder(){
$order = & FLEA::getSingleton("Table_Orders");
$order->create($_POST);
$this->_MemberModel->incrField($_POST["member_id"],"order_num");
}
}
?>
这样访问的时候访问:http://127.0.0.1/FleaPHP/Example/Lab/index.php就可以了
class Controller_Default {
var $_MemberModel;
var $_TasteModel;
function Controller_Default(){
$this->_MemberModel = & FLEA::getSingleton("Table_Member");
$this->_TasteModel = & FLEA::getSingleton("Table_Taste");
}
function actionIndex(){
require_once("MandT.php");
}
function actionIndex1(){
require_once("MandO.php");
}
function actionAdd(){
$this->_MemberModel->create($_POST);
}
function actionCreateOrder(){
$order = & FLEA::getSingleton("Table_Orders");
$order->create($_POST);
$this->_MemberModel->incrField($_POST["member_id"],"order_num");
}
}
?>
这样访问的时候访问:http://127.0.0.1/FleaPHP/Example/Lab/index.php就可以了
相关阅读 更多 +