Welcome Guest, Not a member yet? Register   Sign In
dmz htmlform dropdown rendering
#1

[eluser]The Mask[/eluser]
Hi,

I have a data model where clients have a 1 to many relationship with sites and a site has a 1 to many relationship with jobs. When I wish to add a new Job, I'd first like to capture the client using a dropdown (and then show a filtered list of sites for the selected client).

So...

$c = new Client();
$c->get();
$form_fields = array( 'client' => array( 'list' => $c ) etc... );
When the dropdown renders, I get...
<select>
<option value="0">Client 1</option>
<option value="1">Client 2</option>
</select>

But the ID for Client 1 is 17 and for Client 2 is 18.

In order to get this to render with the correct IDs, I need to specify a relationship between Clients and Jobs (which is not really correct as their relationship is via Sites). i.e.

class Client extends Datamapper
{
var $has_many = array( 'com3_site' ); // renders clients with 0,1,2,3 etc
var $has_many = array( 'com3_site', 'com3_job' ); // renders clients using table IDs
}

class Job extends Datamapper
{
var $has_one = array( 'com3_site' ); // renders clients with 0,1,2,3 etc
var $has_one = array( 'com3_site', 'com3_job' ); // renders clients using table IDs
}

Can anyone shed any light on this please as it seems I am having to specify a relationship incorrectly to get this to render as expected. Thanks
#2

[eluser]bEz[/eluser]
Including Fields from Deep Relationships

This method also supports deep relationships. You can only include columns from objects that are related by single relationships all the way. The default column prefix for deep relationships is to replace all forward slashes with underscores. You can still override this to be whatever you want.

A deep relationship is simply the name of each related object, in order, separated by a forward slash (/).

Here's an example:
Code:
// Create Post
$p = new Post();

// Include the user's name in the result:
$p->include_related('user', 'name');
// include the user's group's name in the result:
$p->include_related('user/group', 'name');
$p->get();  

foreach($p->all as $post) {
    echo("{$post->user_name} ({$post->user_group_name})\n");
}

So in your case, you would just need to do 'client/site/job' as your deep relationship.

Code:
$c = new Client();

// Include the client's site data in the result:
$c->include_related('site', 'name');
// include the site's job's data in the result:
$c->include_related('site/job', 'name');
$c->get();  

foreach($c->all as $client) {
    echo("{$client->site_name} ({$client->site_job_name})\n");
}

Of course, you would want to supply the "list" data for the form_fields parameter.
#3

[eluser]The Mask[/eluser]
Sorry bEz but your reply does not answer my question.

I am trying to render a dropdown from my Client table and because this table is not related to Jobs, for some reason it assigns values 0,1,2 etc and is not using the IDs from the Client table.

i.e.

<select>
<option value=“0”>Client 1</option>
<option value=“1”>Client 2</option>
</select>

But the ID for Client 1 is 17 and for Client 2 is 18 so I would expect...

<select>
<option value=“17”>Client 1</option>
<option value=“18”>Client 2</option>
</select>

Is there a way to force the rendering of the dropdown to use IDs?




Theme © iAndrew 2016 - Forum software by © MyBB