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

[eluser]Mark Price[/eluser]
I have the table field set to allow null in the database but once it is set with a value I can't figure out how to get Datamapper to set it back to null.

[eluser]WanWizard[/eluser]
So, what happens if you do this? What's the result of $u->check_last_query() after that save?

[eluser]Mark Price[/eluser]
When I set a field to NULL it looks like it works as if I did not define the field at all. This is the query that runs when I run this code:

Code:
$u = new User();
$u->id = 1;
$u->username = "foo";
$u->password = "bar";
$u->email = NULL;
$u->save();  

UPDATE `users` SET `username` = 'foo', `password` = 'bar' WHERE `id` = '1'

[eluser]WanWizard[/eluser]
You are not allowed to set an id manually, this MUST be an autoincrement column. You are now creating a new object, but the presence of the id indicates an existing object (hence the UPDATE query). But the object contains no stored data.

Code:
// get the user record for user id 1
$u = new User(1);

// reset the email address and save the record
$u->email = NULL;
$u->save();

[eluser]Mark Price[/eluser]
I am wanting to update a row that already exists in the database and set one of its fields to NULL.

For example, I have two different user types and one of the user types has an extra field in the database to store data that doesn't apply to the other user type.

My goal is to be able to change the user type from an admin panel on an existing user and that would set the inapplicable field to NULL.

[eluser]WanWizard[/eluser]
If you want to update, retrieve the object first, then update it, as the example I gave you.

[eluser]Mark Price[/eluser]
Perfect, Thank you for the help. I did not realize I had to get the row first.

[eluser]Arministrator[/eluser]
Me againSmile

I've encountered an overwrite that I didn't expect. I came up with the solution (not tested), but it got me thinking why is it happening, so If you would care to clarify...

Code:
$donor = new Donor;
$donor->donation->order_by(...)->get_iterated();

$data['donor'] = $donor;

$last_donation = $donor->donation->order_by(...)->limit(...)->get();

$data['last_donation'] = $last_donation;

2nd query seems to overwrite my first query and $data['donor'] returns only one row.
I understand that Donor object attributes get rewritten, but why does a variable I set gets rewritten too?

I've tried this to no success:

Code:
$d = $donor->donation->order_by(params)->get_iterated();
$data['donor'] = $d

I figure if I instantiate a new Donor object I could get around this, but this isn't optimal.

[eluser]WanWizard[/eluser]
You're running the second query on the same object.

Remember it is not a method or function that returns a result, Datamapper uses methods to manipulate the object itself (and returns itself (i.e. $this) for the purpose of chaining).

In PHP5, per default objects are assigned by reference, so when you do
Code:
$donor = new Donor();
$test = $donor;
$test actually points to the same object as $donor. See http://www.php.net/manual/en/language.oo...rences.php for an explanation.

If you want a copy of the object, use the get_clone() method. Which will create a new object as well, but with content, unlike when you use new. In terms of processing, I don't think there's much difference between the two, but I haven't measured it.

edit: now that I think of it, perhaps this will even work:
Code:
$donor = new Donor;
$data['donor'] = $donor->donation->order_by(...)->get_iterated();
$data['last_donation'] = $donor->get_clone->donation->order_by(...)->limit(...)->get();

[eluser]Arministrator[/eluser]
I do understand what you're saying, I mentioned in my previous post that I know that object's data will be overwritten, by the 2nd query. My question really is: isn't it possible to save objects current attributes to, say, a multidimensional array, run another query to fetch another result, and than save that to the next array, and so on. Can't an array be completely unaware of the object that passed the data to it?

Because as long as I want to manipulate given object's data I don't feel (I use the word "feel" often, because I'm still not that proficient in OOP, and go by guts a lot) it's necessary to make new objects, but rather save and pass the data I need from the current object.

Did I make any sense at all? Because the whole point of this rambling of mine is to try to understand things better, not to question common practice (or best practice). If anything, I'd like to learn them first, and question them later Smile




Theme © iAndrew 2016 - Forum software by © MyBB