• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DataMapper 1.6.0

#21
[eluser]BaRzO[/eluser]
stensi i have a problem to show errors

in your example i have added
echo $this->error->username;
but i can not touch to error message
i get PHP error
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: Examples::$error
Filename: controllers/examples.php
Line Number: 105

Code:
// Save User

        if ($u->save())

        {

            echo '<br />';

            echo '<p>Current values:</p>';

            

            echo '<code><strong>ID</strong>: ' . $u->id . '<br />' .

            '<strong>Username</strong>: ' . $u->username . '<br />' .

            '<strong>Email</strong>: ' . $u->email . '<br />' .

            '<strong>Password</strong>: ' . $u->password . '<br />' .

            '<strong>Salt</strong>: ' . $u->salt . '</code>';

        }

        else

        {

            echo '<p><b>User has already been created</b></p>';
            echo $this->error->username;

        }
Thanks for reply...

#22
[eluser]stensi[/eluser]
Hi BaRzO. The errors are set on the object itself, not the Controller, so all you need to do with your code example is replace <b>$this</b> with <b>$u</b>. Like so:

Replace:
Code:
echo $this->error->username;

With:
Code:
echo $u->error->username;

Alternatively, you can show all errors as a single string (each error within it will be wrapped by the error prefix and suffix tags):
Code:
echo $u->error->string;
// This might output something like:
//   <p>Username is taken.</p>
//   <p>Password must be at least 6 characters long.</p>

Looping through all errors:
Code:
foreach ($u->error->all as $error)
{
    echo $error;
}

If you had instantiated your object like so:

Code:
$vehicle = new Vehicle();

You would access its errors like so:
Code:
echo $vehicle->error->property;

If you think I should update my User Guide to make this clearer, let me know.

#23
[eluser]BaRzO[/eluser]
Code:
// Create User
$u = new User();
$u->username = 'Fred Smith';
$u->email = '[email protected]';
$u->password = 'apples';
echo '<br />';
echo '<p>Current values:</p>';
echo '<code><strong>ID</strong>: ' . $u->id . '<br />' .
'<strong>Username</strong>: ' . $u->username . '<br />' .
'<strong>Email</strong>: ' . $u->email . '<br />' .
'<strong>Password</strong>: ' . $u->password . '<br />' .
'<strong>Salt</strong>: ' . $u->salt . '</code>';
echo '<hr />';        
echo '<p>Now to save it to the database.</p>';
echo '<code>
// Save User<br />
$u->save();</code>';
// Save User
if ($u->save())
{
echo '<br />';
echo '<p>Current values:</p>';
echo '<code><strong>ID</strong>: ' . $u->id . '<br />' .
'<strong>Username</strong>: ' . $u->username . '<br />' .
'<strong>Email</strong>: ' . $u->email . '<br />' .
'<strong>Password</strong>: ' . $u->password . '<br />' .
'<strong>Salt</strong>: ' . $u->salt . '</code>';
}
else
{
echo '<p><b>User has already been created</b></p>';
echo $u->error->username; // Unable to access an error message corresponding to your field name.
// print_r($u->error->all);
}
i did it but is giving this result...
Quote:Unable to access an error message corresponding to your field name.

edit :
Your user guide is very good i did read it all..
but if you can put some more example would be very nice i think maybe for beginers like me Smile

#24
[eluser]stensi[/eluser]
Ah, I see. I didn't give an example of setting up the error messages for that, sorry.

What DataMapper is trying to do is read an error message for the "unique" validation rule from your validation language file. One is not there by default since CodeIgniter's Validation class doesn't have the "unique" validation rule, DataMapper does, so it's not finding it.

To solve this for now, copy the following into a file named <b>validation_lang.php</b>, and put it in your <b>system/application/language/english</b> folder.

Code:
&lt;?php

// This is the new error message for the unique validation rule
$lang['unique']         = "The %s you supplied is already taken.";

$lang['required']         = "The %s field is required.";
$lang['isset']            = "The %s field must have a value.";
$lang['valid_email']    = "The %s field must contain a valid email address.";
$lang['valid_emails']     = "The %s field must contain all valid email addresses.";
$lang['valid_url']         = "The %s field must contain a valid URL.";
$lang['valid_ip']         = "The %s field must contain a valid IP.";
$lang['min_length']        = "The %s field must be at least %s characters in length.";
$lang['max_length']        = "The %s field can not exceed %s characters in length.";
$lang['exact_length']    = "The %s field must be exactly %s characters in length.";
$lang['alpha']            = "The %s field may only contain alphabetical characters.";
$lang['alpha_numeric']    = "The %s field may only contain alpha-numeric characters.";
$lang['alpha_dash']        = "The %s field may only contain alpha-numeric characters, underscores, and dashes.";
$lang['numeric']        = "The %s field must contain a number.";
$lang['integer']        = "The %s field must contain an integer.";
$lang['matches']        = "The %s field does not match the %s field.";


/* End of file validation_lang.php */
/* Location: ./system/application/language/english/validation_lang.php */

