Jump to content

Doctrine - Joining 3 objects?


Recommended Posts

Given the following schema.

 

# Defines the user table.
User:
    columns:
        name:
            type: string(255)
            minlength: 4
            nospace: true
            regexp: '/[_a-zA-Z0-9]/'
        pass:
            type: string(255)
            minlength: 6
            nospace: true
        salt: string(32)
    actAs:
        Timestampable:
        Searchable:
            fields: [name]
    relations:
        Email:
            local: id
            foreign: user_id
        Phone:
            local: id
            foreign: user_id
        UserRole:
            local: id
            foreign: user_id

# Users email address.
Email:
    columns:
        user_id: integer
        val:
            type: string(255)
            email: true
    relations:
        User:
            local: user_id
            foreign: id
    actAs:
        Timestampable:
        Searchable:
            fields: [val]

# User phone number.
Phone:
    columns:
        user_id: integer
        val:
            type: string(255)
            nospace: true
            # this covers an internal ph extension.
            minlength: 3
    relations:
        User:
            local: user_id
            foreign: id
    actAs:
        Timestampable:
        Searchable:
            fields: [val]

# What role is a user assigned? eg; admin
# Directly related to Zend's ACL.
UserRole:
    columns:
        user_id: integer
        role_id: integer
    relations:
        User:
            local: user_id
            foreign: id
        Role:
            local: role_id
            foreign: id
    actAs:
        Timestampable:

# Define various role types. eg; admin, staff, guest.
Role:
    columns:
        val:
            type: string(80)
            minlength: 4
            nospace: true
            regexp: '/[_a-zA-Z]/'
    relations:
        UserRole:
            local: id
            foreign: role_id
    actAs:
        Timestampable:

 

I'm using the following to retrieve the users name, phone, email and role id.

 

$query = Doctrine_Query::create()
            ->select('u.name name, e.val email, p.val phone, r.role_id role')
            ->from('User u')
            ->leftJoin('u.Email e')
            ->leftJoin('u.Phone p')
            ->leftJoin('u.UserRole r')
            ->where('u.id = ?', 1);
        $user = $query->fetchOne();

        $this->view->name = $user->name;
        $this->view->email = $user->email;
        $this->view->phone = $user->phone;
        $this->view->role = $user->role;

 

How would I alter this query to enable me to retrieve the users role val?

 

I am aware I could execute a simple query, then use the object relations to get to the data I want. eg;

 

$query = Doctrine_Query::create()
            ->select('u.name')
            ->from('User u')
            ->where('u.id = ?', 1);
        $user = $query->fetchOne();

        $this->view->name = $user->name;
        $this->view->email = $user->Email->val;
        $this->view->phone = $user->Phone->val;
        $this->view->role = $user->UserRole->Role->val;

 

However this is apparently very inefficient.

 

Any help would be much appreciated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.