douzhong1907 2011-10-28 20:45
浏览 66
已采纳

PHP ORM和优化的关系查询

I need a PHP ORM to work with relations well. Please consider code below in Zend:

$persons = new Persons();
$person = $persons->find(5)->current();
echo 'Name: '.$person->fullname;

$phones = $person->findDependentRowset('Phones');
foreach($phones as $phone)
    echo 'Phone: '.$phone->phonenumber; 

Or code below in xPDO:

$person = $xpdo->getObject('Persons', 5);
echo 'Name: '.$person->get('fullname');

$phones = $person->getMany('Phones');
foreach($phones as $phone)
    echo 'Phone: '.$phone->get('phonenumber');

in both scripts, ORMs executes two queries as below:

SELECT * FROM persons WHERE id=5;
SELECT * FROM phones WHERE person=5;

It means one query for main object and one query for each relation but what i need is using ONE query for main object and its relations! xPDO can do it as below:

$person = $xpdo->getObjectGraph('Persons', '{"Phones":{}}', 5);
echo 'Name: '.$person->get('fullname');

foreach($person->Phones as $phone)
    echo 'Phone: '.$phone->get('phonenumber');

which executes this query:

SELECT * FROM persons
LEFT JOIN phones ON phones.person=persons.id
WHERE persons.id=5

This is great but its not possible to set fields to get from tables! it means in this case xPDO use "SELECT * " so if i get an object and its 4 relations, i will get all fields of all these tables!

So i need an ORM to execute query below for example above:

SELECT persons.fullname , phones.phonenumber FROM persons
LEFT JOIN phones ON phones.person=persons.id
WHERE persons.id=5

Doctrine can do it via DQL but i think Doctrine is not good for personal projects. Is there any PHP ORM to do this?

Thanks AHHP

  • 写回答

3条回答 默认 最新

  • douhunbei0166 2013-05-18 23:30
    关注

    xPDO can do it. You just have to adjust your thinking and your request.

    Remember the objectGraph utilizes the class name of the object, where as the relationships in the graph are used in the graph and query.

     $criteria = $this->xpdo->newQuery('prefixClient');
                if (!empty($limit))
                    $criteria->limit($limit);
                $criteria->where(array('Child.planid' => $this->getPrimaryKey(),
                                       'Child.active' => true,
                                       'GrandChild.someAttribute' => true,
                                       'GreatGrandChild.someOtherAttribute' => true,
                                       'suspended' => false
                                 ));
    
                $out = $this->xpdo->getCollectionGraph('prefixClient', '{"Child":{"GrandChild":{"GreatGrandChild":{}}}}', $criteria);
    

    You set the WHERE on any aspect of the relation, including the current object as show in the suspended => false line.

    My book might help you a little in establishing your schema and relations. Objects should always be singular in nomenclature, whereas their relations can be plural (1:M) or singular (1:1).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)