Cakephp关系模式

时间:2013-09-17 05:18:26

标签: cakephp

public $belongsTo = array(
    'Hospital' => array(
        'className' => 'Hospital',
        'foreignKey' => 'hospital_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);


public $hasMany = array(
    'Floor' => array(
        'className' => 'Floor',
        'foreignKey' => 'hospital_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

我有两张医院和楼层的桌子,我从医院病床台检索数据,并在楼层视图中显示添加楼层。

2 个答案:

答案 0 :(得分:0)

您已经设置了关系,所以现在您只需要创建一个控制器操作和一个视图。

假设您的表已就位,最简单的方法就是在Hospital模型上运行cake bake all,Cake将为您生成视图。 Cake足够聪明,可以生成一个视图,如果你想要的话,它会显示一个相关楼层的列表(你的帖子中不清楚)。

答案 1 :(得分:-1)

您可以像这样从数据库中检索数据......

mysql_select_db("users"); //your database name

$sql = mysql_query("SELECT * FROM langs"); //table name

$limit = 4;
$count = 3;

echo "<table border='1'>";
while($row = mysql_fetch_array($sql)){
$name=$row['name'];
$email=$row['email'];
$contact=$row['contact'];
if($count < $limit){
echo "<tr>";
}

............. //you can put in table and in any other field
?>