EllisLab text mark
Advanced Search
31 of 31
31
   
DataMapper ORM v1.8.1
Posted: 15 May 2012 05:40 AM   [ # 451 ]   [ Rating: 0 ]
Joined: 2011-11-06
6 posts

Hi, i have a problem using foreign keys

‘Content’ table has many ‘parts’ table

Parts table

CREATE TABLE IF NOT EXISTS `parts` (
  `
content_idint(11NOT NULL,
  `
partint(2NOT NULL,
  `
urlvarchar(400collate utf8_turkish_ci NOT NULL,
  
KEY `content_id` (`content_id`),
  
KEY `part` (`part`)
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
class Content extends DataMapper {
    
public $table 'contents';
    var 
$has_many = array(
        
'label',
        
'part' => array(
            
'class' => 'part',
            
'other_field' => 'content',
            
'join_self_as' => 'content',
            
'join_other_as' => 'part'
        
)
    ); 
class Part extends DataMapper {
    
public $table 'parts';
    var 
$has_one = array("content"); 
$c = new Content();
$c->where('id',1);
$c->get();
$c->part->get();
$c->delete_part($c->part->all); 

doesn’t works, is it normal not to delete foreign table relation or missing something?

 

 
Posted: 15 May 2012 05:47 AM   [ # 452 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4489 posts

Your table is missing the ‘id’ column, which is required in Datamapper. All operations happen on the id.

 Signature 

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

 
Posted: 14 January 2013 03:16 PM   [ # 453 ]   [ Rating: 0 ]
Avatar
Joined: 2007-09-11
293 posts

Hi!

I have a issue when I’m dealing with many to many relationships.

$obj = new Associado(1);
$folder = new Pasta();
$folder->name 'banana';
$folder->save($obj);
echo 
$this->db->last_query();
//Returns
INSERT INTO `cms_associados_pastas` (`pasta_id`, `associado_id`) VALUES (12711)


$obj = new Aviacaom(1);
$folder = new Pasta();
$folder->name 'banana';
$folder->save($obj);
echo 
$this->db->last_query();
//Returns
UPDATE `cms_aviacao_pastasSET `pasta_id` = 128, `aviacao_id` = 1 WHERE `aviacao_id` = 

This code (both blocks) is supposed to create a record in the table Pasta (mean Folder) and associate a “Associado” (can be User) to that created reg from Pasta table.

The first block is doing right, INSERT INTO relationship table (associados_pastas).
The second is wrong. It’s trying to UPDATE (?!?!?) the relationship table (aviacao_pastas).

The name of the model (Aviacaom) is right. I just configured the model using $has_many variable inside the model

class Pasta extends DataMapper {
  
public $has_many = array(
    
'aviacaom' => array(
      
'class'              => 'aviacaom',
      
'other_field'     => 'pasta',
      
'join_self_as'    => 'pasta',
      
'join_other_as' => 'aviacao',
      
'join_table'      => 'cms_aviacao_pastas'
    
)
  );
}

class Aviacaom extends DataMapper {
    
public $table 'aviacao';
    public 
$has_one = array(
  
'pasta' => array(
   
'class'              => 'pasta',
   
'other_field'     => 'aviacaom',
   
'join_self_as'    => 'aviacaom',
   
'join_other_as' => 'pasta'
  
)
 );

Anyway, thanks for any help and sorry about Portuguese language in the code. I just copy ‘n paste the code.

 Signature 

cool mad
Rafael
Last.fm
Twitter

“Because they’re stupid, that’s why. That’s why everybody does everything!”

 
Posted: 21 January 2013 07:54 AM   [ # 454 ]   [ Rating: 0 ]
Avatar
Joined: 2007-09-11
293 posts

Gosh, I was so stupid!

Sorry about the post, now I know what I did wrong.

With my database schema, I need to put $has_many on the Aviacaom model, not $has_one.
That’s why DataMapper was trying to UPDATE, since when I put $has_one, it’s 1:N relationship, not N:N

$has_many = array(); 

I hope with anyone gets this kind of error, this post can help.

Peace!

 Signature 

cool mad
Rafael
Last.fm
Twitter

“Because they’re stupid, that’s why. That’s why everybody does everything!”

 
Posted: 04 March 2013 10:52 PM   [ # 455 ]   [ Rating: 0 ]
Joined: 2013-02-22
7 posts

I’m very sorry for double posting. I noticed too late it was not the version I’m using.

 
Posted: 05 March 2013 10:00 AM   [ # 456 ]   [ Rating: 0 ]
Joined: 2013-03-05
1 posts

Kyle Noland

Here is an ORM that works with SQL Server
https://www.kellermansoftware.com/p-47-net-data-access-layer.aspx

 
31 of 31
31