I'll make an updated version of DataMapper that includes a language file with this sort of thing already set up for you, since it will need a similar message for the other inbuilt DataMapper validation rules.

For any custom rules you make in your models that extend DataMapper, you can use the error_message() function.

UPDATE

A language file has been included with version 1.2.1 that has the validation error messages for the DataMapper-only validation rules.

#25
[eluser]BaRzO[/eluser]
Ok now it fixed Smile
thanks i will try my self to learn what can i do with DM Wink

Edit :
It's me again Smile
I want to ask... How we will drive with validation fields
Code:
$rules['username']    = "required";
$rules['password']    = "required";
$rules['passconf']    = "required";
$rules['email']        = "required";

$this->validation->set_rules($rules);

$fields['username']    = 'Username';
$fields['password']    = 'Password';
$fields['passconf']    = 'Password Confirmation';
$fields['email']    = 'Email Address';

$this->validation->set_fields($fields);

#26
[eluser]stensi[/eluser]
Good question, and thanks for bringing it up since I think you've pointed out a shortcoming in the current version that I didn't notice.

I've got the $rules side covered with the $validation array in the models, but I don't think I covered off the $fields naming side of things for error messsages. At the moment it uses the exact name of the field (username, email, etc) but as you've shown, sometimes you'll want it like "Email Address" or "Password Confirmation" which is currently not accomodated.

It'll require only a small change though, probably a change in format of the $validation array, or splitting it into a $rules and $fields array. I'll see what works best when I get the time, and then I'll write up a full working "real world" example on how to use all aspects of the validation, including the naming of the fields for error messages etc.

#27
[eluser]GregX999[/eluser]
Hi,

Two quick questions about DataMapper:

1. Why do you use join tables for one-one and one-many relationships instead of just using foreign keys?

2. Can you search based on relationships? (ie: if you have a "restaurant" model, a "menu" model and a "menu_item" model, and restaurants have one menu and menus have many menu_items, can you find all restaurants that serve items with "chicken" in the name? Can you find all menu_items served at Joe's Diner?)

Greg

#28
[eluser]stensi[/eluser]
1:
I know I could have used foreign keys for One to One and One to Many relationships but my personal preference is to make it so the normal tables have no information at all about any other tables (including foreign keys). So, they only contain the information relevant to themselves. I also found doing it this way made the implementation of DataMapper much easier.

DataMapper ensures the integrity of relationships stored in joining tables are kept intact for each of the different relationship types, so you don't need to worry about your One to One relationship becoming something else, it wont.

2:
Ok, so the Restaurant has a One to One relationship with Menu, and Menu has a One to Many relationship with MenuItem.

There's several ways you can find which restaurants have the "chicken" dish.

NOTE: I'm assuming the "menuitems" table has a "dish" field, and the "restaurants" table has a "name" field.

Here's one way (starting with the MenuItem):
Code:
// Dish to find
$dish = "chicken";

// Get the menu item the user wants
$menuitem = new MenuItem();
$menuitem->where('dish', $dish)->get();

// Loop through the menu's this dish is on
foreach ($menuitem->menu->all as $menu)
{
    // Show the restaurants that the menu belongs to (that has the dish)
    echo 'Restaurant ' . $menu->restaurant->name . ' has ' . $dish . '<br />';
}

Here's another way (starting with all restaurants, to find the restaurants with the dish):
Code:
// Dish to find
$dish = "chicken";

// Get all restaurants
$restaurant = new Restaurant();
$restaurant->get();

// Loop through all restaurants
foreach ($restaurant->all as $r)
{
    // Loop through all menu items in the restaurants menu
    foreach ($r->menu->menuitem->all as $mi)
    {
        // If the meni item's dish is the same dish we're looking for
        if ($mi->dish == $dish)
        {
            // Show the restaurant that has it
            echo 'Restaurant ' . $r->name . ' has ' . $dish . '<br />';
        }
    }
}

All MenuItems served at "Joe's Diner":

Code:
// Name of Restaurant
$name = "Joe's Diner";

$r = new Restaurant();
$r->where('name', $name)->get();

echo 'Restaurant ' . $r->name . ' has the following menu items:<br />';

foreach ($r->menu->menuitems->all as $mi)
{
    echo $r->dish . '<br />';
}

#29
[eluser]ray73864[/eluser]
From what i have read so far in the User Guide, i can see how powerful of a system this is.

I was however reading the section up on 'Automated Timestamps' and see at least one main problem with it.

The problem i see with it is that the 'Created' and 'Updated' fields have to be set 'DateTime' type, however when i am creating fields that hold dates i never store them that way i always have it as an integer field which stores the unix_time_stamp.

Is there a possibility of making it so that the automated timestamps can accept either of the 2?

Ray

#30
[eluser]stensi[/eluser]
Yep, that's do-able although if you need unix style timestamps urgently, you can manage them manually. I chose to use MySQL's DateTime for the automated timestamps because it's much more flexible. I'll make it a simple TRUE/FALSE setting as to which type gets used.

Just a bit of trivia, although a long way off the unix timestamp has a year 2038 problem. Not really an issue though since most people will fix their systems before then Wink


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.