Many-to-Many does'nt save...

8 views
Skip to first unread message

asakurayoh

unread,
Sep 8, 2010, 3:32:05 PM9/8/10
to Outlet ORM
Hi.
For a reason I don't understand, my many-to-many relation does'nt
save.
Here my code:

//-----------------------

<?php

include('Outlet.php');
Outlet::init(include 'config.php');
$outlet = Outlet::getInstance();

include('BookEntity.php');
include('AuthorEntity.php');

$outlet->createProxies();

$author = new Author();
$author->Name = 'Isaac Asimov';

$book = new Book();
$book->Title = 'Foundation';
$book->getAuthors()->add($author);

$outlet->save($book);

//---------------------------

<?php
class Book {
public $Authors;

public function __construct () {
$this->setAuthors(new Collection());
}

public function getAuthors() {
return $this->Authors;
}

public function setAuthors(Collection $Authors) {
$this->Authors = $Authors;
}

}

//--------------------

<?php
class Author {
}

//---------------------

<?php
return array(
'connection' => array(
'dsn' => 'mysql:host=localhost;dbname=dbname',
'username' => 'username',
'password' => '*******',
'dialect' => 'mysql'
),
'classes' => array(
'Book' => array(
'table' => 'books',
'props' => array(
'ID' => array('id', 'int', array('pk'=>true,
'autoIncrement'=>true)),
'Title' => array('title', 'text')
),
'plural'=>'Books',
'associations' => array(
array('many-to-many', 'Author', array('table'=>'authors_books',
'tableKeyLocal'=>'id_books', 'tableKeyForeign'=>'id_authors'))
)
),
'Author' => array(
'table' => 'auteurs',
'props' => array(
'ID' => array('id', 'int', array('pk'=>true,
'autoIncrement'=>true)),
'Name' => array('name', 'text')
),
'plural'=>'Authors',
)
)
);

//---------------

So, as someone can help me on this?

The book is saved but not the author or the relation...
I found that in OutletMapper.php, at line 295 ("$children = $obj->
$getter();") the children is always empty.
If I var_dump $obj before this line, the Collection "Authors" is not
empty, but after, it's an empty "OutletCollection" that take it's
place...

I use the website download RC1 zip file.

I really like the way Outlet works and wan't to use it in my projects,
by it need a lots more of documentation, for all possibilities!


Thanks for your help!

Holtkamp

unread,
Sep 9, 2010, 8:19:01 AM9/9/10
to Outlet ORM
You now have a many-to-many association between Books (X) and Authors
(Y) defined, in my (working) config, I also add a many-to-many
association between Y and X.

additionally, I use identifiers that are a little more expressive than
simply 'id' in my tables:

Author(
authorId
)

Book(
bookId
)

To keep things clear ;)


You might want to consider using the XML based configuration, as the
array config tends to be a puzzle for me.

asakurayoh

unread,
Sep 9, 2010, 2:51:36 PM9/9/10
to Outlet ORM
That doesn't help...
I use that script only to test.
I use a YAML config file that is compile to php array.
I have tested with adding the inverse relation, but does'nt save too.

Can you send me you test files?

Is it because the two table avec the save id name field?
If that the case, that a bug in Outlet!

I test with the SVN version too and does'nt work too...


Thanks.

asakurayoh

unread,
Sep 9, 2010, 3:15:00 PM9/9/10
to Outlet ORM
I've done the test.
I change my ids for book_id and author_id and does'nt work more...

Here my 2 test:
http://dl.dropbox.com/u/1277776/Outlet-bug.zip

There is 2 folder:
- outlet-bug : there is the test with on "id" field name for the 2
principal table
- outlet-bug2 : The same test, with different id filed name...

The SQL.txt in each folder is the dump SQL to create the 3 table...
Only have to change the config to connect to the right DB....


If you can create a WORKING demo for me (and everyone), it can be
userful ;)

Thanks!

asakurayoh

unread,
Sep 13, 2010, 4:39:51 PM9/13/10
to Outlet ORM
Hi!
No answer wet........ Anybody who can create me an example of many-to-
many association saving well !!
If nobody can do it, it means that it's buggy :P

Thanks!

Holtkamp

unread,
Sep 15, 2010, 3:09:50 PM9/15/10
to Outlet ORM
Make sure all your classes have the attributes that are required.

In the ZIP you have posted, both the Book and Author class do not have
a 'id' attribute. I suggest the following:

class Author {
public $authorId;

public $name;

public $books;

public function __construct(){
$this->books = new Collection();
}

public function getBooks(){
return $this->books;
}

public function setBooks(Collection $b){
$this->books = $b;
}

public function addBook(Book $b){
$this->books->add($b);
}
}

class Book {
public $bookId;
public $title;
private $authors;

public function __construct(){
$this->authors = new Collection();
}

public function getAuthors(){
return $this->authors;
}

public function setAuthors(Collection $a){
$this->authors = $a;
}

public function addAuthor(Author $a){
$this->authors->add($a);
}
}



Good luck mate!

asakurayoh

unread,
Sep 15, 2010, 5:27:50 PM9/15/10
to Outlet ORM
No, that change absolutely nothing...

Don't said WITHOUT a working example... That's what I want! A really
simple functionning example. If you know how it can work, it's not
long to do! (maximum 5 min.).


Thanks!

asakurayoh

unread,
Sep 18, 2010, 12:11:37 PM9/18/10
to Outlet ORM
Ok.... just got it work... I'm sure I have test that way but.....
[...]

$a = new Author;
$a->Name = 'Asimov';

$b = new Book;
$b->Title='Foundation';
//$b->addAuthor($a);

$orm->save($a);
$orm->save($b);

$b->addAuthor($a);
$orm->save($b);

Thanks anyway.... :S
Reply all
Reply to author
Forward
0 new messages