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

[eluser]developer10[/eluser]
Well, for the past 5-6 days I have been struggling to rewrite somewhat small amount of code of my current project to work with Doctrine.
Since Doctrine's documentation says little about integration with CI, I have several questions:

1. If I have a function/query in Doctrine model, can it be called from my controller like this:
Code:
$var = Doctrine::getTable('classFromMyModel');

2. If YES, would it be correct to say that the above is a real equivalent of:
Code:
$var = $this->modelName->functionName();

I need to get that class/model into $var variable and then call a function from that class, like this:
Code:
$categories = $var->get_categories();

When I want to do $var = Doctrine::getTable('classFromMyModel'); I end up with this Doctrine error:
Code:
Fatal error: Uncaught exception 'Doctrine_Table_Exception' with message 'Unknown method Doctrine_Table::get_categories' in C:\xampp\htdocs\uposlidoct\application\plugins\doctrine\lib\Doctrine\Table.php:2856

Stack trace:
#0 [internal function]: Doctrine_Table->__call('get_categories', Array)
#1 C:\xampp\htdocs\uposlidoct\application\controllers\kategorije.php(18): Doctrine_Table->get_categories()
#2 [internal function]: Kategorije->index()
#3 C:\xampp\htdocs\system\codeigniter\CodeIgniter.php(236): call_user_func_array(Array, Array)
#4 C:\xampp\htdocs\uposlidoct\index.php(115): require_once('C:\xampp\htdocs...')
#5 {main} thrown in C:\xampp\htdocs\uposlidoct\application\plugins\doctrine\lib\Doctrine\Table.php on line 2856
#2

[eluser]altrano[/eluser]
why not simply use
$categories = Doctrine::getTable('className')->get_categories(); ?
#3

[eluser]developer10[/eluser]
[quote author="altrano" date="1288517563"]why not simply use
$categories = Doctrine::getTable('className')->get_categories(); ?[/quote]

I tried, but it doesnt work! There's a great tutorial on Doctrine by Burak Guzel, and he uses it just like that, but i cant do so in my project.
I'm really stuck here, i dont want to have queries in my controllers so i'd like to move them to models.
#4

[eluser]kaejiavo[/eluser]
Seems you did not declare the method get_categories() in the class. I don't use the Table classes but DQL to fetch my objects, so i am not sure...

Marco
#5

[eluser]developer10[/eluser]
[quote author="mawi27" date="1288550583"]Seems you did not declare the method get_categories() in the class. I don't use the Table classes but DQL to fetch my objects, so i am not sure...

Marco[/quote]

how to do that?
here's my model code:
Code:
public function get_categories()
  {
    $result = Doctrine_Query::create()
      ->select('k.naziv, k.naziv_seo')
      ->addSelect('p.naziv, p.naziv_seo')
      ->addSelect('pro.id, COUNT(pro.id) as br_projekata')
      ->from('Kategorija k')
      ->leftJoin('k.Podkategorija p')
      ->leftJoin('p.Projekti pro')
      ->groupBy('p.id')
//     ->setHydrationMode(Doctrine::HYDRATE_ARRAY)
      ->execute();
      
    return $result;
  }

w/ or w/o hydrate_array line, it doesnt work!
#6

[eluser]kaejiavo[/eluser]
I use doctrine like this:
In my doctrine model the functions are declared
Code:
class Test extends BaseTest
{
    public function get_image()
    {
        if (!empty($this->image))
        {
            return $this->image;
        }
        else
        {
            return 'noimage.jpg';
        }
    }


}
In the controller i call the model using DQL
Code:
$q = Doctrine_Query::create()
                        ->from('Test t')
                        ->leftJoin('t.Type ty')
        $tests = $q->execute();

// then i can use the method as you asked:

$image = $tests->get_image();

I don't use the Doctrine::getTable() syntax, so i don't know if it should work the same way.
Marco
#7

[eluser]developer10[/eluser]
[quote author="mawi27" date="1288554864"]I use doctrine like this:
In my doctrine model the functions are declared
Code:
class Test extends BaseTest
{
    public function get_image()
    {
        if (!empty($this->image))
        {
            return $this->image;
        }
        else
        {
            return 'noimage.jpg';
        }
    }


}
In the controller i call the model using DQL
Code:
$q = Doctrine_Query::create()
                        ->from('Test t')
                        ->leftJoin('t.Type ty')
        $tests = $q->execute();

// then i can use the method as you asked:

$image = $tests->get_image();

I don't use the Doctrine::getTable() syntax, so i don't know if it should work the same way.
Marco[/quote]

but you still have a query in your controller. how to get rid of that?
#8

[eluser]Zorancho[/eluser]
try using this and you will see what's the error, cause your error says: Uncaught exception

<code>
try{
//place your code here
}
catch(Exception $e)
{
exit($e->getMessage());
}
</code>
#9

[eluser]developer10[/eluser]
[quote author="Zorancho" date="1288570702"]try using this and you will see what's the error, cause your error says: Uncaught exception

<code>
try{
//place your code here
}
catch(Exception $e)
{
exit($e->getMessage());
}
</code>[/quote]

well, isn't this a message on the error:
Code:
...with message 'Unknown method Doctrine_Table::get_categories' in C:\xampp\htdocs...

If i'll get the same, why bother?
#10

[eluser]kaejiavo[/eluser]
As i wrote earlier, the error message says your method get_categories is not defined in the class Doctrine_table.
If you are using doctrine class file generation, you can activate an option to generate table class files and then you would have to put your function into the correct table class file.

Marco




Theme © iAndrew 2016 - Forum software by © MyBB