Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 原则:如何操纵收藏?_Php_Symfony1_Doctrine_Doctrine 1.2 - Fatal编程技术网

Php 原则:如何操纵收藏?

Php 原则:如何操纵收藏?,php,symfony1,doctrine,doctrine-1.2,Php,Symfony1,Doctrine,Doctrine 1.2,使用symfony&&doctor 1.2,我尝试为用户显示排名最高的网站 我做到了: public function executeShow(sfWebRequest $request) { $this->user = $this->getRoute()->getObject(); $this->websites = $this->user->Websites; } 唯一的问题是,它返回一个包含所有网站的条令集合,而不仅仅是排名

使用symfony&&doctor 1.2,我尝试为用户显示排名最高的网站

我做到了:

 public function executeShow(sfWebRequest $request)
  {
    $this->user = $this->getRoute()->getObject();
    $this->websites = $this->user->Websites; 
  }
唯一的问题是,它返回一个包含所有网站的条令集合,而不仅仅是排名靠前的网站

我已经设置了一个GetToRanked方法,但如果设置了,则:

$this->user->Websites->getTopRanked()
它失败了

如果有人想改变条令集合,只过滤排名靠前的人

谢谢

PS:我的方法在websiteTable.class.php中类似:

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

}

如果GetToRanked是您的用户模型中的一个方法,那么您可以使用$this->user->GetToRanked来访问它。如果GetToRanked是您的用户模型中的一个方法,那么您可以使用$this->user->GetToRanked来访问它。在您的例子中,$this->user->Websites包含所有用户网站。据我所知,除非您遍历现有的条令集合并选择感兴趣的元素,否则无法过滤现有的条令集合

我只需在用户类中实现GetToPankedWebsites方法:

class User extends BaseUser
{
  public function getTopRankedWebsites()
  {
    WebsiteTable::getTopRankedByUserId($this->getId());
  }
}
并在网站表中添加相应的查询:

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();
  }
}

在您的例子中,$this->user->Websites包含所有用户网站。据我所知,除非您遍历现有的条令集合并选择感兴趣的元素,否则无法过滤现有的条令集合

我只需在用户类中实现GetToPankedWebsites方法:

class User extends BaseUser
{
  public function getTopRankedWebsites()
  {
    WebsiteTable::getTopRankedByUserId($this->getId());
  }
}
并在网站表中添加相应的查询:

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();
  }
}

我宁愿在方法之间传递查询:

//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
}

我宁愿在方法之间传递查询:

//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
}
您还可以使用getFirst函数

您还可以使用getFirst函数


您将GetToRanked放在了哪里?它看起来怎么样?请参阅我的编辑,我已添加了此功能:-您正在读取所有网站的值,而不是属于某个用户的所有网站的值。虽然您从用户对象中调用它,但您的查询不使用用户值。。。我认为Doctrine不会自行添加所需的连接。$this->user->x方法就是您的用户模型中的方法。您将此函数放置在您的websiteTable模型中,这是完全不同的。您应该在网站模型中创建一个函数,该函数接受用户ID,例如toprankedbyuser$user,或者,如果您想像上面那样访问它,请在用户模型中创建一个函数。您将GetToRanked放在何处,它看起来如何?请参阅我的编辑,我已经添加了该函数:-您正在读取所有网站的值,而不是属于某个用户的所有网站的值。虽然您从用户对象中调用它,但您的查询不使用用户值。。。我认为Doctrine不会自行添加所需的连接。$this->user->x方法就是您的用户模型中的方法。您将此函数放置在您的websiteTable模型中,这是完全不同的。您应该在网站模型中创建一个函数,该函数接受用户ID,例如toprankedbyuser$user,或者,如果您想像上面那样访问它,在用户模型中创建一个函数。在过滤之前,我需要为使用别名网站的用户检索网站否?在过滤之前,我需要为使用别名网站的用户检索网站否?谢谢。我认为有可能改变一套教义。谢谢。我认为有可能改变一套教义。