【问题标题】:cakephp: index site with all related habtm datacakephp:包含所有相关 habtm 数据的索引站点
【发布时间】:2012-06-20 15:25:19
【问题描述】:

我尝试了 3 天后的解决方案以使其正常工作,但我需要帮助。主要是我做了尝试和错误。如果有人能给我一个解决方案,我可以对其进行逆向工程并更好地理解 cakephp。

我想要的是这样的索引视图(制作图形): http://www.bilder-upload.eu/show.php?file=fd8e93-1340110385.png

门和钥匙有一个 habtm 关系,我想列出属于 1 个钥匙的所有门

我有 3 张桌子:

门(ID,标题,txt) 键(ID、编号、描述) door_keys(door_id,key_id)

这是我的 key.php

<?php

class Key extends AppModel {
    public $hasAndBelongsToMany = array ('Door');
    public $displayField = 'description';
    // containable
    var $actsAs = array('Containable');
}

door.php

<?php



class Door extends AppModel {
    public $name = 'Door';
    public $belongsTo = array('Building');
    //public $actsAs = array('Containable');
    public $hasAndBelongsToMany = array('Key');
    // validation array wird aufgemacht
    public $validate = array(
    // validation wird für das Datenbankfeld title definiert
        'title' => array(
        // Feld darf nicht leer sein
            'rule' => 'notEmpty' ,
            // Eintrag muss ausgewählt werden
            'required' => true
        ),
        // validation für das DB Feld building_id
        'building_id' => array(
            'rule' => 'notEmpty' ,
            'required' => true
        ),
    );
    // viele türen sind in einem Gebäude-> n:1->$belongsTo
    // in der tabelle doors muss ein fremdschluessel auf die andere datenbank angelegt sein


}

keyscontroller.php(不工作)

class KeysController extends AppController {


public function index() {
    $this->paginate = array(
      'contain' => array(
        // we want to get the key
        'Door' => array(
       // the info i wanna know is in the field txt
          'fields' => array('txt')

        ),

          ),
        );
        $keys = $this->paginate('Key');
    debug($this->set(compact('keys')));
}

来自键的index.ctp

<h2>Schluessel selbstgemacht</h2>

<table>
<!-- tr definiert eine Tabellenzeile -->
    <tr>
        <!-- th Überschriften in der Zeile -->
        <th>ID</th>
        <th>Description</th>
        <th>number</th>
    </tr>


<?php foreach($keys as $key): ?>
    <tr>
        <td><?php echo $key['Key']['id'] ?></td>
        <td><?php echo $key['Key']['description'] ?></td>
        <td><?php echo $key['Key']['number'] ?></td>
        <td><?php echo $key['Door']['txt'] ?></td>

        <td>
            <?php echo $this->Html->link('Details', '/keys/view/' . $key['Key']['id']) ?>
            <?php echo $this->Html->link('bearbeiten', '/keys/edit/' . $key['Key']['id']) ?>
            <?php echo $this->Html->link('loeschen', '/keys/delete/' . $key['Key']['id']) ?>
        </td>
    </tr>
<?php endforeach; ?>
</table>

我想这是一个基本问题,但我找不到解决方案。所以我需要一些帮助。谢谢

【问题讨论】:

  • 我不知道如何编辑我的帖子。所以上面的例子是错误的,请忽略下面的,因为它显示错误:door1----key1 ----key2 door2 ---key1 door3----key4
  • 仍然需要帮助。看看这个帖子下面。
  • 要编辑您的问题,在此评论部分的正上方,您应该会看到您选择的标签 cakephp。正下方应该是编辑链接。

标签: cakephp


【解决方案1】:

'recursive' =&gt; 1 添加到您的 Door->find() 以获取相关键的数据:

class KeysController extends AppController {
    public function index() {

    $doors = $this->Door->find(
        'recursive' => 1
    );
    $this->set('doors', $doors);

    // Look at the result
    debug($doors);
}

但是,如果您的模型关系很复杂,使用 recursive 会变得非常混乱。建议在您的模型上使用 ContainableBehavior

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-05
    • 2012-11-29
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    相关资源
    最近更新 更多