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

[eluser]North2Alaska[/eluser]
[quote author="WanWizard" date="1332068607"]As to the question about timestamps, just overload the save() method in your model:
Code:
// your model
public function save($object = '', $related_field = '')
{
    // check if object has a 'created' field, and it is not already set
    if (in_array($this->created_field, $this->fields) && empty($this->{$this->created_field}))
    {
        $this->{$this->created_field} = 'add your custom data here';
    }

    // and call the DM save() method to save the object
    return parent::save($object, $related_field);
}
You can do the same with other fields or methods. There are users that have overloaded several methods to implement soft delete functionality, where records are not deleted but marked as deleted.

If you want to use this for multiple models, create a base model for it:
Code:
class Basemodel extends Datamapper
{
    // your base model methods here
}

class User extends Basemodel
{
   // your normal model info here
}
Now your User model will inherit all methods defined in the Basemodel, as well as all methods in the Datamapper class.[/quote]

This is exactly what I was looking for. Thank you!

[eluser]North2Alaska[/eluser]
This leads me to feature request.

In the save function we find

Code:
// Check if object has a 'created' field, and it is not already set
   if (in_array($this->created_field, $this->fields) && empty($this->{$this->created_field}))
   {
    $this->{$this->created_field} = $timestamp;
   }

I would like the update date also be set

Code:
// Check if object has a 'created' field, and it is not already set
   if (in_array($this->created_field, $this->fields) && empty($this->{$this->created_field}))
   {
    $this->{$this->created_field} = $timestamp;
    $this->{$this->updated_field} = $timestamp;
   }

What this would do is ensure that the update field would always be set. Subsequent queries would only have to search the update field for date information rather than having to check both.

[eluser]WanWizard[/eluser]
The created field is only set if it wasn't set before (which is only on a new object).

The updated field is modified a bit further in the code, when Datamapper detects you have changes one of more values (i.e. retrieving a record and saving it again doesn't modify the updated_field).

On new records the updated field isn't modified, as there haven't been any updates yet.

[eluser]North2Alaska[/eluser]
[quote author="WanWizard" date="1332098105"]On new records the updated field isn't modified, as there haven't been any updates yet.[/quote]

This is an ongoing battle in my 30 year career. Do you consider a new record as updated as well? Some camps take your position, that nothing has been updated, so no. Others take the position that it was an empty record before but was updated (by doing the insert) and as such, the update date should be recorded as well. I'm of this second camp.

The other justification is when doing a query to get all the records that have changed or created in a given period, it makes the queries simpler because you only have to query the update date. If it becomes significant when the record was created, (which it usually is not) then you can either compare the two or query the create date directly.

So, I'm going to use your suggestion above and overload the save function to handle my point of view. Maybe another config variable is in order. If you would like I could put together the code...

[eluser]WanWizard[/eluser]
You can't please everyone all of the time... Wink

[eluser]North2Alaska[/eluser]
I'm working on the Contact information of our system. I have a contacts table and an attributes table. The idea is the attributes table can hold a phone number, email address, IM account, or dates of some kind. Rather than a single attributes model, I'm thinking I would like to have a phone model that selects attributes of type phone.

Is this a reasonable plan or is there a better/more preferred way of doing this?

[eluser]WanWizard[/eluser]
If the amounts and type of attributes are variable, that is the only solution. Datamapper doesn't support EAV implementations.

[eluser]North2Alaska[/eluser]
[quote author="North2Alaska" date="1332201162"]I'm working on the Contact information of our system. I have a contacts table and an attributes table. The idea is the attributes table can hold a phone number, email address, IM account, or dates of some kind. Rather than a single attributes model, I'm thinking I would like to have a phone model that selects attributes of type phone.

Is this a reasonable plan or is there a better/more preferred way of doing this?[/quote]

OK, digging deeper into the documentation. I'm reading the advanced relationships section, and I'm getting close, but not there yet. It's a little more complicated than I detailed above. There are three tables involved; contacts, resources, and attributes.

Contacts is more than just this but this is all we need for this exercise.
CREATE TABLE `ab_contacts` (
`id` bigint(20) NOT NULL AUTO_INCREMENT
) ;

The attributes table is a simple list of type and value. Type may be phone, email, etc... and value may be work, home, mobile, etc...
CREATE TABLE `ab_attributes` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`type` varchar(25) DEFAULT NULL,
`value` varchar(25) DEFAULT NULL
) ;

Now we have a table to join them up called resources. As I type this I see that I may need to rename the table, but let's continue for the moment. Here we have just a value field to store the entered data.
CREATE TABLE `ab_resources` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`contact_id` bigint(20) DEFAULT NULL,
`attribute_id` bigint(20) DEFAULT NULL,
`value` varchar(255) DEFAULT NULL
) ;

We join the table with the contact_id to the contacts table and attributes table. This is really a many to many table with an extra field for the value related to the relationship. I can use the set_join_field function to populate this value. I'm good so far.

So, here is where I think the complexity is. When I query for the resources, I don't want all the resources, I just want the phone numbers for example. And there may be specific things that need to happen to the phone number (formatting it). To me this sounds like a model. But I don't see how to create a Phone model where only the resources with a related type of Phone are the results of the model.

I'm still reading the docs, but in the absence of a lot of testing, I'm not seeing it. Any pointers?

[eluser]North2Alaska[/eluser]
[quote author="WanWizard" date="1332201878"]Datamapper doesn't support EAV implementations.[/quote] I've not heard that term before. I'll study it out...

[eluser]WanWizard[/eluser]
You can use where_related_attibute('type', 'value') to select only a specific type when querying the relation.

If you need to format the phone number, you can use a set_rule to do that when you retrieve the data from the database. Make sure you have a corresponding rule that will convert the formatted value back to the raw data so when you save it, it doesn't save the formatted value.




Theme © iAndrew 2016 - Forum software by © MyBB