Welcome Guest, Not a member yet? Register   Sign In
Please help with active record + insert with object
#1

[eluser]walrus_lt[/eluser]
Hello, i developed my own forum model, with functions that creates, inserts new topics, categories, forums, messages and so on. I dont know whatever i made it "as best as it can be". So i want to please you to share suggestions what i need to edit to improve my class and usage of it. My forum model class is:

Code:
<?php

  class Forum_Model extends Model
  {
    var $category;
    var $forum;
    var $topic;
    var $message;

    var $forum_id;
    var $topic_id;

    function Forum_Model()
    {
      parent::Model();

      $this -> category = & new _Category;
      $this -> forum    = & new _Forum;
      $this -> topic    = & new _Topic;
      $this -> message  = & new _Message;
    }

    function get_categories()
    {
      $this -> db -> select('category_id AS id, category_name AS name')
                  -> from('forum_categories')
                  -> order_by('category_order', 'ASC')
                  -> get();
    }

    function get_forums()
    {
      $this -> db -> select('forum_id AS id, forum_name AS name, forum_topics AS topics, forum_messages AS messages')
                  -> from('forum_forums')
                  -> where('forum_category', $this->category)
                  -> order_by('forum_order', 'asc')
                  -> get();
    }

    function get_topics()
    {
      $this -> db -> select('topic_id AS id, topic_name AS name, topic_author AS author, topic_messages AS messages')
                  -> from('forum_topics')
                  -> where('forum_id', $this->forum_id)
                  -> order_by('forum_last_message', 'asc')
                  -> get();
    }

    function get_messages()
    {
      $this -> db -> select('message_id AS id, message_title AS title, message_text AS text, message_author AS author, message_timestamp AS timestamp')
                  -> from('forum_messages')
                  -> where('topic_id', $this->topic_id)
                  -> order_by('message_id', 'asc')
                  -> get();
    }

    function create_category()
    {
      $this -> db -> insert('forum_categories', $this -> category);
    }

    function create_forum()
    {
      $this -> db -> insert('forum_forums', $this -> forum);
    }

    function post_topic()
    {
      $this -> db -> insert('forum_topics', $this -> topic);
    }

    function post_mesage()
    {
      $this -> db -> insert('forum_messages', $this -> message);
    }
  }

  class _Category
  {
    var $category_order;
    var $category_name;
  }

  class _Forum
  {
    var $forum_category;
    var $forum_name;
    var $forum_order;
  }

  class _Topic
  {
    var $forum_id;
    var $topic_title;
    var $topic_author;
  }

  class _Message
  {
    var $topic_id;
    var $message_title;
    var $message_text;
    var $message_author;
  }

?>

Usage of it is (With create_category() example):

Code:
<?php

  class Forum extends Controller
  {
    function Forum()
    {
      parent::Controller();
      $this->load->model('forum_model');
    }

    /** ** ** ** ** ** ** **
     * Index Page
     ** ** ** ** ** ** ** **/
    function index()
    {
      $this -> forum_model -> category -> category_order = 0;
      $this -> forum_model -> category -> category_name  = 'Gera Kategorija';
      $this -> forum_model -> create_category();
    }
  }

?>

I would apprecate any help from anyone, i want make it best Smile
#2

[eluser]TheFuzzy0ne[/eluser]
You'll need to support pagination, and limit the data sets returned by your models. You'll need a way to grab a breadcrumb efficiently without too many unnecessary queries, and you will need to decide on a structure for your forum categories. You may want to integrate an ACL and perhaps some kind of spam filter. You may also want to implement search functionality, so you'll need to encompass that into your design.

Also, your query results are not currently being assigned to anything.

Other than that, it looks good so far.
#3

[eluser]walrus_lt[/eluser]
[quote author="TheFuzzy0ne" date="1242506285"]You'll need to support pagination, and limit the data sets returned by your models. You'll need a way to grab a breadcrumb efficiently without too many unnecessary queries, and you will need to decide on a structure for your forum categories. You may want to integrate an ACL and perhaps some kind of spam filter. You may also want to implement search functionality, so you'll need to encompass that into your design.

Also, your query results are not currently being assigned to anything.

Other than that, it looks good so far.[/quote]

I was writen "return" before $this so it returned data. But i thougth i can do something like this:
Code:
$this->forum_model->some();
foreach ( $this->db->result() as ... )

Instead of:
Code:
$query = $this->forum_model->some();
foreach ( $query->result() as ... )

But i did'nt figure that out Smile




Theme © iAndrew 2016 - Forum software by © MyBB