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

[eluser]Daniel H[/eluser]
Hi Phil,

I'm fairly certain this will have been covered before but I can't really find it in this thread.

How would you properly relate an object to multiple objects of the same type (i.e. a FEATURE can be related to one or many other FEATURES).

Should a new table called 'related_features' be made, with a feature_id and related_feature_id column, or is there a better way?

[eluser]Daniel H[/eluser]
Also, I wonder if any could share the functionality for how they update relationships.

At the moment, if - say - a blog post was associated to multiple categories, and when the post is edited and additional categories are selected (or unselected), I would then delete all the existing relationships, and then save then new selection of categories again.

This seems a little inefficient, but at the same time comparing arrays of IDs doesn't make much sense either?

[eluser]OverZealous[/eluser]
[quote author="Daniel H" date="1252957763"]How would you properly relate an object to multiple objects of the same type (i.e. a FEATURE can be related to one or many other FEATURES).[/quote]

It's in the docs, scroll to Self Relationships.

Quote:Also, I wonder if any could share the functionality for how they update relationships.
Troubleshooting: What's the best way to add a method I want to use multiple times?

[eluser]Daniel H[/eluser]
Ah thanks Phil.

I actually started using the array extension for handling adding and removing relationships, but noticed that while it adds relationships perfectly, if you then deselect the relationship when you edit an object, the relationship isn't deleted.

I should also add that this is a self-referenced relationship, which may be the problem.

Dan.

UPDATE: it does seem that this bug on happens with self-references. Any ideas for a work-around.

[eluser]emorling[/eluser]
I feel that there's something obvious here that I'm missing....

Let's say I have a shelf with a number of books. What's the easiest way to find out if the shelf has a SPECIFIC book? Now I loop thru all the books on the shelf, but this seems rather inefficient...

Code:
$book = new Book();
$book->get_by_id(1);

foreach ($shelf->book->all as $b){
    if($b==$book){
       // found the book, do something useful
    }
}

[eluser]macigniter[/eluser]
hey phil,

i was just trying to do this and found out that it doesn't work...

Code:
$c = new Calendar();
$c->select_min('date');
$c->get();

if ($c->exists())
{
   echo $c->date;
}

The $c->select_min('date') DOES work, but unfortunately the $c->exists() doesn't return TRUE. If I output $c->date directly it returns the correct value, but not within the if clause. Is this a bug or am I doing something wrong?!

[eluser]OverZealous[/eluser]
[quote author="emorling" date="1253112372"]What's the easiest way to find out if the shelf has a SPECIFIC book?
[/quote]

This is the least amount of queries, and it looks up the book at the same time:

Code:
$shelf = ... // Look up shelf
$book = $shelf->book->get_by_id(1); // Look up Book
if($book->exists()) {
    // Shelf contains book
} else {
    // Shelf does not contain book, you might need to re-query book
    $new_book = new Book();
    $new_book->get_by_id(1); (for example)
}

[eluser]OverZealous[/eluser]
[quote author="macigniter" date="1253115364"]
i was just trying to do this and found out that it doesn't work...[/quote]

$C->exists is based on the ID. $c->exists will only return TRUE if this is an object with an assigned ID, so in SELECT queries, or queries that don't include an ID, it will always return FALSE.

Instead, make life simpler:
Code:
if ($c->date)
{
   echo $c->date;
}

[eluser]ZeusChicago[/eluser]
Hopefully this is just something stupid on my part, but its driving me crazy and I cant figure it out.

I have the following code in my controller which is using two simple tables

Code:
$subModel = new Subscription();
    $subscriptionWhere = ('fa_user_id =' .$this->db_session->userdata('id'));
    $subModel->where($subscriptionWhere);
    $subModel->order_by('startdatetime','DESC');
    $subModel->join_related('subscriptiontypes')->get();
    $this->data['previousSubDataCount'] = $subModel->count();
    $this->data['previousSubData'] = $subModel->all;

and this code in my view

Code:
echo "<tr><td>" . $previousSubDataCount . "</td></tr>";
                            foreach($previousSubData as $preData){
                                echo "<tr><td>". $preData->subscriptiontype_displayname . "</td><td>" . date("m/d/y",strtotime($preData->startdatetime)) . "</td><td>". date("m/d/y",strtotime($preData->enddatetime)) . "</td></tr>";
                            }

But to my dismay, even though the $previousSubDataCount spits out 2 (which I have 2 records) the foreach($previousSubData as $preData){} loop only prints out 1 record.

If I enable the profiler and run the SQL that the code is executed returns 2 rows. I tried to move the foreach loop into the control just to test what I was getting back like

Code:
foreach($subModel->all as $test){
            echo $test->subscriptiontype_displayname ."|" . date("m/d/y",strtotime($test->startdatetime)) . "|". date("m/d/y",strtotime($test->enddatetime));
        }

and I still only get one record Sad not sure what im doing wrong.

Thanks in advance

Zeus


update I figured out my count above was not looking at the same recordset it seems but even after updating the count statement to

Code:
$this->data['previousSubDataCount'] = $subModel->join_related('subscriptiontypes')->count();

its still returning 2 rows, which is what I am expecting

[eluser]OverZealous[/eluser]
[quote author="ZeusChicago" date="1253142670"]
...
But to my dismay, even though the $previousSubDataCount spits out 2 (which I have 2 records) the foreach($previousSubData as $preData){} loop only prints out 1 record.
...
[/quote]

Well, one thing is wrong here, for sure — you have to rebuild your queries between get(s), count(s), and the like. ActiveRecord deletes the old query information between queries, so you can have run a new query each time.

If you looked at your query output (by turning on CI's profiler — it's in the Troubleshooting Section), you'd see that your count() query is basically doing:
Code:
COUNT(*) FROM subscriptions

So it's returning all rows. If you need two of the same query, you can either use DMZ/AR's caching, or build the query twice.


But you don't need to do this at all!
The DMZ count method is used when you do not want the results, just the number of items, because it runs a query. If you want to know how many items are returned normally, just use count($object->all)!

(Also, if you are using a non-current DMZ, you might need to add distinct to your query, because older DMZ's automatically collapsed items with the same ID into one result.)




Theme © iAndrew 2016 - Forum software by © MyBB