CodeIgniter Forums
DataMapper 1.6.0 - 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: DataMapper 1.6.0 (/showthread.php?tid=11358)



DataMapper 1.6.0 - El Forum - 03-02-2009

[eluser]stefanv[/eluser]
[quote author="OverZealous.com" date="1236006441"]
Any thoughts?[/quote]

Perhaps the code:
Code:
// Build up the error message
        $message = $this->error_prefix . $error . $this->error_suffix;

        // Set field specific error
        $this->error->{$field}[] = $message;

        // Add field error to errors all list
        $this->error->all[] = $message;

        // Append field error to error message string
        $this->error->string .= $message;

should be replaced with:
Code:
// Build up the error message
        $message = $this->error_prefix . $error . $this->error_suffix;

        // Set field specific error
        $this->error->{$field} .= $message;

        // Add field error to errors all list
        $this->error->all[] = $message;

        // Append field error to error message string
        $this->error->string .= $message;

But then the error string is going the be real long... Some intelligence should be added, since the 'fieldname' isn't needed for all the messages added.

So:
Code:
Username is a required field.
Username should be at least 3 character long.
should become:
Code:
Username is a required field and should be at least 3 character long



DataMapper 1.6.0 - El Forum - 03-02-2009

[eluser]OverZealous[/eluser]
I just don't see a good reason to return multiple errors per field. Once one error occurs, that should be all that is really needed to convey the message.

Since the order you set the validation rules matters, if you put 'required' at the end, the required rule wouldn't ever run until after the min_length rule, therefore you'd always get:
Code:
"Username must be at least 3 characters long."

Which not only conveys all of the information, but makes more sense.

Also, there is no practical way DataMapper could combine error messages and still be language-agnostic. I still say, if you need that kind of compiled information ("Field should be this and that and this too."), either create a custom validation method and error message, or provide that information inline.

Anyway, this is probably something for stensi or someone else to decide. It's easy enough to customize if that is what is desired.


DataMapper 1.6.0 - El Forum - 03-02-2009

[eluser]stefanv[/eluser]
[quote author="OverZealous.com" date="1236011250"]I just don't see a good reason to return multiple errors per field. Once one error occurs, that should be all that is really needed to convey the message.

Since the order you set the validation rules matters, if you put 'required' at the end, the required rule wouldn't ever run until after the min_length rule, therefore you'd always get:
[/quote]

Looking at your reply and (re)thinking what I stated.. You are probably right. There should only be 1 error per field: the must important one (the first one in the validation rule)

The statement 'the validation rules order' triggered me to look at my code again.. The order was wrong.. fixed it..

This fix solved my problem.

1 thing i would like to add.. ->string and ->all both represent all the errors found.. Wouldn't it be nice to add something like ->current the get a list of the current errors (the limited list)


DataMapper 1.6.0 - El Forum - 03-02-2009

[eluser]OverZealous[/eluser]
[quote author="stefanv" date="1236012299"]
1 thing i would like to add.. ->string and ->all both represent all the errors found.. Wouldn't it be nice to add something like ->current the get a list of the current errors (the limited list)[/quote]

I'm confused by this. From the guide:

Code:
$obj->error->all; // all errors from last validation as an array
$obj->error->string; // all errors from last validation as a string
$obj->error->{$field}; // the last validation error for $field.

What else do you want? The errors listed are all of the errors for this transaction. If you see an error in this list, it happened now. There is one minor known bug, that $obj->error->transaction doesn't get initially set or cleared. This only occurs if you are using auto transactions, AND you get a transaction error.


DataMapper 1.6.0 - El Forum - 03-02-2009

[eluser]Matthew Pennell[/eluser]
Someone might have already pointed this out, but just in case - the download links on the DataMapper User Guide are broken.


DataMapper 1.6.0 - El Forum - 03-02-2009

[eluser]Billy Khan[/eluser]
Yep, really would like to try this but the download links are broken.


DataMapper 1.6.0 - El Forum - 03-02-2009

[eluser]OverZealous[/eluser]
Check the first page of this thread. The latest official version of DataMapper is there, including the help documentation. You do not need to download it from stensi's website.


