Welcome Guest, Not a member yet? Register   Sign In
DataMapper 1.6.0

[eluser]OverZealous[/eluser]
I finally dug through my code (it's been a long while since I wrote that section), and I realized that I am actually hand checking to ensure that the fields are not empty, before I even call ->validate(). (I think I wanted a better error than the generic required error.)

In other words, you are absolutely correct. DataMapper does not handle ->validate()->get() correctly, because even if it is invalid, it still queries.

I might correct it on my version (DMZ), but for now I recommend just checking to ensure that the fields are not empty first. Alternatively, check $user->validate()->valid first and run the errors or then do the get() if it was valid.

[eluser]wilco512[/eluser]
Hi, I've just started using DataMapper and I've probably overlooked or forgotten something, but as a quick test I ran the following code, expecting it to output all the users' emails.

Code:
$u = new User();
            
foreach ($u->get()->all as $user)
{
    echo $user->email . '<br/>';
}

Instead I only get the email of the last user in the table. What have I done wrong?

[eluser]naren_nag[/eluser]
Could you post the code from your model, and the structure of the users table

[eluser]wilco512[/eluser]
Table users structure is :

Code:
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| ID       | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| username | varchar(255) | NO   |     | NULL    |                |
| f_name   | varchar(255) | NO   |     | NULL    |                |
| l_name   | varchar(255) | NO   |     | NULL    |                |
| email    | varchar(255) | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

Model code is :

Code:
class User extends DataMapper {
    
    function User()
    {
        parent::DataMapper();
    }

    function test() {
        
        $u = new User();
            
        foreach ($u->get()->all as $user)
        {
            echo $user->email . '<br/>';
        }
        
    }

}

[eluser]OverZealous[/eluser]
I know what's wrong. Your ID is uppercase, it needs to be "id". [Update: on the table.] The reason this matters is that DataMapper uses the ID as the index for multiple item results. Since "id" is being returned as "" (empty string), all of the users are being stored at the same index, overwriting each other.

[eluser]Peet86[/eluser]
Hello!
Im new in DataMapper and I have some probleme:

When my table is empty, I can save a new product this way:

Code:
function add(){
              $p = new Product();
              $p->status=$this->input->post("status");
              $p->brand=$this->input->post("brand");
              $p->name=$this->input->post("name");
              $p->note=$this->input->post("note");
              $p->url=url_title(strtolower($this->input->post('name')), 'underscore');
              if ($p->save()){
                  echo "ok";
              }else{
                  echo $p->error->string;
              }        
    }

But second time when I save an other product with different data, first row duplicates in the database instead of a new insert with product2 data. I dont understand..

Code:
id: 1
name: product1
brand: Nike
(..)

id: 2
name: product1 (instead of product2)
brand: Nike (instead of Adidas)
(..)

Whats wrong with my code?
Peter

[eluser]OverZealous[/eluser]
There's nothing that DataMapper is doing that could cause this. You are either inputting the same data, or your database has problems.

Turn on CodeIgniter's profiling (it's in the docs), and see what the query is. Use that to do some debugging.

<rant>
This isn't directed at any one person in particular, it's a general complaint ;-)

I don't understand why developers have a problem, and then don't even try some basic debugging. It doesn't take that much time, and it's not like someone can look at the code above and see if something is wrong.

Please take some time to do basic debugging. Look at your queries, echo() input variables, take some time to look over the documentation, and compare your code with example code. It helps reduce the number of "I did X. It didn't work. Help!" posts, which are almost always mistakes in custom code, not in the libraries.

As you can tell by looking through this thread, I don't have a problem helping people, and I do my best to ensure that problems are fixed (when I can, since DM isn't written or managed by me).
</rant>

[eluser]ale21ale[/eluser]
Hello Guys!

this is amazing ! , very thanks stensi. First, sorry for my english.

I have problems with the join of tables.

My data model :

Code:
class Costumer extends DataMapper {
        $table = 'costumers';
    var $has_many = array('invoice','receipt');

}

class Invioce extends DataMapper {
        $table = 'invoices';
    var $has_one = array('costumer');

        var $validation = array(
                array(
                    'field' => 'date',
                    'label' => 'Invoice date',
                    'rules' => array('valid_date','required')
                ),
                array(
                    'field' => 'total',
                    'label' => 'Invoice total',
                    'rules' => array('required')
                )
        );
}

class Receipt extends DataMapper {
        $table = 'receipts';
    var $has_one = array('costumer');

        var $validation = array(
                array(
                    'field' => 'date',
                    'label' => 'Receipts date',
                    'rules' => array('valid_date','required')
                ),
                array(
                    'field' => 'total',
                    'label' => 'Receipts total',
                    'rules' => array('required')
                )
        );
}

When i use my costumer model , i have 2 records set or objects set =):

$costumer->invoice and $costumer->receipts , but i need make a report like this :

from costumer id : 1

date (from receipts or invoices) invoice sum receipt sum
2008-03-20 200 u$s
2008-02-22 150 u$s
2008-02-23 150 u$s
2008-02-23 100 u$s

when using native sql query i created a table with union and ordered by date. Received a set of records and printed without any problems. How can I do this using datamapper?

Very thanks for the work. Congraturations =)
Ale.
Web development

[eluser]OverZealous[/eluser]
DataMapper does not provide the means to do what you ask. I recommend loading in all of the invoices and receipts that you want, and then sorting them in PHP.

It shouldn't take too long to sort those records, especially if each group is pre-sorted by date.

Pseudo-code:
Code:
$invoices = ... // load in your invoices
$receipts = ... // load in your receipts

$list = array_merge($invoices->all, $receipts->all);
usort($list, "sort_on_date");

// $list now contains a sorted list

// in a helper
function sort_on_date($a, $b) {
    // you may need to replace this with a different function if your dates aren't YYYY-MM-DD strings
    return strcmp($a->date, $b->date);
}

I forgot to mention that you can check to see what type an object is by using $object->model, which should be "invoice" or "receipt".

[eluser]Jack Scott[/eluser]
A couple DataMapper questions:

Is there a standard method for importing form fields from $_POST into a DataMapper object, aside from a bunch of $this->fieldname = $CI->input->post('fieldname') lines?

Is there a way to distinguish a form validation error from a database or other system error in the save() method?

Thanks for all your help,
Jack




Theme © iAndrew 2016 - Forum software by © MyBB