Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition)

[eluser]OverZealous[/eluser]
[quote author="Mirage" date="1258862206"]But let me just say, that finding the tidbit about the naming rules for join tables would've been pure luck on my part. I couldn't have done this without asking and moreso - without you answering. The documentation for DMZ is clearly very complete. Yet I find myself jumping all over to try and find answers to my questions.[/quote]

Well, it is under the section called Database Tables. ;-) I don't know how to make that more obvious. Admittedly, the info about join tables got pushed down when I added the ITFK info.

There is a lot of information in the manual. (In some ways, it's almost as big as the CodeIgniter manual!) Sadly, there's not really a way to make everything easy to find for everyone. (Hooray for Google searches.) The manual is really oriented for new users – ie, if you read the getting started section in order, it tries to help get new users started.

[eluser]Kip zonder Kop[/eluser]
[quote author="q.Nghia" date="1258940548"]I think I found the reason ^^

PHP version of server is 5.1.6, my localhost using PHP 5.2, the __toString() of models doesn't work. But still dont know how to fix this...[/quote]

I think this message might help you: http://ellislab.com/forums/viewthread/133950/

[eluser]chadbob[/eluser]
Thanks, I found the extension files!


How does the CSV extension handle has many relationships?

I have a model setup like this:

Code:
class Report extends DataMapper {

    
var $has_many = array("statute");


And I have reports, statutes, and reports_statutes tables in my DB. It all works as it should.


But if I wanted to bulk import with the CSV extension, how would I depict a report that had many statutes associated with it?

In normal code, each one would be accessed as $report_object->statute->description.

Each report has a different number of statutes, so I use foreach loops typically. How would a CSV file be able to communicate this to DMZ?

thanks.

[eluser]Mirage[/eluser]
About accessing relations - I think I've got the concept wrong.

Plant Collections > many plants > many images

Code:
$_collection=new PlantCollection();
$_collection->get_by_id(1); // get the collection record
$_collection->plant->get(); // get all the related plants
$_collection->plant->image->get(); // for each plant get all related images

<?php foreach($_collection->plant->all as $_p):?>
        <p>&lt;?= $_p->botanical_name?&gt;, &lt;?= count($_p->image->all)?&gt;</p>
&lt;?php endforeach; ?&gt;

The image count is 0. But there are related images. So I'm thinking that my last line before the loop doesn't work the way I expected it to. Doing it the following way works:


Code:
$_collection=new PlantCollection();
$_collection->get_by_id(1); // get the collection record
$_collection->plant->get(); // get all the related plants        

&lt;?php foreach($_collection->plant->all as $_p):?&gt;
<p>&lt;?= $_p->botanical_name?&gt;, &lt;?= count($_p->image->get()->all)?&gt;</p>
&lt;?php endforeach; ?&gt;

This is ok for a single pass. But I actually have to run that loop in a couple of places on a page, so I was hoping I could resolve the deeper relations before. Is there an intended way for that? Also, since getting the relation causes a left join, I also get plants for which no images have been assigned. Do I have to test that in the loop or is there a way to turn the left join into a regular join?

Thanks,
-m

[eluser]Mirage[/eluser]
Is this a doco error?

On the Accessing Relationships page there is an example for adding two users to a group and the query for selecting the users is written like this:

Code:
// Get users Jayne Doe and Joe Public
$u = new User();
$u->where('username', 'Jayne Doe')->where('username', 'Joe Public')->get();

Should that second 'where' be 'or_where' ?

-m

[eluser]OverZealous[/eluser]
[quote author="chadbob" date="1258963948"]
How does the CSV extension handle has many relationships?[/quote]

The CSV extension is very basic. In short, it does not support relationships at all.

But you have the source. Feel free to augment it. Optionally, you could store the IDs of the related objects in a different field, then use that to save the item later.

If you are importing, you can use the callback:
Data:
Code:
id,name,widget_ids
1,"Thing","5,6,7"
2,"Other Thing","5,7,9"

Code:
Code:
// Controller:
function _handle_import($object) {
    $widget_ids = explode(',', $object->widget_ids);
    $widgets = array();
    for($id in $widget_ids) {
        $w = new Widget($id);
        if($widget->exists()) {
            $widgets[] = $w;
        }
    }
    return $object->save($widgets);
}

function import_csv($csv_file) {
    $object = new Object();
    $object->load_extension('csv');
    $count = $object->csv_import($csv_file, NULL, TRUE, array($this, '_handle_import'));
    echo "$count Objects imported."
}

Obviously, you'll want to do more verification, etc., to make sure that no one is hacking, but this is a skeleton of the code.

[eluser]OverZealous[/eluser]
[quote author="Mirage" date="1258969194"]About accessing relations - I think I've got the concept wrong.[/quote]

In your example, when you do this:
Code:
$_collection->plant->image->get();

You are only looking up the images for one Plant. It's hard to show in the forum, but the object that is related is a single Plant. For multiple relationships, the only way to access them is through the ->all array. This means that to load each item, you'd have to loop through the all array and call get() on each item.

But wait — there's more!
Using the method include_related, you can include the results from deeper relationships in a single query. You end up with something like this:
Code:
$_collection->plant->include_related('image')->get();
foreach($_collection->plant as $plant) {
    echo($plant->image_id);
    ...
}

Include_Related also supports Deep Relationships, which is really cool. It does have some limitations, so please read through the linked section above to understand (namely: it only works with $has_one relationships).

The unreleased $NEXT_VERSION of DMZ (I'm gonna try to pack it up and get it out there before Wednesday this week) can also automatically instantiate the related objects, so you can do it like this:
Code:
foreach($_collection->plant as $plant) {
    echo($plant->image->id);
    ...
}

---

Also, you were right, there was a mistake in the docs.

[eluser]OverZealous[/eluser]
Also, @Mirage, you probably are already be doing this, but it's a good habit to set up your queries in your controller, and leave the view for rendering data. That keeps a clean separation between display data and data manipulation.

Ignore this comment if you are already doing this.

(Also, if I have any mistakes in my previous code samples, please excuse them. I've been writing a Java app for the last week. My brains a bit befuddled. ;-) )

[eluser]Mirage[/eluser]
@OverZealous - Yes I'm usually doing my data buildup in the controller. Even more so since I started using DMZ. However from a CMS perspective it often makes sense to do read-only access in the view as the content is page driven. I'm looking to break that out into blocks, but testing in the view.

No worries about mistakes in code samples. I know all about befuddled brains. ;-)

-m

[eluser]OverZealous[/eluser]
DMZ 1.6.0 was just released!

You may continue to discuss 1.5.4 here for a while, but I would plan on migrating to DMZ 1.6.0 in the near future, as 1.5.4 will probably not see any more changes.

Please continue the discussions here.




Theme © iAndrew 2016 - Forum software by © MyBB