DataMapper 1.6.0 - El Forum - 03-03-2009

[eluser]tdktank59[/eluser]
I came up with a neat function for deleting single or multiple relationships while editing a record.

For instance,

Brainstorm
-> id
-> name
-> description
-> author_id
-> data_source_1_id
-> data_source_2_id

DS1 and DS2 cannot be the same and can both be nulled if only 1 source is provided.

However while updating I dont belive you can delete then save... (not sure here and havnt tried...)
But this will search the master object, grab the id of the field you want to delete, create the object of the relation and delete it all for you!

Anyways heres the fun little method I created (just tacked it into the brainstorm model
Sorry for the somewhat confusing nature of the variables lol

Code:
/**
* Deleted the relation between the master object
* and its children.
*
* @param varchar $master_obj the name master object loosing the relation(s)
* @param varchar $master_key the column name to get the master object
* @param varchar $master_value the value for the column name
* @param array $remove_objs The relations to be deleted in format ID => array ($class => $coloum_name)
*/
function delete_relation($master_obj,$master_key,$master_value,$remove_objs)
{
    $mast_obj = new $master_obj();
    $mast_obj->where($master_key,$master_value)->get();

    // echo 'Master Object: '.$master_obj.' ( '.$master_key.' => '.$master_value.' )<br/>';

    foreach ($remove_objs as $remove)
    {
        foreach ($remove as $key => $val)
        {
            //echo key($remove).' => '.$key.' => '.$val.'<br />';

            $mast_obj->$val->get();

            $obj = new $key();
            $obj->where('id',$mast_obj->$val->id)->get();
            $delete[$val] = $obj;
        }
    }

    $mast_obj->delete($delete);
}

Then in the controller to delete the records

set an array such as (you need the same format)
the $array[] makes it so you can have multiples of the same (so I could delete DS1 and DS2 at the same time) You can fill this in with a value it wont matter... just as long as the array under it has the $class => $field
Code:
$delete[] = array ($class => $field);

and then right before you save call the delete method as such
Code:
// Delete any nulled relations
if (isset($delete)) $b->delete_relation($master_class,$master_key,$master_value,$delete);

Simple as that let me know if there are any questions.
BTW I tested and this method works.


DataMapper 1.6.0 - El Forum - 03-03-2009

[eluser]OverZealous[/eluser]
I'm confused as to what this method does. It seems like a lot of complexity, but all it does is delete a set of related objects?
Why would you use this instead of:
Code:
$master->delete(array(
    'data_source_1' => $ds1
    'data_source_1' => $ds2
));

Or, if you know you are using the has_one fields on the same table:
Code:
$master->data_source_1_id = NULL;
$master->data_source_2_id = NULL;
$master->save();

I'm just not sure what the method does. Also, you query $mast_obj in a way that might return multiple values, however, you only call $mast_obj->delete() on the first returned value, possibly leading to unexpected results.

Finally, if you just want a simpler method to delete a set of related objects, why not this:
Code:
function delete_related_fields($fields) {
    if(!is_array($fields)) {
        // allow for single fields
        $fields = array($fields);
    }
    $success = array();
    foreach($fields as $f) {
        $this->{$f}->get();
        // This should work, even if the field is different than the model, due to the way the delete method is designed.
        $success[] = $this->delete($this->{$f}->all, $f);
    }
    return !in_array(FALSE, $success);
}

Usage:
Code:
$my_obj->delete_related_fields(array('data_source_1', 'data_source_2'));

This also allows for deleting all related has_many ones.

---

On a related note, I'm trying to get my project to a private beta by this weekend. If everything goes as planned, I hope to set some time aside to build up some examples and better document my extended DataMapper.


DataMapper 1.6.0 - El Forum - 03-06-2009

[eluser]Billy Khan[/eluser]
I am new to datamapper and need to do this...

I have three tables, regions, members and join_members_regions.

After i have downloaded all data into a csv, i need to truncate the members and join_members_regions tables. From reading the help pages, i know i can do delete_all or delete but this requires a specific member record, ho do i delete all from both?