开发者

Reduce the number of additional Queries to 0 by overriding functions in the base model

开发者 https://www.devze.com 2022-12-29 04:34 出处:网络
my basic database setup is: User:... Info: relations: User: { foreignType:one } When displaying information on the user it takes: 1 query to find info on the user, and 1 query to find additional in

my basic database setup is:

User:...  
Info:  
  relations:  
    User: { foreignType:one }

When displaying information on the user it takes: 1 query to find info on the user, and 1 query to find additional info

I want to reduce this to one query that finds both, I assume I need to override a function from BaseUser.class.php, or something along those lines but I'm not really sure wha开发者_Python百科t to do.

Thanks!


Assuming you're using Doctrine, you need to override the findOneBy*** methods in your InfoTable class to join data on retrieval

Class InfoTable extends Doctrine_Table {
   [...]
   public function findOneById($id) 
   {
        $q = $this->createQuery('i')
           ->leftJoin('i.User u')
           ->where('i.id = ?', $id);
        return $q->fetchOne();
    }
}

Doctrine will handle and hydrate the associated object, and save one query.

0

精彩评论

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