3

I'm a little foggy with Lithiums relationships. I'm trying to create a tag cloud using Lithium but I'm not sure how to do this without using HABTM relationships. I'm using MySQL, btw.

Any suggestions?

:edited to add sample code:

Here is a very simplified version of something I'm working on right now. I have Items, Tags and 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')
    ));
?>

How can I change my code so that I can access Tags data via $items

Housni
  • 963
  • 1
  • 10
  • 23

1 Answers1

1

If you using SQL take a look into: How do I perform joins with lithium models? for join examples.

Another way might be designing a "Join"Model which is using an own habtm query. There are some cakePHP Examples out there which should be adaptable with not much hazzle.

Or, using noSQL with embedded Documents.

Community
  • 1
  • 1
dgAlien
  • 428
  • 1
  • 4
  • 9
  • Thanks for the response. I'm using SQL. I should have mentioned that. I'll edit the original post. I know how to use find() and invoke joins. I'm just not sure how to implement HABTM within models. For example, I wouldn't know how to use hasOne, hasMany, belongsTo, etc in order to simulate HABTM. I'll Google and look for the way Cake does it. That might help a bit. – Housni Mar 23 '12 at 13:13
  • You could create three models => two "normal" ones and one n to m "Model": Users, UsersToCities, Cities. then relate them as usual. – dgAlien Mar 23 '12 at 13:20
  • But how would I get the Cities data while iterating through the Users? – Housni Mar 23 '12 at 21:24