Doctrina: ¿cómo manipular una colección?

With symfony && doctrine 1.2 in an action, i try to display the top ranked website for a user.

Yo hice :

 public function executeShow(sfWebRequest $request)
  {
    $this->user = $this->getRoute()->getObject();
    $this->websites = $this->user->Websites; 
  }

The only problem is that it returns a Doctrine collection with all the websites in it and not only the Top ranked ones.

I already setup a method (getTopRanked()) but if I do :

$this->user->Websites->getTopRanked()

Fracasa.

If anyone has an idea to alter the Doctrine collection to filter only the top ranked.

Muchas Gracias

PS: my method looks like (in websiteTable.class.php) :

   public function getTopRanked()
{
  $q = Doctrine_Query::create()
       ->from('Website')
      ->orderBy('nb_votes DESC')
       ->limit(5);
  return $q->execute();

}

preguntado el 08 de enero de 11 a las 23:01

Where did you put getTopRanked() and how does it look like? -

See my edit, i've added the function. :-) -

You are reading the values for all websites, not all websites that belong to a certain user. ALtough you call it from within a user object your query doesn't use a user value... I don't think Doctrine will add the required JOINs on its own. -

the $this->user->x() methods are the ones in your user model. you placed this function in your websiteTable model, which is entirely different. You should either create a function in the website model that takes in a userid (eg toprankedbyuser($user) ) or, if you want to access it like you do above, create a function in the user model. -

4 Respuestas

I'd rather pass Doctrine_Query between methods:

//action
public function executeShow(sfWebRequest $request)   
{
   $this->user = $this->getRoute()->getObject();
   $this->websites = $this->getUser()->getWebsites(true);  
}

//user
  public function getWebsites($top_ranked = false)
  {
    $q = Doctrine_Query::create()
     ->from('Website w')
     ->where('w.user_id = ?', $this->getId());
    if ($top_ranked)
    {
      $q = Doctrine::getTable('Website')->addTopRankedQuery($q);
    }
    return $q->execute();
  }

//WebsiteTable
public function addTopRankedQuery(Doctrine_Query $q)
{
  $alias = $q->getRootAlias();
  $q->orderBy($alias'.nb_votes DESC')
    ->limit(5)
  return $q
}

Respondido el 10 de enero de 11 a las 18:01

Thanks. I thought it was possible to alter a doctrine collection. - sf_tristanb

If getTopRanked() is a method in your user model, then you would access it with $this->user->getTopRanked()

Respondido el 09 de enero de 11 a las 08:01

Well i need to retrieve the website for a user with the alias "Websites" before filtering it no ? - sf_tristanb

In your case $this->user->Websites contains ALL user websites. As far as I know there's no way to filter existing doctrine collection (unless you will iterate through it and choose interesting elements).

I'd simply implement getTopRankedWebsites() method in the User class:

class User extends BaseUser
{
  public function getTopRankedWebsites()
  {
    WebsiteTable::getTopRankedByUserId($this->getId());
  }
}

And add appropriate query in the WebsiteTable:

class WebsiteTable extends Doctrine_Table
{
  public function getTopRankedByUserId($userId)
  {
    return Doctrine_Query::create()
     ->from('Website w')
     ->where('w.user_id = ?', array($userId))
     ->orderBy('w.nb_votes DESC')
     ->limit(5)
     ->execute();
  }
}

Respondido el 09 de enero de 11 a las 21:01

También puedes utilizar la getFirst() función

$this->user->Websites->getTopRanked()->getFirst()

http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_collection.html#getFirst()

respondido 30 mar '11, 21:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.