has-and-belongs-to-many - 如何在没有 HABTM 的情况下使用 Lithium 编写标签云?

标签 has-and-belongs-to-many lithium

我对锂的关系有点模糊。我正在尝试使用 Lithium 创建标签云,但我不确定如何在不使用 HABTM 关系的情况下执行此操作。 顺便说一句,我正在使用 MySQL。

有什么建议吗?

:编辑添加示例代码:

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

<?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')
    ));
?>

如何更改代码,以便可以通过 $items 访问 Tags 数据

最佳答案

如果您使用 SQL,请查看:How do I perform joins with lithium models? 有关连接示例。

另一种方法可能是设计一个使用自己的 habtm 查询的“Join”模型。那里有一些 cakePHP 示例,它们应该是适应性强的,没有太多困惑。

或者,使用带有嵌入文档的 noSQL。

关于has-and-belongs-to-many - 如何在没有 HABTM 的情况下使用 Lithium 编写标签云?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9830194/

相关文章:

ruby-on-rails - Rails - 播种 HABTM 协会

php - Laravel belongsToMany 没有其中之一

ruby-on-rails-3 - Rails 不创建中间表? - Habtm 关系

ruby-on-rails-4 - 当 where 条件与 has_and_belongs_to_many 关联关联时,Rails Admin 会出错

php - 关于重构自定义路由场景的建议(Lithium Framework)

mongodb - Lithium mongodb 模型之间的关系

cakephp - 如何在 CakePHP 中将数据写入 HABTM 连接表的 HABTM 关联?

php - Li3 - 正确处理 MySQL 5.7 生成的列

php - lithium 弃用 __init() 方法,需要手动调用它

php - 在 Lithium session 中存储密码安全吗?