• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Datamapper: Get last id & relationship stuffs

#1
[eluser]scherman[/eluser]
Hi, i want to split this topic in three parts (everything about datamapper):

Part 1:
How can i do to get the id i've just inserted?
With this code, it doesn't work:
Code:
$image = new Image();
$image->url = $url;

if($image->save()){
   echo $image->id;
}

It returns 0

______________

Part 2:
I have two classes: Post and Image. Each Post can has one image, and each image can belong to many posts.
But i want that the image's post can add after create the post. For example, i am writing a post, but i am not finish yet, so i save it, and i will finish at the future.

My problem is that i don't know how to change the model to set the image as optional, and not required.

There is my post's class (only the image's part):
Code:
class Post extends DataMapper {
    
    var $table = 'posts';
    var $has_one = array('image');

    var $validation = array(
        'image' => array(
            'label' => 'Image',
            'rules' => array()
        )
    );
}

But when i want to save the post with the image ($post->save($image)), it returns me some strange errors (only put the one i consider the key to solve the problem):
Quote:An Error Was Encountered
Unable to relate post with .



______________


Part 3:
Which is the difference between the save() and the save_as_new()?
Because i had an error, and i didn't know how to fix it, and i try change save() to save_as_new, and it worked.
I wanted to save a new object, anything more.


I hope you can help me


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.