Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)

[eluser]Guido Mallee[/eluser]
Heck, of course that isn't a has_many relationship. Thanks!

The order_by and limit also did the trick there!

[eluser]Colm Ward[/eluser]
Hi Folks

Apologies if this is a stupid question. Is there any convenient way I can override or overload save()?

The docs explicitly says that an extension will not overwrite a DataMapper method, so I can't do it that way. I could modify save() in the core Datamapper library but I'd rather not mess with that. I could do it in each model but I have a lot of models so that's a lot of duplicate code.

Any ideas?

[eluser]The Hamburgler[/eluser]
I would extend the Datamapper class with your own library, and then have your models extend from the new class.

When I have done this previously I have added code to the classes constructor to prevent the object looking for a table called datamapper_ext.

Code:
class DataMapper_Ext extends DataMapper {

    public function __construct($id=NULL)
    {
        if(get_class($this) == 'DataMapper_Ext')
            return;
        
        return parent::__construct($id);
    }

}

Hope that helps

[eluser]Colm Ward[/eluser]
Excellent, thanks for that. Will give it a shot. I did indeed get caught out with a non existent table being looked for when I tried it that way, so thanks for the tip.

I'm trying to build in access control to models by overloading save() and get(). The basic idea is, if the logged in user is allowed to 'read' the model they are retrieving, go ahead with the regular get() in the parent. Same thing with overloading save() to handle write access.

Hopefully it will work! :-)

[eluser]PoetaWD[/eluser]
Double posted....

[eluser]PoetaWD[/eluser]
Is there a way to get the id after save() ??

I am building a form that dinamicaly adds records to a table.

After the record is saved, it will return the ID of the created record and append it to the form.

It will send this ID as a $POST and will save the form data related to the dinamicaly created record.

Is it possible to be made ?

[eluser]Dennis Rasmussen[/eluser]
[quote author="PoetaWD" date="1294464952"]Is there a way to get the id after save() ??

I am building a form that dinamicaly adds records to a table.

After the record is saved, it will return the ID of the created record and append it to the form.

It will send this ID as a $POST and will save the form data related to the dinamicaly created record.

Is it possible to be made ?[/quote]

After you save(), the object will have its new ID added.

Code:
$user = new User();
$user->name = "mr bean";
$user->save();

echo $user->id; // the new ID

[eluser]tdktank59[/eluser]
Ok I must be going insane or something.
I think this used to work at least lol...

Ive got 2 tables
users and statuses
they have a many to one relation (each user can have a status)

On the user table there is the field status_id

I am trying to pull the status for the user like this

This one does not work I get the error
"DataMapper Error: 'User' is not a valid parent relationship for Status. Are your relationships configured correctly?"
Code:
$user = new User();
$user->get_by_id(1);
$user->status->get();

$user->status->name;

However going from the status it works...
Code:
$status = new Status();
$status->get_by_id(2);
$status->user->get();
foreach ($status->user as $user) {
    print $user->username;
}

So just wondering whats going on, and want to make sure im not imagining things.
The models are setup properly
user: $has_one = array('status');
status: $has_many = array('user');

[eluser]WanWizard[/eluser]
I think you have to re-check everything, you must be missing something obvious.

The code above, with the corresponding model files and relationship definitions, works fine here.

[eluser]tdktank59[/eluser]
[quote author="WanWizard" date="1294504699"]I think you have to re-check everything, you must be missing something obvious.

The code above, with the corresponding model files and relationship definitions, works fine here.[/quote]

I thought as much, which is why I was pulling my hair out. Turns out the $model in the user script was $model = "User"; instead of "user" which caused the issue.

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB