Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]WanWizard[/eluser]
No, there isn't. It uses several components of the CI framework, most importantly the DBAL (database layer).

There are plans to make the next version framework independent, but that doesn't help you now, as I don't expect it before the end of the year.

[eluser]pyweb1[/eluser]
Does datamapper support repopulating form with set_value()? i can't find any information about repopulating when validation fail.

thx!

[eluser]WanWizard[/eluser]
When it comes to validation, there are two ways of handling them. Which you use depends on personal preference.

Some use the CI way of validation: they use the form validation library, set_value(), and only after succesful validation they create a new model object and populate it. You can do that manually by assigning the POST value to the properties, or use the from_array() method from the 'array' extension.

Some use the DM way of validation: form data is directly assigned to the properties of a new object, and the validation rules of the model are used to validate the data. In case validation failed, you pass the object back to the view, you don't need set_value() as that is linked to the form_validation library which is not used in this case.

I use the last way, like so:
Code:
public function add()
{
    // create a new object
    $model = new Modelname();

    // was the form posted?
    if ( $post = $this->input->post() )
    {
        // populate the object
        $model->from_array($post);

        // save the new object, which runs validation
        if ( ! $model->save() )
        {
            // did validation fail?
            if ( $model->valid )
            {
                 // insert failure
            }
            else
            {
                 // validation failure
            }
        }
        
        // save succesful, redirect to edit
        redirect('edit/'.$model->id);
    }
    else
    {
        // new object, add some default values to the object if needed
    }

    // load the view
    $this->load->view('add', array('model' => $model));
}

Edit works the same, but in that case you load an existing record instead of creating a new one.

[eluser]pyweb1[/eluser]
awesome answer, thx a lot!

a noob question about something I find unclear in the user guide, i think it may help someone else!

it's about alphabetic order for join tables names,

I have multiple word tables name like car_names. let say that a single car has many commons name all register in car_names.

I have a table called cars and now i want to make a join table.

should i name it cars_car_names or car_names_cars. is "_" arriving before "s" or after "s" in the alphabetical order?

thx a lot!

[eluser]WanWizard[/eluser]
It doesn't matter if the parent tables have underscores, they are just glued together using a string comparison.

So "s" < "_" will determine the order. An underscore is ASCII 95, a lowercase s is ASCII 115, so "car_names_cars".

Alternatively you can use the 'join_table' setting in the relationship definition to give it a completely different name.

[eluser]animatora[/eluser]
I was reading the manual on advanced relationships, and it is not clear to me how to the the following:

Table Users
- id
- name

Table Posts
- id
- post
- user_id

In my Post model class I define the relationship as follows:
Code:
public $has_one = array(
            'creator' => array(
              'class' => 'user',
              'other_field' => 'created_post',
              'join_self_as' => 'user',
                        'join_other_as' => 'post',
              'join_table' => 'posts'  
            )
    );

User model:
Code:
$has_many = array(
                'created_post' => array(
        'class' => 'post',
        'other_field' => 'creator',
        'join_self_as' => 'user',  
        'join_other_as' => 'post',
        'join_table' => 'posts'
      )
        );

Is this is the right way to represent the one-to-many relationship between the objects? There is an example on many-to-many. Do I have to override all the relation properties when defining the relation ?

Thanks

[eluser]WanWizard[/eluser]
That is a very standard table definition, so no advanced relationship definition is needed. $has_one('user') and $has_many('post') should work fine.

The definition you're proposing looks like a copy/paste from the example in the manual, and is definately not going to work, as it refers to columns that are not present in the table, like "created_post_id".

The "Attribute table" and the section on "manually defining the relationship" explain what each of the settings do. You only have to define them if they are different from the default (for example, you only use "join_table" in a many-many relation that uses an external join table, and you're not using the default naming convention for this table).

[eluser]Maglok[/eluser]
I am back! I am still loving this library. Actually understanding some more of it.

Been trying to add a many to many relation to an exsisting object with extra join fields.

Example: Assume I have a Person object. I also have a Dog object. Now assume between the Person and Dog is a relationship of one to many. Finally lets say we want to store how many years the dog has been with the person.

This would result in a table Dogs, Characters and a join table between the two. In the join table I have a column 'years_together'.

Now I have a form in which you can input all these things. I then grab them by post and save like this:
Code:
$dog = new Dog();
$dog->where('id', $this->input->post('dog_id'))->get();

$person = new Person();
$person->where('id', $person_id)->get();

$person->save($dog);
All of this I got working. The thing that is not working yet is being able to define that 'years_together' field. At the moment the dog and person are definately having a 'relationship' *snicker*, but I don't know what syntax I should use to save join fields.

[eluser]sevenpointsix[/eluser]
I think this is quite straightforward. In your example, to save the join field, you'd just call:

Code:
$person->set_join_field($dog, 'years_together', 5);

Unless I'm missing something? See also: http://datamapper.wanwizard.eu/pages/joinfields.html

[eluser]Maglok[/eluser]
I tried that, I think it might not play nice with the save($dog) specifically.




Theme © iAndrew 2016 - Forum software by © MyBB