Welcome Guest, Not a member yet? Register   Sign In
datamapper removes the use of views?
#11

[eluser]WanWizard[/eluser]
Can you do a check_last_query() after the save, and post the generated query here?
Which version of Datamapper are you using? What does your table look like?

The save() method checks for the presence of the column 'id' in the object. If it has a value, it does an update, if it is empty(), it does an insert. (that means that you can't have an id of zero!).
#12

[eluser]johnmerlino[/eluser]
My object does have a column property but it still does an insert into statement:

Code:
var_dump($post->id);
$post->save();
$post->check_last_query();

output:
Code:
int(1) int(1)

INSERT INTO `posts` (`title`, `body`) VALUES ('HL Group', 'HL Group is an organization dedicated to helping others.')

So above $post has an id of 1. But the save() method still does an insert into statement rather than an update statement.

my posts table has the following structure:

id int auto_increment not null primary key length(11)
title varchar 255 null
description varchar 255 null
body text
created_at datetime
updated_at datetime
user_id int length(11)
page_id int length(11)

I am using the latest version of datamapper - datamapper orm.

Thanks for response.
#13

[eluser]WanWizard[/eluser]
I can't reproduce it.

I've created your table here (called it test, used only the id and title columns), and ran this code:
Code:
$test = new Test(1);
var_dump($test->to_array());

$test->title = 'NEW';
var_dump($test->to_array());

$test->save();
var_dump($test->to_array());

$test->check_last_query();

Resulting output:
Code:
array
  'id' => int 1
  'title' => string 'INITIAL' (length=7)

array
  'id' => int 1
  'title' => string 'NEW' (length=3)

UPDATE `tests` SET `title` = 'NEW' WHERE `id` = 1

array
  'id' => int 1
  'title' => string 'NEW' (length=3)

And I can't imagine someone so basic as save() would be broken. Datamapper is used by a lot of people, if nobody was able to save data, the forums would be flooded...

What does your Post model look like?
#14

[eluser]johnmerlino[/eluser]
Yep, I had my own save() method in my model that was overriding the save() method of super class. And the one I created was always doing an insert into statement.
#15

[eluser]WanWizard[/eluser]
Ah, little detail you left out?

It's not wise to override methods like that. If you need preprocessing of some kind, do it, but then call parent:Confusedave() to do the actual work. Datamapper does a lot behind the scene's, if you start replacing internal methods you can break Datamapper...
#16

[eluser]johnmerlino[/eluser]
Thanks for response. I noticed in another forum post you said that you can use $child->parent->get() to get related objects. I have an object defined and it has a parent and I established the has many and has one relationship between them and I try to access the parent:

var_dump($image->parent->get());

This throws this error:

Call to a member function get() on a non-object

So I presume parent is an array and does not have the get() method. But then how would you access the parent of object?

Thanks for response.
#17

[eluser]johnmerlino[/eluser]
ok it appears I didn't have to use parent.

I could directly refer to the parent as a property of current object and then get the id that way:

echo $image->post->get()->id;


Is this correct WanWizard? It appears to work.
#18

[eluser]WanWizard[/eluser]
With 'parent' I meant the parent object in the relationship, not a method or property called 'parent'. So yes, this is what I meant.

Note that get() will retrieve all posts related to that image, and '->id' will return the id of the first record found.
#19

[eluser]johnmerlino[/eluser]
[quote author="WanWizard" date="1301171695"]With 'parent' I meant the parent object in the relationship, not a method or property called 'parent'. So yes, this is what I meant.

Note that get() will retrieve all posts related to that image, and '->id' will return the id of the first record found.[/quote]

It appears I can't reference the grandparent of an object unless I get the parent first:

Code:
$post = $image->post->get(); //I have to do this. Otherwise category is null

            
            $category = $image->post->category->get();

Is this intended behavior?

Thanks for response.
#20

[eluser]WanWizard[/eluser]
There is no relation between image and category. In SQL terms, you can't create a JOIN between these two, without including post into the join.

And it currently works only one relation deep. If you don't fetch the posts first, you're asking for all categories of an empty post object, so the result will be an empty catagory object. It currently does not return all categories of all posts of that image. Objects are not aware of anything further up the chain, so this behaviour can't be altered without some automatic get() behaviour, which might lead to all kinds of side effects...




Theme © iAndrew 2016 - Forum software by © MyBB