0
votes

In my symfony 1.4 application i'm using doctrine data models.I'm new to symfony and doctrine.I generated doctrine models from command-line after defining database table information in the schema.yml file.Those generated successfully.Then i created a custom function inside Table.class.php file.Following is that.

class Table extends BaseTable
{
 public function getuname()
 {
    $user=new Table();
    $uname=$user->getUsername();
    return $uname;
 }

}

I want to know how to call this inside the controller ? I called it normal MVC application's way.But i don't know whether it's correct in symfony.In symfony 1.4 manual also i couldn't find a proper way to do this.

This is my controller.

class loginActions extends sfActions
{

  public function executeIndex(sfWebRequest $request)
  {
     $this->userdata = User::getuname();
  }

}

Then i tried to print this inside view.

<?php 
   echo $userdata;
?>

But view is showing an empty page.

Update with exception details--------------------------------

stack trace at () in SF_SYMFONY_LIB_DIR\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Connection.php line 1082 ...

    $message .= sprintf('. Failing Query: "%s"', $query);

    }


    $exc  = new $name($message, (int) $e->getCode());

    if ( ! isset($e->errorInfo) || ! is_array($e->errorInfo)) {

        $e->errorInfo = array(null, null, null, null);

    }
1
You should read about table and model classes: symfony.com/legacy/doc/gentle-introduction/1_4/en/… There are examples how to use them. And you can find some help in the doctrine docs as well docs.doctrine-project.org/projects/doctrine1/en/latest/en/…1ed
Thanks for the reply.Yes actually i followed the symfony document that you have posted.But my problem is as i can see there i can't find a way to call the model object inside the symfony controller.But anyhow again i'll go through the doc thoroughly.CodeCanyon

1 Answers

0
votes

When using Doctrine you retrieve objects from the database using the ...Table classes (in your case it will be a TableTable class. You can use its' methods to fetch objects from DB (e.g. find($id)) and then access them. So in your case your classes should something like this:

class Table extends BaseTable
{
    public function getuname()
    {
       return $this->getUsername();
    }
}

Now it effectively becomes just an alias of getUsername().

Then in your action:

class loginActions extends sfActions
{

    public function executeIndex(sfWebRequest $request)
    {
        $user = Doctrine_Core::getTable('Table')->find(123);
        $this->userdata = $user->getuname();
    }
}

This will print the username in your template (assuming of course that you have a user with id 123).