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 - 04-23-2009

[eluser]camporter1[/eluser]
There appears to be a problem with the validation errors. Here's my validation for firstname:

Code:
var $validation = array(
        array(
            'field' => 'firstname',
            'label' => 'First Name',
            'rules' => array('trim', 'required', 'xss_clean', 'min_size' => 1, 'max_size' => 25, 'strip_tags')
        ),
(etc)

And here's the controller test code that I was using to figure out how error handling works:

Code:
$profile = new Profile();
            $profile->where('user_id', $profileid)->get();
            
            $profile->firstname = "abcdefghijklmnopqrstuvwxyz";
            
            $profile->save();
            print($profile->error->firstname);

Instead of getting an error about the fact that the firstname is too long (over 25), I get the error that: <i>The First Name field must be at least 1.</i> Which, even if it were the case, seems confusing by itself.

Anyone know if I'm doing something wrong?


DataMapper 1.6.0 - El Forum - 04-23-2009

[eluser]camporter1[/eluser]
Argh. My fault. Didn't notice there was a max_length and max_size. Two different validator types. The length one wasn't listed in the 'predefined' list on the validation manual page, and the form validation one wasn't working either (max_length[25]).


DataMapper 1.6.0 - El Forum - 04-24-2009

[eluser]NachoF[/eluser]
Im trying to figure out how this works and why its worth it... but Im a little confused on many things... first of all.. in the provided example.. why does the User Model not have a var $table?? while Country and Book do specify the name of the table...


DataMapper 1.6.0 - El Forum - 04-24-2009

[eluser]OverZealous[/eluser]
If you read the documentation more carefully, you'll see that $table and $model are automatically determined if they are not provided, based on the class name of the model. Providing them is optional.

DataMapper is about increasing development productivity, as well as helping to standardize your codebase. 95% of your database access code is abstracted away, and validation can be automated for most objects. It speeds up development, helps to ensure data integrity, and is significantly easier to read.


DataMapper 1.6.0 - El Forum - 04-24-2009

[eluser]camporter1[/eluser]
You will most certainly not miss using SQL queries or even just activerecord (although DataMapper works similarly).


DataMapper 1.6.0 - El Forum - 04-24-2009

[eluser]NachoF[/eluser]
Ok, Im still having trouble grasping the concept though.. but Im very willing to give it a shot.. please help me with this little example and I will try to understand it all so that I wont need any help changing the rest of my project.

My project has a table 'products' and a table 'existence' (although Im willing to change the name to existences for the sake of this to work)... in the products table I only have id and name of the product.. while in the existences table I have id (im not sure I need this), id_product ammount, price and date .... I do this cause its for a company that receives different shipments of the same product at different times and different prices... how exactly would I create the datamapper classes for this two models?? and how would I do a select that returns a table

ProductName | Price | Ammount


I know its a lot to ask but I have been trying to figure it out for the past half and hour and Im not sure how to get this done.

I would also appreciate recommendations on naming conventions.
Edit:
I dont understand something.. I have products and existences... one product has many existences and one existence has only one product... why do I need a new table??

A Database Error Occurred
Error Number: 1146
"Table 'company.existences_products' doesn't exist"


DataMapper 1.6.0 - El Forum - 04-24-2009

[eluser]OverZealous[/eluser]
First, as the docs say, every single model requires an id field. (Also, it is preferred that you have an id field on all of your relationship tables, but this is not actually used by DataMapper.)

Second, the original DataMapper only supports Fifth form database normalization, which means that every relationship requires it's own table, even if it is 1-to-N related. So the original DM won't support using product_id. However, DMZ DOES support it. DMZ is a drop-in replacement, so you can decide which one you prefer. The additions provided by DMZ are not nearly as well documented as the original DM.

Third, what you are describing is fairly basic. Each Model needs to be named as the singular form, with the table in plural form. You add 'product' to the $has_one array for Existence, and you add 'existence' to the $has_many table of the Product model. The example on Setting up Relationships is almost an exact duplicate of what you are trying to do.

To select a row, you simply use the get() method, which is related to the ActiveRecord code DM is based on. If you want to get all Existences of a specific Product, it looks like this:
Code:
$product = new Product();
$product->get_by_id($id);
$product->existence->get();

foreach($product->existence->all as $exist) {
    // handle each existence here
}

I believe this is also incredibly well explained.

Using a new code library is always going to be difficult. You need to really take the time to read through the examples, and read through the instructions. With a library as well documented as DataMapper, it shouldn't be this difficult.


DataMapper 1.6.0 - El Forum - 04-24-2009

[eluser]NachoF[/eluser]
[quote author="OverZealous.com" date="1240642604"]
If you want to get all Existences of a specific Product, it looks like this:
Code:
$product = new Product();
$product->get_by_id($id);
$product->existence->get();

foreach($product->existence->all as $exist) {
    // handle each existence here
}

[/quote]
Thanks, I got that working now..... one more question, what I want is a table that has every existence of a product.. for instance

productName | Quantity | Price
Whisky 25 1300
Whisky 30 1200
Beer 100 500

The way i did that before was using a very specidic query inside my model and then just use this->table->generate($query); and save that to a $data['table'] variable that gets echoed inside my view.... is there no way i can use the table generate function anymore if I use this?


DataMapper 1.6.0 - El Forum - 04-24-2009

[eluser]OverZealous[/eluser]
Do you mean all existences of all products? Because I showed you what you asked. If you mean all existences, don't make it so complicated:
Code:
$existence = new Existence();
$existence->get(); // contains all existences
foreach($existence->all as $e) {
    $product = $e->product->get();
    // do whatever
}

If you want to do multiple results in a single query, use DMZ instead, and try this:
Code:
// note: add or replace 'id', 'name' with any fields you need from Product.
$existence->join_related('product', array('id', 'name'))->get();
foreach($existence->all as $e) {
    $product_id = $e->product_id;
    $product_name = $e->product_name;
    // do whatever
}

Join related only works for $has_one relationships, by-the-way.

Now, you should be able to go from here, because, as I mentioned earlier, the manual for DataMapper is very complete, and has a lot of examples for how to build queries.


DataMapper 1.6.0 - El Forum - 04-25-2009

[eluser]NachoF[/eluser]
[quote author="OverZealous.com" date="1240652546"]Do you mean all existences of all products? Because I showed you what you asked. If you mean all existences, don't make it so complicated:
Code:
$existence = new Existence();
$existence->get(); // contains all existences
foreach($existence->all as $e) {
    $product = $e->product->get();
    // do whatever
}


Now, you should be able to go from here, because, as I mentioned earlier, the manual for DataMapper is very complete, and has a lot of examples for how to build queries.[/quote]

Yes, thats what I meant... all existences of all products... my bad... anyway, I dont think Ill be needing DMZ...following your advices I have accomplished the table creation like so
Code:
$this->table->set_heading('Name', 'Price', 'Quantity');
$e = new Existence();
        $e->get();
        foreach($e->all as $existence) {
        $product = $existence->product->get();
        $this->table->add_row($product->name, $existence->price, $existence->quantity);        
        }
        $data['ProductsTable']= $this->table->generate();

Ill keep on messing with this cause it definitely seems to save a lot of time.. thanks man.