我对锂的关系有点模糊。我正在尝试使用 Lithium 创建标签云,但我不确定如何在不使用 HABTM 关系的情况下执行此操作。 顺便说一句,我正在使用 MySQL。
有什么建议吗?
:编辑添加示例代码:
这是我现在正在做的事情的一个非常简化的版本。
我有 Items
、Tags
和 ItemsTags
。
<?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/