fire-eyed-boy wrote:
>
>
> <snip>
>
> My design issue is the following:
>
> Imagine that sometimes I only need some basic info about the User with
> perhaps one (main profile) image. For instance for some search result
> functionality.
>
> The other time around I might only need all images. For
> instance when you arrived on the User's profile and you viewing the
> images section of that User.
>
>
> Now, on the one hand I would want the UserManager to issue one big SQL
> statement with lots of joins that creates and 'fills' the instances of
> User models, as to minimize SQL queries to the DB.
>
> But on the other hand I don't always need all the information about the
> User as per my previous example situations. So I would rather lazy load
> the stuff about the User only when I need it and have it implemented in
> the User model.
>
> How would I approach this in a consistent manner, such that I don't have
> SQL statements scattered all around my UserManager and User model?
> Or would you perhaps create different UserManager models for these
> different tasks?
> Perhaps my whole initial approach could use some rethinking. If you
> think so, don't hesitate to give me a heads up about it please.
>
> I'ld love to read about how you generally approach these kinds of
> situations.
> Thanks in advance.
>
>
>
So basically my question is (with pseudo code):
Do you recursively execute SQL statements in the User model when needed. For
instance something like:
$users = $userManager->getAllByCity( 'Amsterdam' );
foreach( $users as $user )
{
$user->loadImages(); // executes SQL statement to retrieve 'child' image
rows
$user->loadVideos(); // executes SQL statement to retrieve 'child' video
rows
}
Or do you rather execute one large SQL statement in the UserManager model.
For instance something like:
function userManager::getAllByCity( $city )
{
$users = array();
$sql = '
SELECT
columns
FROM
user
LEFT JOIN
image
LEFT JOIN
video
# etc....
';
$result = $db->execute( $sql );
foreach( $result as $row )
{
if( !isset( $users[ $row[ 'id' ] ] ) )
{
$users[ $row[ 'id' ] ] = new User_Model();
}
if( !$users[ $row[ 'id' ] ]->hasImage( $row[ 'imageId' ] ) )
{
$users[ $row[ 'id' ] ]->addImage( $row[ 'imageId' ], $row[
'imageLocation' ] );
}
if( !$users[ $row[ 'id' ] ]->hasVideo( $row[ 'videoId' ] ) )
{
$users[ $row[ 'id' ] ]->addVideo( $row[ 'videoId' ], $row[
'videoLocation' ] );
}
// etc....
}
return $users;
}
$users = $userManager->getAllByCity( 'Amsterdam' );
--
View this message in context:
http://www.nabble.com/-OT--General-design-question-tp23623977p23624005.html
Sent from the Zend Framework mailing list archive at Nabble.com.