Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.1

[eluser]Bard[/eluser]
Hi, i have a problem using foreign keys

'Content' table has many 'parts' table

Parts table
Code:
CREATE TABLE IF NOT EXISTS `parts` (
  `content_id` int(11) NOT NULL,
  `part` int(2) NOT NULL,
  `url` varchar(400) collate utf8_turkish_ci NOT NULL,
  KEY `content_id` (`content_id`),
  KEY `part` (`part`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

Code:
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'
        )
    );
Code:
class Part extends DataMapper {
    public $table = 'parts';
    var $has_one = array("content");

Code:
$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?


[eluser]WanWizard[/eluser]
Your table is missing the 'id' column, which is required in Datamapper. All operations happen on the id.

[eluser]ELRafael[/eluser]
Hi!

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

Code:
$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 (127, 11)


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

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
Code:
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.

[eluser]ELRafael[/eluser]
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

Code:
$has_many = array();

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

Peace!

[eluser]rzb13[/eluser]
I'm very sorry for double posting. I noticed too late it was not the version I'm using.

[eluser]Unknown[/eluser]
Kyle Noland

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




Theme © iAndrew 2016 - Forum software by © MyBB