The validation errors are from the form validation… not datamapper i found the problem after I started looking lol…
This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.
The active forums are here.
September 05, 2008 12:32pm
Subscribe [115]#601 / Feb 26, 2009 7:27pm
The validation errors are from the form validation… not datamapper i found the problem after I started looking lol…
#602 / Feb 27, 2009 2:09am
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:
class MyDataMapper extends DataMapper
{
/**
* Class Constructor
*/
function MyDataMapper()
{
parent::DataMapper();
}Then I pointed all of my models to the new library:
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.
#603 / Feb 27, 2009 2:26am
@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):
function __construct()
{
parent::__construct();
}This allows you to easily change your base class!
#604 / Feb 27, 2009 2:56am
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:
class MyDataMapper extends DataMapper {
/**
* Class Constructor
*/
function MyDataMapper()
{
parent::DataMapper();
}My other models should extend DataMapper, not MyDataMapper:
class Client extends DataMapper {
function Client()
{
parent::DataMapper();
}I’m not sure exactly what you mean by
@cberk:
Then, just add DataMapper (and not your new model) to your autoloads.
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!
#605 / Feb 27, 2009 3:04am
Sorry to be confusing, you still need to subclass MyDataMapper, not DataMapper.
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!
#606 / Feb 27, 2009 3:09am
That fixed it! Thanks!
#607 / Feb 27, 2009 8:19am
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
A PHP Error was encountered.Severity: Notice.Message: Undefined property: stdClass::$transactionthe user get’s saved but nothing else
any tips ?
in my controller i have
$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;
}
}#608 / Feb 27, 2009 10:31am
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 !
/**
* 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;
}
}#609 / Feb 27, 2009 3:24pm
@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.
#610 / Feb 27, 2009 3:55pm
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
#611 / Feb 27, 2009 4:03pm
OK, looked into it some more. Sorry about the confusion, I don’t use auto-transactions, so I misunderstood what is happening.
It is definitely a bug of sorts. The ‘transaction’ error message is only set if the transaction itself fails (which it never should, unless something really bad is happening on the server!)
To work around it, you should check to see if the transaction error has been set:
if(isset($obj->error->transaction)) {
// show transaction error
}You could also fix this by adding this line into the DataMapper souce:
// around line 1015
}
$this->error->transaction = ''; // <<< Add This Line
// Clear this objects "has many" related objects
foreach ($this->has_many as $related)I’ll add a fix into my testing copy of DataMapper that I am working on.
#612 / Feb 27, 2009 5:52pm
delete
#613 / Feb 27, 2009 6:02pm
Where are you seeing the translation error? Usually you can just specify the table name by hand, and that fixes the issue (that’s how I do it, and I use the model Status/Statuses).
There is no real catch-all to fix pluralizing English words, without some large dictionary. It’s better just to hard-code those few outliers.
#614 / Feb 27, 2009 6:05pm
True… However for some reason status and statuses wasnt working for me…
Here however might be the fix for it if you want to add it to the inflector…
add to singular before the else
elseif ($end2 == 'es' && strleng($str) > 4)
{
$str = substr($str,0,-2);
}change
elseif (in_array($end1, array('h', 'o', 'x')))to
elseif (in_array($end1, array('h', 'o', 'x')) OR $end3 == 'tus')#615 / Feb 27, 2009 7:05pm
Heres what happens with status
Unknown column ‘join_brainstorm_statuses_brainstorms.brainstorm_statu_id’ in ‘on clause’
ignore the brainstorm stuff…
and yes its defined in my model.
class Brainstorm_status extends DataMapper
{
var $table = 'brainstorm_statuses';
var $has_many = array("brainstorm");ALlright I found the problem!
For some reason when its getting the $common_key gets the singular and it takes the s off the end… since its not “plural”
Solution is to change this line: (line 75 to 81 for me)
else
{
if ($end1 == 's')
{
$str = substr($str, 0, -1);
}
}to
else
{
if ($end1 == 's' && $end2 != 'us')
{
$str = substr($str, 0, -1);
}
}