zethon
Member
I've been working my way through the Addon Tutorial and experimenting with my own code as well. One thing I am having problems with is how I would JOIN a table with a
For example, say I want to write a simple plugin that allows you to "comment" on a post (I know various plugins and functionality already exists for this kind of thing, this is just an exercise). So, I create a table that has a three columns:
For now, I'm not worried about how comments get into the table (I've just manually added data for the time being. My first question is: is the
Also, how would I load this information into my
Or something... I really have no idea. But before I can begin to figure out the template, I need to figure out how to access the data.
Any help would be appreciated.
XF:Post
. For example, say I want to write a simple plugin that allows you to "comment" on a post (I know various plugins and functionality already exists for this kind of thing, this is just an exercise). So, I create a table that has a three columns:
comment_id
, post_id
, comment
, where post_id
is the FK for the post. I've created a simple Entity for this, something like so:
Code:
<?php
namespace Demo\Portal\Entity;
use XF\Mvc\Entity\Structure;
class Comments extends \XF\Mvc\Entity\Entity
{
public static function getStructure(Structure $structure)
{
$structure->table = 'lulzapps_comments';
$structure->shortName = 'lulzapps\MyAddon:Comments';
$structure->primaryKey = 'comment_id';
$structure->columns =
[
'comment_id' => ['type' => self::UINT, 'required' => true, 'autoIncrement' => true],
'post_id' => ['type' => self::UINT, 'required' => true],
'comment' => ['type' => self::STR, 'required' => true, 'maxLength' => 255],
];
$structure->getters = [];
$structure->relations =
[
'Post' =>
[
'entity' => 'XF:Post',
'type' => self::TO_MANY,
'conditions' =>
[
['post_id', '=', '$post_id']
],
'primary' => true
],
];
return $structure;
}
}
For now, I'm not worried about how comments get into the table (I've just manually added data for the time being. My first question is: is the
self:TO_MANY
relationship correct here? Is this saying that a comment can belong to multiple posts (which I don't want)? Or is this saying that a post can have multiple comments (which I do want?)Also, how would I load this information into my
$post
object for use within my post_macros
template? I know I want to do something like:
Code:
<xf:foreach loop="$post.comments" key="$comment_id" value="?????">
<h1>{{$post.comments}.comment}</h1>
</xf:foreach>
Or something... I really have no idea. But before I can begin to figure out the template, I need to figure out how to access the data.
Any help would be appreciated.