EllisLab text mark
Advanced Search
1 of 31
1
   
DataMapper ORM v1.8.1
Posted: 08 June 2011 07:48 PM   [ # 11 ]   [ Rating: 0 ]
Joined: 2010-11-05
7 posts

Question about deleting relationships.

I want to relate Object_A to many Object_B with a series of checkboxes. If it’s checked, make the relationship.

But when I want to edit the relationships I need a way of deleting the relationships, but leaving the object itself intact.

Is there an easier way than getting all the existing relationships, deleting them all, then going through the new list and creating the relationships again.


Thanks WanWizard for continuing the development of this awesome ORM!!

 
Posted: 09 June 2011 04:05 AM   [ # 12 ]   [ Rating: 0 ]
Joined: 2011-05-30
18 posts

When i validate completely using my forms and i dont want to rewrite the same rules for my orm-model, how am i able to write data into the database?

My class:

class User extends DataMapper {
    
    
var $table 'user';
    var 
$has_one = array('group');
    
    var 
$validation = array();
    
    
/*
     * validates if given passwort belongs to user
     * @access public
     * @param $username string
     * @param $password string
     */
    
public function validate($username$password{
        $user 
$this->where('username'$username)->get();
        if(
$user{
            $userPassword 
$user->password;
            if(
sha1($password) == $userPassword{
                
return true;
            
}
        }
        
return false;
    

In the controller i do the following:

$userInput $this->add_user_form->post();

                
$newUser = new User();
                
$newUser->username $userInput->username;
                
$newUser->title $userInput->title;
                
$newUser->prename $userInput->prename;
                
$newUser->name $userInput->name;
                
$newUser->email $userInput->email;
                
$newUser->password sha1($userInput->password);
                
$newUser->group_id $userInput->group;

                
$newUser->skip_validation()->save(); 

I get the following error: You must use the “set” method to update an entry.

There is nothing inside the user table, which could be updatet. What is wrong?

PS: using v1.8.1

 
Posted: 09 June 2011 04:15 AM   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

@Steve,

No, there isn’t. But you’ll have to retrieve the current relations anyway, to be able to prepopulate the checkboxes.
You can use that result to delete then as well.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 09 June 2011 04:21 AM   [ # 14 ]   [ Rating: 0 ]
Joined: 2011-05-30
18 posts

My aim is to just write the validated form-values to the database without validating them again against the same rules.

Is this possible?

 
Posted: 09 June 2011 04:21 AM   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

@benboi,

You haven’t defined any validation rules in your model, so no skip_validation() is needed.

The save() method does an UPDATE when $modelobject->id contains a value, and an INSERT if it’s NULL. Is this all your code, or are you assigning a value to the ‘id’ field somehwere?

The idea behind model validation is that you don’t have to do that in your controllers. No matter where in your application you update a model, the validation is run. If you do it in your controllers, you have to add validation code everywhere you’re updating the model.

Which will become a maintenance nightmare if your application has any size, and you add or change your table structure, you’ll have to scan your code to update all your controller-based validation code.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 09 June 2011 04:32 AM   [ # 16 ]   [ Rating: 0 ]
Joined: 2011-05-30
18 posts

Hello,

im not assigning an id to the “User”-object. I just made a var_dump on that attribute and it returns NULL, so an insert must be done by DataMapper, but it doesnt.

When i write data manually to the data-table, my data can be read using

$users = new User();
$users->get(); 

I don’t understand the problem…

Thanks for your help wink

 
Posted: 09 June 2011 04:41 AM   [ # 17 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

I can’t reproduce it.

$test = new Test();
$test->field 'value';
$test->save(); 

just inserts the record and assigns the insert_id() to $test->id.

When you force an insert, does that work:

$test->save_as_new(); 

just to verify nothing else is broken?

Which version of CI are you using? Which database driver?

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 09 June 2011 04:50 AM   [ # 18 ]   [ Rating: 0 ]
Joined: 2011-05-30
18 posts

The force to save didnt work too ....

Hmmm, just created a new User object and just set name = ‘foo’, not even that is getting saved :/

Im Using the current CI-Reactor and Mysql as driver.

Is it maybe because of the has_one connection to group?

 
Posted: 09 June 2011 05:25 AM   [ # 19 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

No, there doesn’t seem to be anything wrong with your code.

Is error_reporting enabled? If not, any PHP errors, notices, warnings, in your logs?

What’s the output of $newUser->check_last_query() after you have called save()?

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 09 June 2011 07:15 AM   [ # 20 ]   [ Rating: 0 ]
Joined: 2011-05-30
18 posts
$bla = new User();
                
$bla->name 'foo';
                
$bla->save();
                
                
var_dump($bla->check_last_query()); 

SELECT *
FROM (`user`)
WHERE `user`.`username` =  ‘’

string(63) “

SELECT *
FROM (`user`)
WHERE `user`.`username` =  ‘’

 
Posted: 09 June 2011 07:23 AM   [ # 21 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

I’m clueless. It doesn’t get any simpler than this. You don’t have a save() method in your model do you?

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 09 June 2011 09:46 AM   [ # 22 ]   [ Rating: 0 ]
Joined: 2011-05-30
18 posts

I found it .... -.-

Was a method inside my User-class which has validate as its name ..... now without it works.

Thanks for your help.

 
Posted: 09 June 2011 09:47 AM   [ # 23 ]   [ Rating: 0 ]
Avatar
Joined: 2008-10-15
147 posts

How would you save a data shared by a N:N relation?
Let me explain:
You have products and attributs for instance.
Products may have 1 or N attributs but Attributs could be used with 1:N products. If you had a sorting order you end with a value to be stored when saving the relationship.

class Product extends DataMapper {
    
    
public $has_many = Array('attribut');
class Attribut extends DataMapper {
    
    
public $has_many = Array('product');

When I create a new product I select the attributs I want to be part of the product. I also want to save some data such as value, sorting order or whatever. How would you recommand me to do this?
Create a another model that will have many product and attribut? Example :

class Product extends DataMapper {
    
    
public $has_many = Array('product_attribut_detail');
}
//------------------------
class Product_attribut_detail extends DataMapper {
    
    
public $has_one = Array('product''attribut');
}
//------------------------
class Attribut extends DataMapper {
    
    
public $has_many = Array('product_attribut_detail');
 
Posted: 09 June 2011 11:10 AM   [ # 24 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts
benboi - 09 June 2011 01:46 PM

Was a method inside my User-class which has validate as its name ..... now without it works.

I should have spotted that. wink

Yes, you have to be careful overloading Datamapper methods. The list of reserved names can be found here.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
Posted: 09 June 2011 11:16 AM   [ # 25 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

@Spir,

If I understand you correctly:

You have models called Product and Attribut, in an N:M relation. They will have a $has_many to oneanother. You will need a relationship table connecting the two, called Attributs_Products, containing 3 fields: ‘id’, ‘attribut_id’ and ‘product_id’. This relationship table doesn’t need a model. So your first code block is correct.

You make relations by loading the required objects, and then link them:

// get a product and an attribute
$product = new Product(1);
$attribut = new Attribut(1);

// relate the two
$product->save($attribut);

// get all attributes
$attribut->get();

// relate all of them to the product
$product->save($attribut->all); 

If you have values that don’t belong to product or attribute, but are part of the relation, you need to add these fields to the Attributs_Products table. Use the Datamapper join_fields methods to manipulate them. See the manual for some examples.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM <= LOOKING FOR A NEW MAINTAINER!

 
1 of 31
1