简化Doctrine查询

时间:2012-07-04 08:50:05

标签: mysql zend-framework doctrine-orm

我是Doctrine ORM或任何ORM的新手

$em = Zend_Registry::getInstance ()->entitymanager;         
$p = $em->createQuery ( "               
SELECT u                
FROM Teon_Model_User u              
WHERE u.app_auth IN (:app_auth)" );                     
$p->setParameter ( 'app_auth', $app_auth );         
$array = $p->getArrayResult();      
$customer_id = $array[0]['customer_id']; 


$p = $em->createQuery ( "               
SELECT p                
FROM Teon_Model_Purchase p              
WHERE p.customer IN (:customer_id)" );                  
$p->setParameter ( 'customer_id', $customer_id );       
$array = $p->getArrayResult();      
$purchase_id = $array[0]['id'];      

$p = $em->createQuery ( "               
SELECT pm               
FROM Teon_Model_PurchaseManual pm               
WHERE pm.purchase_id IN (:purchase_id)" );                  
$p->setParameter ( 'purchase_id', $purchase_id );       
$array = $p->getArrayResult();      
$m_id = $array[0]['manual_id'];     

你能简化这个查询吗,看起来很蠢,我在zend框架中使用Doctrine

此查询用于通过提供身份验证代码来验证用户是否在购买时使用了manual_id。

1 个答案:

答案 0 :(得分:1)

以下是我能想到的两种简单方法:

<?php

$em = Zend_Registry::getInstance()->entitymanager;

// 1st way
$user = $em->getRepository('Teon_Model_User')->findOneByAppAuth($app_auth);
$user->getCustomerId();

// 2nd way
$user = $em->getRepository('Teon_Model_User')->findOneBy(array(
    'app_auth' => $app_auth,
));
$user->getCustomerId();