Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter + Doctrine = Me Going Crazy
#11

[eluser]DigitalSkye[/eluser]
extend the doctrine_table class (name must match the doctrine_record). Put your custom functions in the doctrine_table.

user.php
Code:
Class User extends Doctrine_Record {
/* table definition code here */
}

user_table.php
Code:
Class UserTable extends Doctrine_Table {

function get_something()
{
  /* run query, return result object, array, scalar -- depending on what you are doing with it */
}
}

in your controller
Code:
$something = UserTable::get_something();

Edit: This allows you to keep your controllers thin and you can call the doctrine_table function from anywhere in your app.

http://www.doctrine-project.org/projects...w/en#table
#12

[eluser]developer10[/eluser]
[quote author="DigitalSkye" date="1288766076"]extend the doctrine_table class (name must match the doctrine_record). Put your custom functions in the doctrine_table.

user.php
Code:
Class User extends Doctrine_Record {
/* table definition code here */
}

user_table.php
Code:
Class UserTable extends Doctrine_Table {

function get_something()
{
  /* run query, return result object, array, scalar -- depending on what you are doing with it */
}
}

in your controller
Code:
$something = UserTable::get_something();

Edit: This allows you to keep your controllers thin and you can call the doctrine_table function from anywhere in your app.

http://www.doctrine-project.org/projects...w/en#table[/quote]

This helped, thanks!
But I'm still confused by the fact one of our members (Burak Guzel) managed to do the same thing without creating additional model file (UserTable.php or user_table.php as you suggested). Instead, he writes custom functions right below the setTableDefinition() and setUp() functions?!
It all can be seen in his great tutorial on CI+Doctrine: http://www.phpandstuff.com/articles/code...-and-setup

Ato some point (day 11 of the tutorial) he creates this function (and calls it from his controller):
Code:
<?php
class Forum extends Doctrine_Record {

// ...

    public function getThreadsArray($offset, $limit) {

        $threads = Doctrine_Query::create()
            ->select('t.title')
            ->addSelect('p.id, (COUNT(p.id) - 1) as num_replies')
            ->addSelect('MAX(p.created_at) as last_post_date')
            ->addSelect('fp.created_at, u.username')
            ->from('Thread t, t.Posts p, t.First_Post fp, fp.User u')
            ->where('t.forum_id = ?', $this->id)
            ->groupBy('t.id')
            ->orderBy('last_post_date DESC')
            ->limit($limit)
            ->offset($offset)
            ->setHydrationMode(Doctrine::HYDRATE_ARRAY)
            ->execute();

        foreach ($threads as &$thread) {

            $thread['num_replies'] = $thread['Posts'][0]['num_replies'];
            $thread['created_at'] = $thread['First_Post']['created_at'];
            $thread['username'] = $thread['First_Post']['User']['username'];
            $thread['user_id'] = $thread['First_Post']['User']['id'];

            unset($thread['Posts']);

        }

        return $threads;

    }

}

So, he actually did not create ForumTable.php to put this custom function into it, but still he is able to call it from in his controller in this fashion:

Code:
$forums = Doctrine::getTable('Forum');
$someVar = $forums->getThreadsArray();

Maybe I am missiing something, but it seems to me IF there actually is a way of having custom functions inside user.php file (rather than creating UserTable.php) - i'd be happy to learn how to do it.
#13

[eluser]DigitalSkye[/eluser]
Quote:$forums = Doctrine::getTable('Forum');
$someVar = $forums->getThreadsArray();

I have not tested, since I follow Doctrine doctrine Smile -- strictly speaking, this is exactly what the *_Table Peer is there for -- but I would try:
Code:
$some_var = Forum::getThreadsArray($offset, $limit);

!No camelCase variables, kind sir :bug: CI folks dig on the _underscore_ as a separator.



As far as I know, simply retrieving the table definition doesn't give you access to custom methods that you have defined. I could be wrong.

Please post back and let us know how it goes.

Skye

** EDIT: why load the dbms table when all you want is a (I believe misplaced) method within the classfile?

I have also enjoyed Burak's outstanding tutorials. For the (understandable)sake of brevity, he sometimes puts query logic in Controllers. As I recall, he mentions doing so to make the tut easier to follow.

Cheers,

Skye




Theme © iAndrew 2016 - Forum software by © MyBB