Welcome Guest, Not a member yet? Register   Sign In
Need some help with Datamapper
#1

[eluser]mvdg27[/eluser]
Hi guys,

I just started using Datamapper (WanWizard version). I've read through all the documentation (well, almost everything) and tried some examples. So far I think I get the concept.

Now while trying my first real-life application, I can't get it to work exactly as intended. Basically I have this (simplified) situation:

1) I have users for my application. They are stored in a simple table (id, username, password)
2) I have projects (id, name and description)
3) When a project is created, it must be linked to the user that created the project, the owner so to speak (= the logged in user)
4) More than 1 user can be assigned as a member of the project.

So far I made two models. Project and User, with the following relations:

Code:
class User extends DataMapper {

    var $has_one = array(
        'owner_project' => array(
            'class' => 'project',
            'other_field' => 'owner'
        )
    );
    
    var $has_many = array(
        'member_project' => array(
            'class' => 'project',
            'other_field' => 'member'
        )
    );

   function __construct($id = NULL)
    {
        parent::__construct($id);
    }
    
    
}

class Project extends DataMapper {

    var $has_one = array(
        'owner' => array(
            'class' => 'user',
            'other_field' => 'owner_project'
        )
    );
    var $has_many = array(    
        'member' => array(
            'class' => 'user',
            'other_field' => 'member_project'
        )
    );
    
    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
    
    
}

Furthermore I've made a relations table with the following structure:

id | owner_id | owner_project_id | member_id | member_project_id

Now when I try to save a new project, the project get's saved, but no relationship is saved.

Code:
$oProject = new Project();
$oUser = new User($this->session->userdata('id'));            
                        
$oProject->name = $this->post('name');
$oProject->description = $this->post('description');
                            
if($oProject->save($oUser->owner)) {
                
    $return['success'] = true;
    $return['message'] = 'New project saved';
            
}
else {

    $return['success'] = false;
    $return['message'] = 'Error while saving';
    $return['errors'] = $oProject->error->all;
            
}

Clearly there is something wrong in my code. Can anyone point me in the right direction?

Thanks in advance! Cheers, Michiel
#2

[eluser]WanWizard[/eluser]
There are two mistakes in your code.

1. If you want to save a relationship, you will have to pass the object you want to relate. $oUser->owner is not an object, it's a property (and one that doesn't exist).
2. If you have multiple relations to the same table, you have to indicate which relation you're going to save.

Code:
// save the project, save the user as project owner
if($oProject->save($oUser, 'owner')) {
                
    $return['success'] = true;
    $return['message'] = 'New project saved';
            
}
an alternative is
Code:
// save the project, save the user as project owner
if($oProject->save_owner($oUser)) {
                
    $return['success'] = true;
    $return['message'] = 'New project saved';
            
}
See Save, Save an advanced relationship.




Theme © iAndrew 2016 - Forum software by © MyBB