3

我对 Lithiums 的关系有点模糊。我正在尝试使用 Lithium 创建标签云,但我不确定如何在不使用 HABTM 关系的情况下做到这一点。我正在使用 MySQL,顺便说一句。

有什么建议么?

:编辑添加示例代码:

这是我现在正在做的事情的一个非常简化的版本。我有Items和。TagsItemsTags

<?php

namespace app\models;

class Tags extends \app\extensions\data\Model {

    public $hasMany   = array('ItemsTags');

    // {{{ schema
    protected $_schema = array(
        'id'       => array('type' => 'integer', 'key' => 'primary'),
        'title'    => array('type' => 'string'),
        'created'  => array('type' => 'integer'),
        'modified' => array('type' => 'integer')
    );
    // }}}
}

?>



<?php

namespace app\models;

class Items extends \app\extensions\data\Model {

    public $hasMany   = array('ItemsTags');

    // {{{ schema
    protected $_schema = array(
        'id'          => array('type' => 'integer', 'key' => 'primary'),
        'title'       => array('type' => 'string'),
        'sku'         => array('type' => 'string'),
        'price'       => array('type' => 'float'),
        'created'     => array('type' => 'integer'),
        'modified'    => array('type' => 'integer')
    );
    // }}}
}

?>



<?php

namespace app\models;

class ItemsTags extends \app\extensions\data\Model {

    public $belongsTo = array('Tags', 'Items');

    // {{{ schema
    protected $_schema = array(
        'id'       => array('type' => 'integer', 'key' => 'primary'),
        'tag_id'   => array('type' => 'integer'),
        'item_id'  => array('type' => 'integer'),
        'created'  => array('type' => 'integer'),
        'modified' => array('type' => 'integer')
    );
    // }}}
}
?>



<?php
    $items = Items::find('first', array(
        'conditions' => array('myField' => 'myCondition')
    ));
?>

如何更改我的代码以便我可以Tags通过以下方式访问数据$items

4

1 回答 1

1

如果您使用 SQL,请查看:如何使用锂模型执行连接? 用于连接示例。

另一种方法可能是设计一个使用自己的 habtm 查询的“加入”模型。那里有一些 cakePHP 示例,它们应该可以适应并没有太多麻烦。

或者,将 noSQL 与嵌入式文档一起使用。

于 2012-03-23T12:57:50.663 回答