Welcome Guest, Not a member yet? Register   Sign In
DataMapper related fields is empty?
#1

[eluser]Davva[/eluser]
Hi,
I just downloaded DataMapper, and now I'm trying to do some beginner stuff. I have set up a db (mysql) with three tables:
- clients
- projects
- users

Each client has a creator_id field which is a foreign key mapped to users.id. The creator_id is stored directly in the clients table.

Now I want to list all clients, show a project count for each client and the name of the user that created the client. However, when I try to access the properties of the user that created the client, I get nothing back.

Here are my models:

Code:
class User extends DataMapper {
var $has_many = array('project',
  
  // clients created by this user
  'created_client' => array(
   'class' => 'client',
   'other_field' => 'creator'
  )
);
}

class Client extends DataMapper {
var $has_many = array('project');

var $has_one = array(
  // user that created this client
  'creator' => array(
   'class' => 'user',
    'other_field' => 'created_client'
   )
);
}

class Project extends DataMapper {
var $has_one  = array('client');
var $has_many  = array('user');
}

The view code:
Code:
<table>
&lt;?php

$clients = new Client();
$clients->get();

foreach($clients as $client) {
?&gt;
<tr>
<td>&lt;?php echo $client->name; ?&gt;</td>
<td>&lt;?php echo $client->projects->count() ?&gt;</td>
<td>&lt;?php echo $client->creator->firstname; ?&gt;</td>
</tr>
&lt;?php } ?&gt;
</table>

The view above spits out the names of each client and the project count associated with each client. But the $client->creator->firstname is empty. In fact, no $client->creator props are available. If I just output $client->creator, I get "user" (which is the class name...).

What am I missing here?
#2

[eluser]WanWizard[/eluser]
You're not 'getting' $client->creator anywhere, so you're accessing an empty object (of the class User).

Datamapper does lazy loading, it doesn't fetch any relation automatically unless you configure it to do so. In general, you would not want that, any unnecessary query is one to many...
#3

[eluser]Davva[/eluser]
ok, thanks. So what I need to do in order for the example above to work is to get() each creator.

Thus:
Code:
...
<td>&lt;?php echo $client->creator->get()->firstname; ?&gt;</td>
...

Thanks!

#4

[eluser]WanWizard[/eluser]
Possible, but not very efficient as it will fire a new query for every client in your resultset.

Better is:
Code:
$clients = new Client();
$clients->include_related('creator')->get();

foreach($clients as $client) {
?&gt;
<tr>
<td>&lt;?php echo $client->name; ?&gt;</td>
<td>&lt;?php echo $client->projects->count() ?&gt;</td>
<td>&lt;?php echo $client->creator_firstname; ?&gt;</td>
</tr>
&lt;?php } ?&gt;

p.s. I hope this is for test purposes only, you should not have any executable code in your views (besides the code needed to layout the HTML).
#5

[eluser]Davva[/eluser]
No, this is not production code, I'm just trying to trim things down so to minimize code and simplify things here. Smile

But I'm having trouble making your last suggestion work.. The generated sql query tries to access a join table named "creator_users". But I am storing the creator_id directly in the clients table.

Code:
SELECT `clients`.*, `creator_users`.`firstname` AS creator_firstname, `creator_users`.`lastname` AS creator_lastname
FROM (`clients`)
LEFT OUTER JOIN `users` creator_users ON `creator_users`.`id` = `clients`.`creator_id`

I did read the section on how to store fk in table, but I can't really wrap my head around it...

Edit: by the way, is there a better way to get the project count for each client than via count()?
Code:
$client->projects->count() // generates a query for each row
#6

[eluser]WanWizard[/eluser]
Then your relationship definition is wrong. The penalty for choosing non-standard naming is that you will have to define everything yourself.

Read up on advanced relationships, and try to understand what each of the parameters do.

As for your count question: see http://datamapper.wanwizard.eu/pages/get...ated_count.
#7

[eluser]Davva[/eluser]
Great, thanks. It works now!




Theme © iAndrew 2016 - Forum software by © MyBB