Created
January 6, 2011 23:34
-
-
Save cainus/768849 to your computer and use it in GitHub Desktop.
To ORM or not to ORM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SQL Way | |
//======================================== | |
function getUnpublishedUsers(){ | |
$sql = 'SELECT md.fname AS firstname, md.lname AS lastname, md.email AS email_address, statistics.profile_complited AS percent_complete | |
FROM maestro_details AS md, statistics | |
WHERE statistics.user_id = md.user_id | |
AND md.user_id NOT IN | |
(SELECT user_id FROM maestro_detail_publisheds);'; | |
$result = $this->query($sql); | |
return $result; | |
} | |
//ORM Way | |
//==================================== | |
function getUnpublishedUserIDs(){ | |
$options = array(); | |
$options['joins'] = array( | |
array( | |
'table' => 'maestro_detail_publisheds', | |
'alias' => 'MaestroDetailPublished', | |
'type' => 'left', | |
'conditions' => array('MaestroDetailPublished.user_id = User.id') | |
) | |
); | |
$options['fields'] = array('User.id'); | |
$options['recursive'] = -1; | |
$options['conditions'] = array('MaestroDetailPublished.user_id' => null); | |
$rows = $this->find('all', $options); | |
$ids = array(); | |
foreach($rows as $row){ | |
array_push($ids, $row['User']['id']); | |
} | |
return $ids; | |
} | |
function getUnpublishedUsers(){ | |
$options = array(); | |
$userIDs = $this->getUnpublishedUserIDs(); | |
$options['joins'] = array( | |
array( 'table' => 'statistics', | |
'alias' => 'Statistic', | |
'type' => 'inner', | |
'conditions' => array('Statistic.user_id = MaestroDetail.user_id') | |
) | |
); | |
$options['fields'] = array('MaestroDetail.fname', 'MaestroDetail.lname', 'MaestroDetail.email', 'Statistic.profile_complited'); | |
$options['conditions'] = array('MaestroDetail.user_id' => $userIDs); | |
$options['recursive'] = -1; | |
$result = $this->MaestroDetail->find('all', $options); | |
return $result; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment