CodeIgniter Forums
[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: [Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) (/showthread.php?tid=23751)

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20


[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) - El Forum - 11-06-2009

[eluser]The Mask[/eluser]
Sorry, the Utility option DOES work as I'd like so please ignore the previous post.


[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) - El Forum - 11-06-2009

[eluser]The Mask[/eluser]
I have a problem trying to add a hidden control on a form.

class Test extends Datamapper {

var $validation = array(
'name' => array( 'label => 'Name'... etc ),
'testid' => array( 'type' => 'hidden' )
);
}

$t = new Test();
$form_fields = array( 'name', 'testid' );
echo $t->render_form( $form_fields....etc );

I have viewed the source code and testid does not get rendered - any ideas?


[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) - El Forum - 11-06-2009

[eluser]bEz[/eluser]
If your datamapper model is Test and assuming you're following convention, there is no 'testid' field, just an 'id' field.
You therefore would not need to "VALIDATE" the 'id' field. You would, however, add the 'id' field to the form fields array.

Code:
class Test extends Datamapper {

  var $validation = array(
  ‘name’ => array( ‘label => ‘Name’... etc ),
  etc,
  etc,
  etc
  );
}
// ------------------------------------------------------------------
$t = new Test();
$form_fields = array( 'id', ‘name’, ‘testid’ );
echo $t->render_form( $form_fields, $url);
// ------------------------------------------------------------------

Now, if there was some other use of the field testid, then you could define the type as hidden via the $form_fields array
(can't recall if this still requires a custom function in the Model declaration).
Code:
$form_fields = array(
    'id', // hidden field (by design)
    ‘name’,
    'testid'  => array(
        'type' => 'hidden'
    ),
);



[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) - El Forum - 11-06-2009

[eluser]The Mask[/eluser]
bEz, thanks for the posting but this is exactly what I am doing yet no hidden field is being rendered for testid??

$form_fields = array(
'id', // hidden field (by design)
‘name’,
'testid' => array(
'type' => 'hidden'
),
);


[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) - El Forum - 11-08-2009

[eluser]lsemel[/eluser]
Is there an elegant way to do a Rails-style counter cache in DMZ Datamapper? The idea is to automatically keep a track of relationship's count within a column, for performance.

http://www.idolhands.com/ruby-on-rails/using-counter_cache-with-sti-models/

The only way I can think to do this is to override save() or create a prepping validation rule, on both sides of the many to many relationship, which seems like a hack.


[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) - El Forum - 11-08-2009

[eluser]OverZealous[/eluser]
[quote author="lsemel" date="1257744054"]The only way I can think to do this is to override save() or create a prepping validation rule, on both sides of the many to many relationship, which seems like a hack.[/quote]

I don't think either of those techniques is a hack. Both would be supported techniques.

There are currently no methods in DMZ anywhere that can notify object A of a save on object B or vice-versa (besides save()). It sounds like a good feature to add in, though. (Like, calling on_related_save just before a relationship save, for example.)

Also: the next version of DMZ (which is ready, but I haven't had time to package), has a new method called include_related_count() which does just that - includes the real-time count of directly-related objects with any query. This might be helpful, although it just uses subqueries, so performance may or may not be an issue in your app still.


[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) - El Forum - 11-10-2009

[eluser]Jinkusu[/eluser]
Hail guys

Is their anyway way to chech is a relation ship exist between two objects. Example:

Code:
$lecturer = new User($id);
$courses = new Course();

foreach($courses as $course){

//if $lecturer is related to this course then do sommin

}

Thanks in advance.


[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) - El Forum - 11-10-2009

[eluser]lsemel[/eluser]
Regarding my question about "Rails style counter cache", I realized that overriding save() wouldn't be a great solution because it wouldn't take into account deletes, would need to be done on both sides of the relationship, and would execute on every single save() even if the relationship wasn't changing, unless I parsed out the arguments and tried to identify when a relationship is being saved. Since save() and delete() take a variety of different formats for the arguments, that would definitely be a tricky process and would break if those arguments ever change.

I also checked out the prepping validation method more, but according to the code, validation takes place before saves of related objects, so my count of related objects would end up being off, and there also no way to handle deletes.

I think your idea of calling a user-defined callback right before, or right after, any table is inserted, updated, or deleted, and providing an opportunity to execute code (such as to compute the new related count) is the way to go.


[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) - El Forum - 11-10-2009

[eluser]bEz[/eluser]
[quote author="Jinkusu" date="1257897618"]Hail guys

Is their anyway way to chech is a relation ship exist between two objects. Example:

Code:
$lecturer = new User($id);
$courses = new Course();

foreach($courses as $course){

//if $lecturer is related to this course then do sommin

}

Thanks in advance.[/quote]
I just need to ask, what is the ultimate goal you are seeking.
(A) To know that they are related
or
(B) To ack upon "lecturer" or "courses" if determined to be related.

My initial opinion would be to say (A) is not possible, and that you would need to use "where_related($lecturer)" (or $lecturer->courses) as designed.


[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition) - El Forum - 11-10-2009

[eluser]OverZealous[/eluser]
To add to what bEz is saying, you have to either run a query or loop through the list of all related objects to see if two objects are related.

The following methods would work. I recommend technique 1 for a few items, or technique 2 for many items.

Code:
$item = new Item($id);
$others = new Other();
$others->...->get();

// Technique 1: cleaner, but requires many more queries
foreach($others as $other) {
    if($item->other->where('id', $other->id)->get()->exists()) {
        // act on it
    }
}

// Technique 2: messy, but only has one extra query
$item->other->select('id')->get();
foreach($others as $other) {
    $found = FALSE;
    foreach($item->other as $other_test) {
        $found = $other_test->id == $other->id;
        if(found) {
            break;
        }
    }
    if($found) {
        // act on it
    }
}

// Technique 3: manually add a subquery to do in a single query
// HARD TO MAINTAIN
$sql = '(SELECT COUNT(*) ' .
        'FROM items_others ' .
        'WHERE items_others.other_id = others.id ' .
        '  AND items_others.item_id = ' . $item->id . ') as item_exists';
$others->select('*')->select($sql, FALSE)->get();
foreach($others as $other) {
    if($other->item_exists) {
        // act on it
    }
}