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 - 02-26-2009

[eluser]tdktank59[/eluser]
The validation errors are from the form validation... not datamapper i found the problem after I started looking lol...


DataMapper 1.6.0 - El Forum - 02-27-2009

[eluser]cberk[/eluser]
I'm somewhat new to CI and DM, but you guys have accelerated my learning curve tremendously, so thanks.

I'm working on a project in DM and am enjoying it a lot, but right now I'm stumped. I have some custom validation functions that need to be available to multiple models, so I need to either extend the DM library or edit it to include my functions. Obviously I would prefer the former. So I made my own library and saved it to the libraries folder as mydatamapper.php, and extended the DM library like so:

Code:
class MyDataMapper extends DataMapper
{
    /**
     * Class Constructor
     */
    function MyDataMapper()
    {
        parent::DataMapper();
        
    }

Then I pointed all of my models to the new library:
Code:
class Client extends MyDataMapper {

    function Client()
    {
        parent::MyDataMapper();
    }

Unfortunately, I am getting an error that says "An Error Was Encountered. Unable to load the requested class: Datamapper".

This is the first time I have tried to extend a non-native library, and I'm not sure what I'm doing wrong. Any pointers would be greatly appreciated.


DataMapper 1.6.0 - El Forum - 02-27-2009

[eluser]OverZealous[/eluser]
@cberk:
You almost have it!

Instead of making it a library, place your extension in the models directory. Then, just add DataMapper (and not your new model) to your autoloads. You can extend it the same way, and it will be autoloaded by DataMapper just fine.

I recommend everyone set up DataMapper this way. You almost always will end up extending it in some way!

PS: if you want to make your code more flexible, change your constructors to this (PHP5 only):
Code:
function __construct()
{
    parent::__construct();
}

This allows you to easily change your base class!


DataMapper 1.6.0 - El Forum - 02-27-2009

[eluser]cberk[/eluser]
Thanks, OverZealous! Let me make sure I have this straight.

My extension mydatamapper.php should be saved to the application/models folder, and be constructed something like this:
Code:
class MyDataMapper extends DataMapper {
    /**
     * Class Constructor
     */
    function MyDataMapper()
    {
        parent::DataMapper();
    }

My other models should extend DataMapper, not MyDataMapper:
Code:
class Client extends DataMapper {

    function Client()
    {
        parent::DataMapper();
    }

I'm not sure exactly what you mean by [quote author="OverZealous.com" date="1235737577"]@cberk:
Then, just add DataMapper (and not your new model) to your autoloads.[/quote]

DataMapper is already autoloaded in my application/config/config.php file, so does that mean I'm set? It seems logical that I would need to edit the DataMapper library in some way for it to find my extension. Am I on the right track or am I missing something?

Thanks for your help!


DataMapper 1.6.0 - El Forum - 02-27-2009

[eluser]OverZealous[/eluser]
Sorry to be confusing, you still need to subclass MyDataMapper, not DataMapper.
Code:
class Client extends MyDataMapper {
    function __construct() { // or Client()
        parent::__construct(); // or MyDataMapper()
    }
}

Then leave DataMapper in application/config/autoload.php, but make sure that MyDataMapper is never autoloaded.

When DataMapper starts up, it automatically includes all of the models in the application/models folder, so MyDataMapper will get loaded.

Good Luck!


DataMapper 1.6.0 - El Forum - 02-27-2009

[eluser]cberk[/eluser]
That fixed it! Thanks!


DataMapper 1.6.0 - El Forum - 02-27-2009

[eluser]quasiperfect[/eluser]
hi

in the datamapper config i have auto_transaction set to true
i'm traing to create a user asignit to a group and based on a value $i
create 1 or 2 profiles
this action has to be executed all or at all

when i run my code i always get
Code:
A PHP Error was encountered.Severity: Notice.Message: Undefined property: stdClass::$transaction

the user get's saved but nothing else

any tips ?

in my controller i have
Code:
$u = new User();
$u->name = 'test';
$i= 4;
switch ($i) {
    case 0:
           $p = new Profile();
        $p->sex = '0';
    break;
    case 1:
        $p = new Profile();
        $p->sex = '1';
    break;
    case 2:
        $pa = new Profile();
        $pa->sex = '0';
        $pb = new Profile();
        $pb->sex = '1';
    break;
    case 3:
        $pa = new Profile();
        $pa->sex = '1';
        $pb = new Profile();
        $pb->sex = '1';
       break;
       case 4:
           $pa = new Profile();
        $pa->sex = '0';
        $pb = new Profile();
        $pb->sex = '0';
       break;
}
$g = new Group();
$g->where('name', 'testing')->get();
if (empty($g->id))
{
    echo 'error no group by the name testing'
}
else
{
    $saved = false;
    switch ($i) {
        case 0:
        case 1:
            if ($u->save(array($g,$p)))
               {
                   $saved = true;
               }
           break;
           case 2:
           case 3:
           case 4:
               if ($u->save(array($g,$pa,$pb)))
               {
                $saved = true;
               }
           break;
    }
    if $saved {
        echo 'ok';
    }
    else
    {
        echo 'failed';
        echo $u->error->string;
        echo $u->error->transaction;
    }
}



DataMapper 1.6.0 - El Forum - 02-27-2009

[eluser]stefanv[/eluser]
Today i found out that the was a little bug in the way errors are stored in the 'validation' function in combination with the 'error_message' function.

So i made a patch against the datamapper library file for the 'error_message' function to fix it.

This patch changes one thing: you can get 'all' errors per field and not only the last one!
- $object->error->fieldname now gives an array back instead of a string.
- $object->error->all & $object->error->string are not changed !

Code:
/**
* Error Message
*
* Adds an error message to this objects error object.
*
* @access    public
* @access    string
* @access    string
* @return    void
*/
function error_message($field, $error)
{
    if ( ! empty($field) && ! empty($error))
    {
        // 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;
    }
}



DataMapper 1.6.0 - El Forum - 02-27-2009

[eluser]OverZealous[/eluser]
@quasiperfect
You probably do not have a field named "transaction" on your object.

@stefanv
While I like your idea, it would break a lot of existing code that expects a string on the fields. Why are you getting multiple errors per field? I'm pretty sure DataMapper's validation routine stops as soon as any field has an error.


DataMapper 1.6.0 - El Forum - 02-27-2009

[eluser]quasiperfect[/eluser]
what do u mean "transaction" field ?
i'm refering to this http://stensi.com/datamapper/pages/transactions.html
and any idea about the really important thing

"i’m traing to create a user asignit to a group and based on a value $i
create 1 or 2 profiles
this action has to be executed all or at all"

thanks in advance