开发者

Symfony 1.4: How to limit results of the index action to only those of the logged in user

I apologize in advance for this newb question, but I've been struggling with the proper way to go about getting the correct result.

I have two tables:

sf_guard_user:
  Columns
    id
    name

recipe:
  Columns:
    id:
    user_id:
    name:
  relations:
    sf-guard_user: { local: user_id, foreign: id; foreign_alias: recipes }

Recipe module, indexSuccess: This form is where I want to limit my results to only the logged in user.

Here is my recipe actions.class.php file:

public function executeIndex(sfWebRequest $request)
{
$this->recipe = Doctrine_Core::getTable('recipe')
  ->createQuery('a')
  ->whereStatement: user_id = logged-in/posted user (this is where I'm struggling开发者_运维知识库 with the syntax... do I use a join statement? Or where statement?  ...I'm lost.  I can get the result I want in basic MySql, but not in DQL)
  ->execute();
}

Any help is much appreciated.


a shorter way would be:

public function executeIndex(sfWebRequest $request)
{
  $this->recipes = RecipeTable::getInstance()->findByUserId($this->getUser()->getId());
}


->where('user_id = ?', $this->getUser()->getGuardUser()->getId(););


With the help of both respondents I was able to get it working with an additional method in the myUser.class.php file. Here's how I got it working:

In the following file: project>>apps>>frontend>>lib>>myUser.class.php

I added this method:

public function getId()
{
  return $this->getGuardUser()->getId();
}

And in the following file: project>>apps>>frontend>>modules>>recipe>>actions>>actions.class.php

I revised the executeIndex action to:

public function executeIndex(sfWebRequest $request)
{
  $this->recipes = RecipeTable::getInstance()->findByUserId($this->getUser()->getId());
}

Thanks again to both respondents.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